FBB::ArgConfig(3bobcat)

Program Arguments
(libbobcat-dev_4.08.03-x.tar.gz)

2005-2018

NAME

FBB::ArgConfig - A singleton class processing program arguments

SYNOPSIS

#include <bobcat/argconfig>
Linking option: -lbobcat

DESCRIPTION

Singleton class (see Gamma et al., 1995) built around getopt(3) and getopt_long(3). The class handles short- and long command-line options as well as configuration files.

In addition to the standard command-line options, configuration files containing long options as their keys, optionally followed by a colon are also recognized by the various option members. E.g., an option --input filename can be specified in the configuration file like


input: filename
    
or

input filename
    
Options without arguments should probably not use the colon, although it is accepted by ConfigArg. E.g., for the option --verbose both forms are OK:

verbose
verbose:
    

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

FBB::Arg,
FBB::ConfigFile

CONSTRUCTORS

Since the class is a Singleton, no public constructors are available. Instead, static members are offered to initialize and access the single ArgConfig object. See below.

STATIC MEMBERS

NON-STATIC MEMBER FUNCTIONS

All public members of the Arg and ConfigFile classes are also offered by the ArgConfig class. As several option members were reimplemented by this class all option members are discussed below. All other members inherit straight from the classes Arg and ConfigFile. Consult their man pages for details.

EXAMPLE


#include <iostream>
#include <string>

#include <bobcat/argconfig>
#include <bobcat/exception>

using namespace std;
using namespace FBB;

ArgConfig::LongOption lo[] =
{
    ArgConfig::LongOption{"option", 'o'},
    ArgConfig::LongOption{"value-option", 'v'}
};

class X
{
    ArgConfig &d_arg;

    public:
        X();
        void function();
};

X::X()
:
    d_arg(ArgConfig::instance())
{}

void X::function()
{
    if (d_arg.nArgs() == 0)
        throw Exception() << "Provide the name of a config file as 1st arg";

    cout << "Counting " << d_arg.option('o') << " instances of -o or "
                                                            "--option\n";

    d_arg.open(d_arg[0]);       // Now open the config file explicitly
                            // (alternatively: use a constructor expecting 
                            // a file name)

    cout << "Counting " << d_arg.option('o') << " instances of -o or "
                                                            "--option\n";

    string optval;
    size_t count = d_arg.option(&optval, 'v');

    cout << "Counting " << count << 
                        " instances of -v or --value-option\n";
    if (count)
        cout << "Option value = " << optval << endl;
}

int main(int argc, char **argv)
try
{
    ArgConfig::initialize("ov:", lo, lo + 2, argc, argv);

    X x;
    x.function();

    return 0;
}
catch (Exception const &err)
{
    cout << "Terminating " << err.what() << endl;
    return 1;
}
    

FILES

bobcat/argconfig - defines the class interface

SEE ALSO

arg(3bobcat), configfile(3obcat), bobcat(7)

BUGS

None Reported.

DISTRIBUTION FILES

BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).