#include <string>
#include <sstream>
#include <list>
#include <limits>
#include <map>
#include <cstring>
#include "CmdLineException.h"
Go to the source code of this file.
Namespaces | |
namespace | CmdLine |
Classes | |
class | CmdLine::Option< T > |
class | CmdLine::OptionsType< T > |
class | CmdLine::OptionsINT |
class | CmdLine::OptionsFLOAT |
class | CmdLine::OptionsBOOL |
class | CmdLine::OptionsCHAR |
class | CmdLine::OptionsSTRING |
class | CmdLine::Parser |
Typedefs | |
typedef long double | CmdLine::FLOAT |
typedef long | CmdLine::INT |
Enumerations | |
enum | { CmdLine::NONE = 0, CmdLine::SILENT = 1, CmdLine::OUT_OF_RANGE = 2, CmdLine::NO_VALUE = 4, CmdLine::DUPLICATE = 8, CmdLine::FIRSTONLY = 16 } |
Example:
int main( int argc, char** argv ) { CmdLine::Parser Opts( argc, argv ); // declaring a boolean option with an alias (optional) Opts.Bool.Add( "-h", "--help" ); // an integer option (without alias) with default, min and max values Opts.Int.Add( "-n","",5, 0, 10 ); // a float (float/double/long double) option Opts.Float.Add( "--float", "--float-option" ,1.0, -10.0, 10.0 ); // a char option that specifies a set of valid characters Opts.Char.Add( "--letter", "", "A", "abcABC" ); // a string option Opts.String.Add( "-s", "--string-option", "default string" ); // another string option, but it specifies a range of values Opts.String.Add( "--rgb-color", "", "red", "red", "green" ,"blue", NULL ); // ... // it will contain the unrecognized options/arguments vector<const char*> remains; // processing the command-line Opts.Process( remains ); // getting the results! if (Opts.Bool.Get("-h")) cout << "-h is set" << endl; if (!Opts.Int.Found("-n")) cout << "-n not declared, using default." << endl; int integer = Opts.Int.Get("-n"); cout << "Value for '-n': " << integer << " As short int: " << Opts.Int.Get<short int>("-n") << endl; cout << "Value for '-s': " << Opts.String.Get("-s") << endl; // ... cout << "\nUnrecognized arguments:" << endl; list<const char*>::const_iterator ci = remains.begin(); while (ci != remains.end()) cout << *ci++ << endl; }
For the code above, for example:
$ ./a.out -h --string-option "abc" item1 item2 -h is set -n not declared, using default. Value for '-n': 5 Value for '-s': abc Unrecognized arguments: item1 item2
Other features: