My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Introduction  
GetOpt_pp intro.
Updated Nov 14, 2011 by hugo.arregui

Construct a GetOpt_pp object by passing the argc and argv params to its constructor.

The GetOpt_pp object will behave as an istream. The data can be extracted using some manipulators, the simplest being Option.

int main(int argc, char* argv[])
{
    GetOpt_pp args(argc, argv);
    int ret;

    args >> Option('i', "long_option", ret);

    return ret;
}

In the example above, either the option '-i' (short form) or "--long_option" (long form) is read from the arguments. That means that the program can be invoked in any of the following forms:

  • program -i number
  • program --long_option number
and that number will be stored in ret.

Quick Notes:

  • All the symbols are defined within the GetOpt namespace. Add a using namespace GetOpt; for making all visible. Examples in this documentation assume that line is present.
  • Include getopt_pp.h, and link getopt_pp.cpp
  • For example: g++ example_helloworld.cpp getopt_pp.cpp -o example_helloworld
  • Some people don't like to link new cpp files or create libraries; if that's you, just include getopt_pp_standalone.h without any .cpp from only one of your source files.

//

Next: Basic Usage

Back to Table Of Contents

Comment by rnav...@gmail.com, Oct 28, 2009

getopt_pp.h, applied following patch to avoid a compilation warning

111c111 < : short_opt(other.short_opt), long_opt(other.long_opt), target(other.target) --- > : Option( other ), short_opt(other.short_opt), long_opt(other.long_opt), target(other.target)

Comment by danielgutson@gmail.com, Jun 30, 2010

Thanks, what compiler did you use? g++ 4.4.1 doesn't complain. Anyway, I'm adding a similar change in order to suppress such warning, whatever compiler it is. (I invoke the default constructor of the base class, rather than the copy ctor). Please test the committed version in the repository.


Sign in to add a comment
Powered by Google Project Hosting