Options::GetValue( char opt, int subopt )

Returns the value of a flag previously stored by Options::Parse().

Virtual?

No

 

Class

Options

 

Arguments

char opt

The flag to check

 

int subopt

Return the argument associated with the subopt-th occurrence of the opt flag on the command line.

Returns

StrPtr *

The value of the flag. This is “true” for flags which, when provided, do not take a value, and NULL if the flag is not provided

Notes

You must call Options::Parse() before calling GetValue().

If a flag does not occur on the command line, GetValue() returns NULL.

If a flag is provided without a value, GetValue() returns “true”.

If a flag appears only once on a command line, extract the value of its arguments by calling GetValue() with a subopt of zero, or use the [] operator.

If a flag occurs more than once on a command line, extract the value supplied with each occurrence by calling Options::GetValue() once for each occurrence, using different subopt values.

See also

Options::Parse() Options::operator[]

Example

Executing the following code produces the following output:

$ getvalue -h -c1 -c2 -d3

opts.GetValue( h, 0 ) value is true
opts.GetValue( c, 0 ) value is 1
opts.GetValue( c, 1 ) value is 2
opts.GetValue( d, 0 ) value is 3
#include <stdhdrs.h>
#include <strbuf.h>
#include <error.h>
#include <options.h>

int main( int argc, char **argv )
{
    // Parse options.
    Error *e = new Error();
    ErrorId usage = { E_FAILED, "Usage: getvalue -h for usage." };

    Options opts;

    // strip out the program name before parsing
    argc--;
    argv;

    char *ParseOpts = "ha:b:c:d:e:f:";
    opts.Parse( argc, argv, ParseOpts, OPT_ANY, usage, e );

    if ( e-&gt;Test() )
    {
        StrBuf msg;
        e-&gt;Fmt( &amp;msg );   // See Error::Fmt()
        printf( "ERROR:\n%s", msg.Text() );
        return 1;
    }

    char *iParseOpts = ParseOpts;
    int isubopt;
    StrPtr *s;

    // Print values for options.
    while( *iParseOpts != '\0' )
    {
        if ( *iParseOpts != ':' )
        {
            isubopt = 0;
            while( s = opts.GetValue( *iParseOpts, isubopt ) )
            {
                printf( "opts.GetValue( %c, %d ) value is %s\n",
                        *iParseOpts, isubopt, s-&gt;Text() );
                isubopt;
            }
        }
        iParseOpts++;
    }
    return 0;
}