Command line processing. More...
Command line processing.
Provides a quick and easy way to deal with the arguments coming into an application from the command line. Several useful methods for parsing the command line tokens and generation of detailed usage information are provided for the application.
Command line tokens are separated into three kinds. The zeroth kind is the first command line token which is the name of the program as it was invoked on the command line. This token is stripped of any absolute path information and stored. The first kind are options. Any command line token which begins with a "-" is interpreted as an option. There are several types of options:
Option usage is summarized by the following table.
Type | Description | Usage | Value |
---|---|---|---|
0 | simple flag | -t | .true. |
1 | argument optional | -t or -t [arg] | .true. or [arg] |
2 | argument required | -t <arg> | <arg> |
3 | required w/arg | -t <arg> | <arg> |
The second kind of command line tokens are arguments to the application. These kind of tokens are simply all the tokens not used up in the processing of the zeroth and first kinds of command line tokens. A mechanism for specifying whether these kind of tokens are required or optional is also provided.
Definition at line 411 of file AppTools.H.
#include <AppTools.H>
Public Member Functions | |
comlinehandler () | |
Default constructor. More... | |
comlinehandler (const char *args[]) | |
Constructor. More... | |
comlinehandler (comlinehandler &incom) | |
Copy constructor. More... | |
void | Copy (comlinehandler &incom) |
Copy method. More... | |
int | ProcessCommandLine (const char *args[]) |
One fell swoop processing of command line. More... | |
std::string | GetRawComLine () const |
Raw Command Line Access. More... | |
void | SetRawComLine (const std::string &incl) |
Raw Command Line Access. More... | |
int | ProcessOptions () |
Processes all command line tokens. More... | |
void | Record (const char *args[]) |
Minimal recording of command line. More... | |
void | AddOption (char s, const std::string &l, int=0) |
User interface to describe simple option. More... | |
void | AddOption (char s, const std::string &l, int, const std::string argname) |
User interface to describe option with argument. More... | |
void | AddArgument (const std::string &a, int reqd=0) |
User interface to describe an application argument. More... | |
void | AddArgHelp (const std::string &a, const std::string &help) |
Specify the usage info for application argument. More... | |
void | SetArgName (const std::string opstr, const std::string argname) |
Specify name of an option argument. More... | |
void | SetArgName (char s, const std::string &argname) |
Specify name of an option argument. More... | |
void | AddHelp (char s, const std::string &help) |
Specify usage for an option. More... | |
void | AddHelp (const std::string &l, const std::string &help) |
Specify usage for an option. More... | |
std::string | GetOpStringByType (int mintype, int maxtype) |
Obtains option strings by type. More... | |
std::string | GetHelp (char s) |
Obtain the usage string for an option. More... | |
std::string | GetHelp (const std::string &l) |
Obtain the usage string for an option. More... | |
char | GetShort (const std::string &l) |
Obtain the short one char option from the long word version. More... | |
std::string | GetLong (const char &s) |
Obtain the long word option from the short char version. More... | |
std::string | ShortUsage () |
Generate short usage string. More... | |
std::string | LongUsage () |
Generate long usage string. More... | |
std::string | GetOption (const char &s) |
Get the value of an option. More... | |
std::string | GetOption (const std::string &l) |
Get the value of an option. More... | |
std::string | ErrorReport () |
Error reporting. More... | |
std::string | ProgramName () const |
Program name access. More... | |
std::vector< std::string > | GetArgs () const |
Argument access. More... | |
void | SetDescription (const std::string &desc) |
Set description string. More... | |
void | SetNotes (const std::string ¬es) |
Set notes string. More... | |
void | WriteRC (std::ostream &Ostr) const |
Write an RC string that can be used for config. More... | |
void | ReadRC (const std::string &RCString) |
Read a config from RC string. More... | |
template<typename NumType > | |
void | ProcessRange (NumType &t1, NumType &t2, const std::string stinter) |
Process a range in the format "t1:t2". More... | |
template<typename NumType > | |
void | ResolveOption (NumType &t2, const std::string stinter) |
Resolve an option (i.e. More... | |
virtual void | Initialize (void) |
virtual function for program specific Initialization. More... | |
virtual void | UserInitialize (void) |
virtual function for program specific Initialization. More... | |
virtual | ~comlinehandler () |
Protected Attributes | |
std::string | _description |
application description. More... | |
std::string | _notes |
Notes to be displayed at the end of LongUsage(). More... | |
std::string | _program_name |
the name of the program More... | |
std::string | _line |
unformatted command line More... | |
std::vector< std::string > | _error_messages |
stores error messages More... | |
std::vector< std::string > | _nonops |
stores non-option arguments More... | |
std::map< char, std::string > | _options |
stores the value of each option More... | |
std::map< char, std::string > | _help |
stores the help string for each op More... | |
std::map< char, std::string > | _argname |
stores a name for arguments More... | |
std::map< char, int > | _type |
stores the type of option More... | |
std::vector< std::pair< std::string, int > > | _args |
application arguments; More... | |
std::map< std::string, std::string > | _arghelp |
help string for args More... | |
std::vector< std::string > | _toks |
commandline tokens More... | |
Friends | |
std::ostream & | operator<< (std::ostream &Out, const comlinehandler &cl) |
std::istream & | operator>> (std::istream &In, comlinehandler &cl) |
|
inline |
Default constructor.
Definition at line 451 of file AppTools.H.
|
inline |
Constructor.
Typically, args comes directly from argv. One must be careful about environments in which extra arguments are stored in argv. A good example is MPI. MPI_Init(&argc,&argv) should be invoked before any comlinehandler is constructed.
Definition at line 460 of file AppTools.H.
comlinehandler | ( | comlinehandler & | incom | ) |
Copy constructor.
Definition at line 853 of file AppTools.H.
|
inlinevirtual |
Definition at line 740 of file AppTools.H.
|
inline |
Specify the usage info for application argument.
This function is provided for the specification of the usage information for application arguments. This is called after the argument has been set by AddArgument(...). The string specified in "help" string is displayed in the output of LongUsage().
Definition at line 546 of file AppTools.H.
References a.
Referenced by SetupOptions().
|
inline |
User interface to describe an application argument.
This function is provided for the specification of an argument to the application (i.e. a non-option argument), and whether the argument is required for the application to function. The function arguments are the name of the argument, and an integer which is > 0 for required application arguments.
Definition at line 532 of file AppTools.H.
Referenced by SetupOptions().
|
inline |
Specify usage for an option.
Function is used to set the usage string for an option which is displayed in the output of LongUsage().
Definition at line 576 of file AppTools.H.
Referenced by SetupOptions().
|
inline |
Specify usage for an option.
Function is used to set the usage string for an option which is displayed in the output of LongUsage().
Definition at line 583 of file AppTools.H.
void AddOption | ( | char | s, |
const std::string & | l, | ||
int | atype = 0 |
||
) |
User interface to describe simple option.
This function is provided for the specification of a simple option. The arguments are the short, single letter option, the long "word" version, and the type of the option. If the option is of the type which may have or requires an argument itself, then the argument will be given the default name, "arg".
Definition at line 1025 of file AppTools.H.
Referenced by SetupOptions().
void AddOption | ( | char | s, |
const std::string & | l, | ||
int | type, | ||
const std::string | argname | ||
) |
User interface to describe option with argument.
This function is provided for the specification of an option along with a name for it's argument. Argument names are useful for the generation of usage information to the user. The arguments are the short, single letter option, the long "word" version, the type, and the argument name.
Definition at line 1036 of file AppTools.H.
void Copy | ( | comlinehandler & | incom | ) |
Copy method.
Definition at line 858 of file AppTools.H.
References comlinehandler::_arghelp, comlinehandler::_argname, comlinehandler::_args, comlinehandler::_description, comlinehandler::_error_messages, comlinehandler::_help, comlinehandler::_line, comlinehandler::_nonops, comlinehandler::_notes, comlinehandler::_options, comlinehandler::_program_name, and comlinehandler::_type.
std::string ErrorReport | ( | ) |
Error reporting.
Returns a report of the errors encountered in the ProcessOptions() function.
Definition at line 1203 of file AppTools.H.
References apptools::DumpContents().
Referenced by main().
|
inline |
Argument access.
Definition at line 663 of file AppTools.H.
Referenced by main().
|
inline |
Obtain the usage string for an option.
Definition at line 597 of file AppTools.H.
|
inline |
Obtain the usage string for an option.
Definition at line 601 of file AppTools.H.
std::string GetLong | ( | const char & | s | ) |
Obtain the long word option from the short char version.
Definition at line 951 of file AppTools.H.
std::string GetOpStringByType | ( | int | mintype, |
int | maxtype | ||
) |
Obtains option strings by type.
Internal utility function for building a string of option characters which are of a certain type.
Definition at line 962 of file AppTools.H.
|
inline |
Get the value of an option.
Returns the value of the specified option. If the option has not been set on the command line, then the value will be an empty string.
Definition at line 640 of file AppTools.H.
Referenced by main().
|
inline |
Get the value of an option.
Returns the value of the specified option. If the option has not been set on the command line, then the value will be an empty string.
Definition at line 648 of file AppTools.H.
|
inline |
Raw Command Line Access.
Definition at line 483 of file AppTools.H.
char GetShort | ( | const std::string & | l | ) |
Obtain the short one char option from the long word version.
Definition at line 1014 of file AppTools.H.
|
inlinevirtual |
virtual function for program specific Initialization.
Definition at line 734 of file AppTools.H.
std::string LongUsage | ( | ) |
Generate long usage string.
Generates a long usage string similar to what one would see in the typical UNIX man-like description. Each option is shown with it's short and long version, along with usage information for each option and argument if such information has been specified with AddHelp, or AddArgHelp, respectively. Note that the format of this usage string follows the convention that optional quantities are enclosed in []'s while required quantities are enclosed in <>'s.
Definition at line 918 of file AppTools.H.
Referenced by main().
|
inline |
One fell swoop processing of command line.
Definition at line 475 of file AppTools.H.
int ProcessOptions | ( | ) |
Processes all command line tokens.
This function examines all the command line tokens, sorts them into their respective types, and assigns their values. The number of errors encountered during processing is returned.
Definition at line 1060 of file AppTools.H.
Referenced by main().
|
inline |
|
inline |
Program name access.
Definition at line 659 of file AppTools.H.
void ReadRC | ( | const std::string & | RCString | ) |
Read a config from RC string.
Definition at line 1212 of file AppTools.H.
void Record | ( | const char * | args[] | ) |
Minimal recording of command line.
This function simply parses the command line to extract the executble name and record the full command line in a string.
Definition at line 1046 of file AppTools.H.
References apptools::stripdirs().
|
inline |
|
inline |
Specify name of an option argument.
Function is used to set the argument name for an option if the option can use an argument.
Definition at line 556 of file AppTools.H.
|
inline |
Specify name of an option argument.
Function is used to set the argument name for an option if the option can use an argument.
Definition at line 566 of file AppTools.H.
|
inline |
Set description string.
Definition at line 667 of file AppTools.H.
Referenced by SetupOptions().
|
inline |
Set notes string.
Definition at line 671 of file AppTools.H.
void SetRawComLine | ( | const std::string & | incl | ) |
Raw Command Line Access.
Definition at line 1192 of file AppTools.H.
std::string ShortUsage | ( | ) |
Generate short usage string.
Generates a short usage string similar to what one would see in the typical short command line usage output for a UNIX command. Note that the format of this usage string follows the convention that optional quantities are enclosed in []'s while required quantities are enclosed in <>'s.
Definition at line 974 of file AppTools.H.
Referenced by main().
|
inlinevirtual |
virtual function for program specific Initialization.
Definition at line 738 of file AppTools.H.
void WriteRC | ( | std::ostream & | Ostr | ) | const |
Write an RC string that can be used for config.
Definition at line 1211 of file AppTools.H.
|
friend |
Definition at line 816 of file AppTools.H.
|
friend |
Definition at line 744 of file AppTools.H.
|
protected |
help string for args
Definition at line 444 of file AppTools.H.
Referenced by comlinehandler::Copy(), apptools::operator<<(), and apptools::operator>>().
|
protected |
stores a name for arguments
Definition at line 438 of file AppTools.H.
Referenced by comlinehandler::Copy(), apptools::operator<<(), and apptools::operator>>().
|
protected |
application arguments;
Definition at line 442 of file AppTools.H.
Referenced by comlinehandler::Copy(), and apptools::operator<<().
|
protected |
application description.
This description will be dumped as the first section of the output of LongUsage().
Definition at line 422 of file AppTools.H.
Referenced by comlinehandler::Copy(), apptools::operator<<(), and apptools::operator>>().
|
protected |
stores error messages
Definition at line 430 of file AppTools.H.
Referenced by comlinehandler::Copy(), and apptools::operator>>().
|
protected |
stores the help string for each op
Definition at line 436 of file AppTools.H.
Referenced by comlinehandler::Copy(), apptools::operator<<(), and apptools::operator>>().
|
protected |
unformatted command line
Definition at line 428 of file AppTools.H.
Referenced by comlinehandler::Copy().
|
protected |
stores non-option arguments
Definition at line 432 of file AppTools.H.
Referenced by comlinehandler::Copy().
|
protected |
Notes to be displayed at the end of LongUsage().
Definition at line 424 of file AppTools.H.
Referenced by comlinehandler::Copy().
|
protected |
stores the value of each option
Definition at line 434 of file AppTools.H.
Referenced by comlinehandler::Copy().
|
protected |
the name of the program
Definition at line 426 of file AppTools.H.
Referenced by comlinehandler::Copy().
|
protected |
commandline tokens
Definition at line 446 of file AppTools.H.
|
protected |
stores the type of option
Definition at line 440 of file AppTools.H.
Referenced by comlinehandler::Copy(), apptools::operator<<(), and apptools::operator>>().