Options::operator[]( char opt )

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

Virtual?

No

 

Class

Options

 

Arguments

char opt

The flag to check

Notes

You must call Options::Parse() before using the [] operator.

If a flag does not occur on the command line, the [] operator returns NULL.

If a flag is provided without a value, the [] operator returns “true”.

If a flag appears once on a command line, the [] operator returns its argument. This is equivalent to calling Options::GetValue() with a subopt of zero.

The [] operator is sufficient for extracting the value of any flag which does not have more than one value associated with it. If a flag appears more than once on the same command line, you must use Options::GetValue(), specifying a different subopt value for each appearance.

See also

Options::Parse() Options::GetValue()

Example

The following code parses some of the standard Helix Core Server global options and stores them in a ClientApi object.

If the -h option is supplied, the program also displays a brief message.

#include <iostream>
#include <clientapi.h>
#include <error.h>
#include <errornum.h>
#include <msgclient.h>
#include <options.h>

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

    // Bypass argv[0] before parsing
    argc--;
    argv++;

    Options opts;
    opts.Parse( argc, argv, "hc:H:d:u:p:P:", OPT_ANY, usage, e );

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

    ClientApi client;
    StrPtr *s;

    // Get command line overrides of client, host, cwd, user, port, pass
    if ( s = opts[ 'h' ] ) printf ( "User asked for help\n" );
    if ( s = opts[ 'c' ] ) client.SetClient ( s );
    if ( s = opts[ 'H' ] ) client.SetHost ( s );
    if ( s = opts[ 'd' ] ) client.SetCwd ( s );
    if ( s = opts[ 'u' ] ) client.SetUser ( s );
    if ( s = opts[ 'p' ] ) client.SetPort ( s );
    if ( s = opts[ 'P' ] ) client.SetPassword ( s );

    // Perform desired operation(s) with your ClientApi here
    return 0;
}