|
OpenTop 1.5 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||||
#include "ot/auxil/CommandLineParser.h"
A POSIX-compliant command line parser.
C/C++ programs normally receive the command line from the operating system's shell as an array of null-delimited strings passed in the argv argument to the main() function. The passed array of command tokens comprises the name of the executable, option flags and file names or other positional parameters. The following is a typical command line which needs to be interpreted (or parsed) by the receiving program:- $ xmlvalid -vc --summary test1.xml test2.xml
This class implements a command line parsing engine which recognises the rich command line syntax specified for POSIX commands (see below). An object-oriented approach is adopted, where each permissible option supported by the application is represented by an object that implements the CommandLineOption interface.
Most of the POSIX command parsing logic is implemented by this class, but the task of recognising individual option names as well as remembering if and how an option has been specified is delegated to an application-supplied set of objects that implement the CommandLineOption interface. OpenTop provides several concrete implementatons of CommandLineOption which cover most of the common (and not so common) command option types.
Applications typically use this class by first creating an instance on the stack and then registering one of more CommandLineOption objects. The parse() method is then called, passing in the argv parameter from function main(). Here is a typical example:-
int main(int argc, char* argv[])
{
MemCheckSystemMonitor monitor;
// Set up a command line parser to recognise our following options:
// -h takes no argument
// -p takes a mandatory argument
// -r is a bool, which takes no argument and defaults to false
BasicOption optHelp(OT_T("help"), 'h', BasicOption::none);
BasicOption optPort(OT_T("port"), 'p', BasicOption::mandatory);
BooleanOption optReply(OT_T("reply"), 'r', false);
CommandLineParser cmdlineParser;
cmdlineParser.addOption(&optHelp);
cmdlineParser.addOption(&optPort);
cmdlineParser.addOption(&optReply);
// ...parse the command line, placing the command options
// into their respective objects.
try
{
cmdlineParser.parse(argc, argv);
}
catch (CommandLineException& e)
{
cerr << cmdlineParser.getProgramName() << ": " << e.getMessage() << endl << endl;
return (1);
}
// If the user has request help, then provide some
if(optHelp.isPresent())
{
showUsage();
return (0);
}
// and now do something really useful...
}
program-name [OPTION FLAG(S)...] [FILE(S)...]
Broadly speaking there are two ways of specifying command line options: using either the long or the short method. Long options start with a sequence of two dashes -- followed by the option name. Short options start with a single dash - (or alternatively a forward slash / in Microsoft Windows environments).
Long option names may be abreviated on the command line provided the abreviated name is unambiguous. For example, the long option named 'port' may be expressed as --port or --p so long as there aren't any other long option names beginning with p.
Short option names obviously cannot be abreviated, but they may be concatenated. For example, if a command accepts both an x and a y option, they may be expressed together as -x -y or, more succinctly, as -xy. The exception to this is when the option takes an argument. When a short option takes an argument, the text immediately following the option is taken as the argument. The argument may be concatenated with the short option or separated by white space (e.g. one or more spaces or tab characters).
Long options may also take arguments, but the difference here is that the argument must be separated from the option name by white space.
| Constructor/Destructor Summary | |
CommandLineParser()Constructs a CommandLineParser. | |
| Method Summary | |
void |
addOption(CommandLineOption* pOption)Adds a CommandLineOption to the list of options which will be recognised during a parse() operation. |
StringList |
getFilenames(int argc, char* argv, int firstArg, int lastArg)Returns a list of filenames passed as command line argument(s) to the currently running program. |
unsigned |
getFirstPositionalArg() constFollowing a successful parse, return the index of the argument that represents the first non-option (i.e. |
protected CommandLineOption& |
getLongOption(const String& option, CommandLineOption::ArgumentType& ) constLocates a CommandLineOption that answers to the long option passed. |
const String& |
getProgramName() constReturns the program name, which is determined from the first argument passed to the parse() method. |
protected CommandLineOption& |
getShortOption(char option, CommandLineOption::ArgumentType& ) constLocates a CommandLineOption that answers to the short option passed. |
unsigned |
parse(int argc, char* argv)Parses the passed command line parameters, extracting any option names and associated arguments. |
| Typedefs |
typedef std::list< String > StringList
| Constructor/Destructor Detail |
CommandLineParser()
| Method Detail |
void addOption(CommandLineOption* pOption)
pOption - StringList getFilenames(int argc,
char* argv,
int firstArg,
int lastArg)
The UNIX shell is more powerful than the Windows command prompt. One of the advanced functions it performs automatically is filename wildcard expansion (also known as filename 'globbing'). This feature allows users to enter a command like the following,
dir *
When running under Windows this function performs filename globbing so that the command can be used from either a DOS/Windows or UNIX shell using the same syntax.
Under UNIX this is a very simple wrapper. Under Win32, each positional argument is tested for wildcard characters and expanded into a list of matching filenames.
unsigned getFirstPositionalArg() const
protected CommandLineOption& getLongOption(const String& option,
CommandLineOption::ArgumentType& ) const
CommandLineException - const String& getProgramName() const
protected CommandLineOption& getShortOption(char option,
CommandLineOption::ArgumentType& ) const
CommandLineException - unsigned parse(int argc,
char* argv)
Parsing stops as soon as a non-option argument is found.
argc - argv - CommandLineException -
|
OpenTop 1.5 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||||