Public Methods | |
| CommandLineManager (const std::string &title, const std::string &version, const std::string ©right, const std::string &usage, int &argc, char **argv, bool fail=true) | |
| Create a CommandLine parser. | |
| void | help (std::ostream &os) const |
| Print the help message. | |
| void | version (std::ostream &os) const |
| Print the version information. | |
| bool | wantHelp () const |
| Has the user required help on this application? | |
| bool | wantVersion () const |
| Has the user required version information on this application? | |
| void | process () |
| Process the command line. | |
| size_t | size () const |
| The number of registered options. | |
Friends | |
| class | CommandLineOptionBase |
Command line arguments (argc, argv) can be analysed using this manager class together with the helper classes CommandLineOption and CommandLineFlag. It can handle complex combinaison of command line options and flags. Options can be given to the parser in any order, and it can recognise both "options" (i.e. command line flags which require a value):
-x short option with no value-x VALUE short option with value--xopt long option with no value--xopt=VALUE long option with valueor command line "flags" (i.e. command line flags which can only be "on" or "off"):
-x short option, set the flag to 'true'-x- short option, set the flag to 'false'--xopt long option, set the flag to 'true'--xopt- long option, set the flag to 'false'After the parsing, the (argc, argv) couple will be modified so that only the unrecognised options remain. For instance if your program needs options and files, CommandLineManager will process the options and you will get only your file names left in the (argc, argv) couple.
Example:
#include "sword/sword.h" #include "sword/CommandLineManager.h" using namespace sword; int main(int argc, char *argv[]) { // Create the manager CommandLineManager clm( "Foo", "1.0", "Copyright (C) ACME Corporation", "[OPTIONS] FILES", argc, argv); // Create the options and flags to recognize CommandLineOption<std::string> cl_o(clm, 'o', "output", "Output File Name", false); CommandLineOption<int> cl_d(clm, 'd', "debug", "Debug Level", false, 1); CommandLineFlag <bool> cl_v(clm, 'v', "verbose", "Switch to Verbose Level", false); // go and process the command line clm.process(); // display the help if it was required if (clm.wantHelp()) { clm.help(std::cout); return 1; } if (clm.wantVersion()) { clm.version(std::cout); return 1; } // check some more conditions if (!cl_o.isSet()) { std::cout << "Missing '-o' argument, try --help"; return 1; } if (argc == 1) { std::cout << "Missing files arguments, try --help"; return 1; } // use the results std::cout << "Output file is : " << cl_o.value() << std::endl; std::cout << "Debug level is : " << cl_d.value() << std::endl; std::cout << "Verbose is : " << (cl_v.value() ? "on" : "off") << std::endl; std::cout << "Files are : "; for(int i=1; i<argc; ++i) std::cout << argv[i] << ", "; std::cout << std::endl; return 0; }
|
||||||||||||||||||||||||||||||||
|
Create a CommandLine parser. Initialize the command line parser with enough information to display proper help on the command line as well as the actual command line arguments to parse.
|
|
|
Print the help message.
|
|
|
Process the command line.
|
|
|
The number of registered options.
|
|
|
Print the version information.
|
|
|
Has the user required help on this application?
|
|
|
Has the user required version information on this application?
|
1.3-rc2