|
Core Library
|
A command line parser. More...
#include <CmdLineParser.hpp>
Classes | |
| struct | IsLongName |
| The predicate for matching the switches long name. More... | |
| struct | IsSeparator |
| The predicate for finding the name/value separator. More... | |
| struct | IsShortName |
| The predicate for matching the switches short name. More... | |
Public Types | |
| enum | Format { WINDOWS = 0x0001, UNIX = 0x0002 } |
| The switch formats. More... | |
| enum | Flag { ALLOW_WIN_FORMAT = WINDOWS, ALLOW_UNIX_FORMAT = UNIX, ALLOW_UNNAMED = 0x0004, ALLOW_ANY_FORMAT = (ALLOW_WIN_FORMAT | ALLOW_UNIX_FORMAT), DEFAULTS = ALLOW_ANY_FORMAT | ALLOW_UNNAMED } |
| The parsing flags. More... | |
| typedef const CmdLineSwitch * | SwitchCIter |
| An iterator into command line switch table. | |
| typedef std::vector< tstring > | StringVector |
| A vector of strings. | |
| typedef std::map< int, StringVector > | NamedArgs |
| The type used to store the named arguments. | |
| typedef StringVector | UnnamedArgs |
| The type used to store the unnamed arguments. | |
Public Member Functions | |
| CmdLineParser (SwitchCIter itFirstSwitch, SwitchCIter itLastSwitch) | |
| Default constructor. | |
| ~CmdLineParser () | |
| Destructor. | |
| const NamedArgs & | getNamedArgs () const |
| Get the collection of named arguments. | |
| const UnnamedArgs & | getUnnamedArgs () const |
| Get the collection of unnamed arguments. | |
| void | parse (int argc, tchar *argv[], int nFlags=DEFAULTS) |
| Parse the command line. | |
| bool | isSwitchSet (int nID) const |
| Check if a switch was provided. | |
| tstring | getSwitchValue (int nID) const |
| Get the value for a switch. | |
| tstring | formatSwitches (Format eFormat) const |
| Generate the list of switches for a usage message. | |
Private Types | |
| typedef const tchar * | CharCIter |
| An iterator for c-style strings. | |
Private Member Functions | |
| void | reset () |
| Reset the internal state. | |
| SwitchCIter | findSwitch (CharCIter itNameFirst, CharCIter itNameLast) |
| Try and match the argument to a switch definition. | |
Static Private Member Functions | |
| static void | validateSwitches (SwitchCIter first, SwitchCIter last) |
| Validate the switch definitions. | |
Private Attributes | |
| SwitchCIter | m_itFirstSwitch |
| The first switch in the table. | |
| SwitchCIter | m_itLastSwitch |
| The last switch in the table. | |
| NamedArgs | m_mapNamedArgs |
| The collection of named arguments. | |
| UnnamedArgs | m_vecUnnamedArgs |
| The collection of unnamed arguments. | |
A command line parser.
The parser supports both Unix style (eg -h & --help) and Windows style (eg /h & /help) switches. The value for the switch can be specifed after the switch name (eg -p <file>) or inline in either format (eg /p:<file> or --print=<file>). A switch can also be referenced multiple times and/or have multiple values (eg -x <value> -x <value> or -x <value>
).
| typedef const CmdLineSwitch* Core::CmdLineParser::SwitchCIter |
An iterator into command line switch table.
| typedef std::vector<tstring> Core::CmdLineParser::StringVector |
A vector of strings.
| typedef std::map<int, StringVector> Core::CmdLineParser::NamedArgs |
The type used to store the named arguments.
The type used to store the unnamed arguments.
typedef const tchar* Core::CmdLineParser::CharCIter [private] |
An iterator for c-style strings.
| Core::CmdLineParser::CmdLineParser | ( | SwitchCIter | itFirstSwitch, |
| SwitchCIter | itLastSwitch | ||
| ) |
Default constructor.
Destructor.
| const CmdLineParser::NamedArgs & Core::CmdLineParser::getNamedArgs | ( | ) | const [inline] |
Get the collection of named arguments.
| const CmdLineParser::UnnamedArgs & Core::CmdLineParser::getUnnamedArgs | ( | ) | const [inline] |
Get the collection of unnamed arguments.
| void Core::CmdLineParser::parse | ( | int | argc, |
| tchar * | argv[], | ||
| int | nFlags = DEFAULTS |
||
| ) |
Parse the command line.
| bool Core::CmdLineParser::isSwitchSet | ( | int | nID | ) | const |
Check if a switch was provided.
| tstring Core::CmdLineParser::getSwitchValue | ( | int | nID | ) | const |
Get the value for a switch.
This only returns the first value that was parsed. To obtain the full list iterate the NamedArgs collection directly.
| tstring Core::CmdLineParser::formatSwitches | ( | Format | eFormat | ) | const |
Generate the list of switches for a usage message.
| void Core::CmdLineParser::reset | ( | ) | [private] |
Reset the internal state.
| CmdLineParser::SwitchCIter Core::CmdLineParser::findSwitch | ( | CharCIter | itNameFirst, |
| CharCIter | itNameLast | ||
| ) | [private] |
Try and match the argument to a switch definition.
| void Core::CmdLineParser::validateSwitches | ( | SwitchCIter | first, |
| SwitchCIter | last | ||
| ) | [static, private] |
Validate the switch definitions.
The first switch in the table.
The last switch in the table.
NamedArgs Core::CmdLineParser::m_mapNamedArgs [private] |
The collection of named arguments.
The collection of unnamed arguments.
1.7.6.1