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

What GetOpt_pp can parse

GetOpt_pp parses the options passed to the program. Each option can have a list of arguments. Options can be specified in two ways: with a letter (´short option format´), or with a word (´long option format´). For example:

    -h               // the short option format
    --help           // the long option format

In terms of GetOpt_pp, the long option format is optional, whereas the short option is always required for identifying the option.

You can do the following operations:

  • get the arguments of an option, if specified
  • just test whether an option was specified or not.

You can do such operations by using manipulators (Option and OptionPresent respectively), iterators, and more (refer to the table of content, or keep reading).

A more complete example:

./program -a 1 2 3 -b 4 5 6 --word 7 8 9
means that program was invoked with the option '-a' (with arguments 1 2 3), option 'b' (with arguments 4 5 6), and option '--word' (with arguments 7 8 9). Short options can be any letter, long options can be any word, and arguments can be alphanumeric strings.

How to use GetOpt_pp

The GetOpt_pp class can be queried using the following manipulators:

  • Option
  • OptionPresent

Usage of manipulators can be done in two ways, just as any regular STL stream:

  • without exceptions
  • with exceptions (explained later)

Additionally, the GetOpt_pp class can be queried without using manipulators. See the Querying without Manipulators section for more details.

Finally, the GetOpt_pp::app_name() method can be invoked to retrieve the name of the application (that is, the first argument passed to main()).

"Option" Manipulator

The Option manipulator receives the following arguments:

  1. the short option char (optional)
  2. the long option string (optional)
  3. the reference where it will store the value, on success
  4. the default value (optional)

Either one or the two short/long options can be specified (at least one of them is mandatory). The way to distinguish between short and long options, is that short options are single characters (specified with single quotes, such as 'a'), whereas long options are strings (specified with double quotes, such as "hello").

Example:

    int myvar;
    GetOpt_pp ops(argc, argv);

    ops >> Option('t', "test", myvar);

The variable can have a previous value. If the operation does not succeeds (either because the format is invalid, or the option was not provided), the value of the variable is preserved.

You can check if succeeded by casting to bool, as regular streams. The same example as above would look like:

    int myvar;
    GetOpt_pp ops(argc, argv);

    if (ops >> Option('t', "test", myvar))
         ...
    else
         cerr << "Error in option 't'" << endl;

"GlobalOption" Manipulator

This applies to version 2.15. The GlobalOption manipulator is used to retrieve arguments which were not preceded by an option. For example, the invocation ./myprogram one two three does not specify any option. In the example below, the list "one two three" would be retrieved in args by doing:

    std::vector<std::string> args;
    GetOpt_pp ops(argc, argv);
    ops >> GlobalOption(args);

"OptionPresent" Manipulator

The way to check whether an option (a flag) was set or no, is using the OptionPresent manipulator. This manipulator never throws exceptions, and has the same signature as the Option manipulator (being the last argument the target bool flag). Example

     bool flag;
     ops >> OptionPresent('x', flag);

Another way is not assigning it to a variable, but checking the >> result:

     if (ops >> OptionPresent('h', "help"))
         show_help();

//

Next: Using Exceptions

Back to Table of Contents

Comment by cesar.ro...@gmail.com, Oct 31, 2009

how do i access all the arguments from either of the options in the example? whenever I try something like -p 1 2 3 I get a TooManyArgumentsEx?

Comment by danielgutson@gmail.com, Oct 31, 2009
Comment by karasevpa@gmail.com, Feb 15, 2010

Very nice, but some suggestions/comments that I have:

1. is it possible to make short flags optional? Becomes awkward when the 'nice' letters run out

2. Not sure if there is regard to operation on windows. It seems to trigger an obscure bug in MSVC: when building in 'debug' and windows libraries, having:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

triggers a compiler error; there is a conflict in memory mapping with MS's "new" operator

3. More character types would be nice to support natively... here is my current hack to use the TCHAR args in a windows command line program:

// Hack: Dump 16-bit unicode  strings into 8-bit so 
// that the parser function works
        char** argv_ascii = new char*[argc];
        unsigned int convertedBytes;
        unsigned int maxBytes = 1024;
        int theSize;
        for( int k = 0; k < argc; k++ ) {
            wchar_t* wstr = argv[k];
            theSize = wcslen(wstr) + 1;
            char* ascii = new char[theSize];
           // wcstombs( ascii, wstr, theSize-1);
            wcstombs_s( &convertedBytes, ascii, theSize, wstr, maxBytes );
            argv_ascii[k] = ascii;
        }
        GetOpt_pp ops(argc, argv_ascii); // create parser object
Comment by danielgutson@gmail.com, Jun 30, 2010

Hi karasevpa, thanks for your comments. Could you please fill the issues in the issue tracker? The first one is an enhancement request, the second is something I can't reproduce on Linux, so please fill a report as complete as possible; the third would be also an enhancement request too. Sorry the late response. Daniel.


Sign in to add a comment
Powered by Google Project Hosting