VCF::CommandLine Class Reference
A utility for parsing command lines. More...
#include <vcf/FoundationKit/CommandLine.h>
Inheritance diagram for VCF::CommandLine:

Public Member Functions | |
| CommandLine () | |
| virtual | ~CommandLine () |
| int | splitLine (int argc, char **argv) |
| parse the command line into switches and arguments. | |
| int | splitLine (const String &commandLine) |
| int | splitLine (const std::vector< String > &commandLine) |
| bool | hasSwitch (const String &aSwitch) const |
| was the switch found on the command line ? | |
| String | getSafeArgument (const String &aSwitch, size_t iIdx, const String &aDefault) const |
| fetch an argument associated with a switch . | |
| String | getArgument (const String &aSwitch, size_t iIdx) const |
| fetch a argument associated with a switch. | |
| String | getArgument (size_t index) const |
| int | getArgumentCount (const String &aSwitch) const |
| Enumerator< String > * | getOriginalCommands () |
| size_t | getArgCount () const |
| const std::vector< String > & | getOriginalCommandLine () const |
Protected Types | |
| typedef std::map< String, CmdParam > | CommandLineMap |
Protected Member Functions | |
| bool | isSwitch (const String ¶m) const |
| protected member function test a parameter to see if it's a switch : switches are of the form : -x where 'x' is one or more characters. | |
Protected Attributes | |
| CommandLineMap | commandLine_ |
| std::vector< String > | originalCommandLine_ |
| EnumeratorContainer< std::vector< String >, String > | commandLineContainer_ |
Detailed Description
A utility for parsing command lines.This was originally written by Chris Losinger, changes were simply removing the inheritance from std::map<>, and adding a map as a member variable instead. Also made some cosmetic name changes to conform better with VCF naming standards.
Example :
Our example application uses a command line that has two required switches and two optional switches. The app should abort if the required switches are not present and continue with default values if the optional switches are not present.
Sample command line : MyApp.exe -p1 text1 text2 -p2 "this is a big argument" -opt1 -55 -opt2
Switches -p1 and -p2 are required. p1 has two arguments and p2 has one.
Switches -opt1 and -opt2 are optional. opt1 requires a numeric argument. opt2 has no arguments.
Also, assume that the app displays a 'help' screen if the '-h' switch is present on the command line.
#include "CmdLine.h" void main(int argc, char **argv) { // our cmd line parser object CommandLine cmdLine; // parse argc,argv if (cmdLine.splitLine(argc, argv) < 1) { // no switches were given on the command line, abort ASSERT(0); exit(-1); } // test for the 'help' case if (cmdLine.HasSwitch("-h")) { show_help(); exit(0); } // get the required arguments String p1_1, p1_2, p2_1; try { // if any of these fail, we'll end up in the catch() block p1_1 = cmdLine.getArgument("-p1", 0); p1_2 = cmdLine.getArgument("-p1", 1); p2_1 = cmdLine.getArgument("-p2", 0); } catch (...) { // one of the required arguments was missing, abort ASSERT(0); exit(-1); } // get the optional parameters // convert to an int, default to '100' int iOpt1Val = atoi(cmdLine.getSafeArgument("-opt1", 0, 100)); // since opt2 has no arguments, just test for the presence of // the '-opt2' switch bool bOptVal2 = cmdLine.hasSwitch("-opt2"); .... and so on.... }
- Version:
- 1.0 Chris Losinger
2.0 Jim Crafton
Member Typedef Documentation
|
|
|
Constructor & Destructor Documentation
|
|
|
|
|
|
Member Function Documentation
|
|
|
|
|
|
|
||||||||||||
|
fetch a argument associated with a switch. throws an exception of (int)0, if the parameter at index iIdx is not found. example : |
|
|
|
|
|
|
|
|
|
|
||||||||||||||||
|
fetch an argument associated with a switch . if the parameter at index iIdx is not found, this will return the default that you provide. example : |
|
|
was the switch found on the command line ?
ex. if the command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5 call return ---- ------ cmdLine.hasSwitch("-a") true cmdLine.hasSwitch("-z") false
|
|
|
protected member function test a parameter to see if it's a switch : switches are of the form : -x where 'x' is one or more characters. the first character of a switch must be non-numeric! |
|
|
|
|
|
|
|
||||||||||||
|
parse the command line into switches and arguments.
|
Member Data Documentation
|
|
|
|
|
|
|
|
|
The documentation for this class was generated from the following file:
- vcf/FoundationKit/CommandLine.h
