[Insight-developers] MetaCommand::Parse() - improved handling of -v/-h options

Wheeler, Frederick W (GE Global Research) wheeler at ge.com
Thu Feb 24 12:43:38 EST 2011


These lines are from MetaCommand::Parse() in metaCommand.cxx in the
latest ITK, from git.  I think the two return statements should be
changed to "return false", or removed.

    if(!strcmp(argv[i],"-V") || !strcmp(argv[i],"-H"))
      {
      METAIO_STREAM::cout << "Usage : " << argv[0] <<
METAIO_STREAM::endl; 
      this->ListOptions();
      return true;
      }
    // List the options if using -v
    if(!strcmp(argv[i],"-v") || !strcmp(argv[i],"-h"))
      {
      METAIO_STREAM::cout << "Usage : " << argv[0] <<
METAIO_STREAM::endl; 
      this->ListOptionsSimplified();
      return true;
      }

As the code is now, if I run my program like this

  myprog.exe -v -bad-option unused extra stuff -i img.mhd

then Parse() returns true due to the -v option, never discovers that
there are bogus options (-bad-option unused extra stuff) and does not
parse correct options (-i img.mhd) and will not check to see if all
required options are set.  If the return statements are changed to
"return false", then the usual boilerplate like the following will stop
the program from continuing, which would be desirable.

  if( ! command.Parse( argc, argv ) )
  {
      std::cerr << argv[0] << ": Failed to parse command line.  Use -h
option for help." << std::endl;
      return EXIT_FAILURE;
  }

It might be OK to leave the "return true" statements as they are if
there was some other way to know that the user used a -v/-h option, so
one could do this:

  if( ! command.Parse( argc, argv ) )
  {
      std::cerr << argv[0] << ": Failed to parse command line.  Use -h
option for help." << std::endl;
      return EXIT_FAILURE;
  }
  if( command.ParsePrintedHelp() )
  {
      return EXIT_SUCCESS;
  }

If something like ParsePrintedHelp() is desired I'd be happy to produce
and test a patch, but would prefer to first have an ITK maintainer
approve the concept and warn me about any non-obvious gotchas or
requirements.  Adding something like ParsePrintedHelp() (or
ParseHandledHelpOption()? ) allows the program to know whether the
command-line was incomplete and separately to know whether -v/-h was
used, so the program knows why it is (should be) stopping and can print
appropriate messages and return an appropriate status.

Fred Wheeler



More information about the Insight-developers mailing list