LibCppCmd v1.0 Beta 15
C++ Command Line Parsing Library
|
Parses command line arguments. More...
#include <Parser.h>
Classes | |
class | DuplicateOption |
An exception thrown for adding a duplicate Option. More... | |
class | DuplicatePosParam |
An exception thrown for adding duplicate PosParam. More... | |
class | EmptyArguments |
An exception thrown for a Parser with empty arguments. More... | |
class | NullParameter |
An exception thrown for adding null Parameter. More... | |
Public Types | |
enum class | Status { Success , Failure } |
Determines the status of the Parser. More... | |
Public Member Functions | |
Parser (ProgParam *p, std::vector< std::string > args) | |
Constructs a new Parser. | |
Status | Parse () |
Parses the command line arguments. | |
std::string | GenerateUsage () const |
Generates program usage info. | |
std::string | GenerateHelp () const |
Generates program help info. | |
bool | BuiltInHelpOptionIsSpecified () const |
Determines if the built-in help Option has been specified. | |
void | Add (Option *o) |
Adds an Option to the Parser. | |
void | Add (PosParam *p) |
Adds a PosParam to the Parser. | |
void | Set (MultiPosParam *p) |
Sets a MultiPosParam on the Parser. | |
void | Set (Option::Style s) |
Sets the Option::Style on all options added to the Parser. | |
bool | AllMandatoryParamsSpecified () |
Checks that all mandatory Param were specified. | |
Parses command line arguments.
The command line Parser populates command line parameters (Param) from command line arguments. A Parser is created from a ProgParam and a vector containing a list of command line arguments. Prior to parsing the arguments, the program must add command line parameters (Param) to the Parser so it can populate them. Such parameters include Options (both Option and ValueOption) and positional parameters (PosParam). The parser also allows zero or one multi-positional parameters (MultiPosParam) to be set (positional parameters that accept multiple positional arguments). Finally, the Parser can generate command line usage and help information for the program based on each Param added to the Parser.
|
strong |
Determines the status of the Parser.
CmdLine::Parser::Parser | ( | ProgParam * | p, |
std::vector< std::string > | args ) |
Constructs a new Parser.
p | The ProgParam to populate with the program name. |
args | The command line arguments to parse. |
NullParamter | The parameter is null. |
EmptyArguments | The arguments were empty. |
void CmdLine::Parser::Add | ( | Option * | o | ) |
Adds an Option pointer to the Parser so that the Parser can populate the Option if the user specifies it at the command line. This method will also accept pointers of the derived class ValueOption.
DuplicateOption | Tried to add a duplicate Option. |
void CmdLine::Parser::Add | ( | PosParam * | p | ) |
bool CmdLine::Parser::AllMandatoryParamsSpecified | ( | ) |
Checks that all mandatory Param were specified.
|
inline |
std::string CmdLine::Parser::GenerateHelp | ( | ) | const |
Generates program help info.
Generates program help info from each command line Param (ProgParam, Option, ValueOption, PosParam, and MultiPosParam) that has been added or set on the Parser. Help info is the detailed help information that programs typically show when a help option (-h, –help, -?, /h, /help, etc.) is specified.
std::string CmdLine::Parser::GenerateUsage | ( | ) | const |
Generates program usage info.
Generates program usage info from each command line Param (ProgParam, Option, ValueOption, PosParam, and MultiPosParam) that has been added or set on the Parser. Usage information is typically shown when the user supplies invalid command line arguments, or no arguments, to the program.
Status CmdLine::Parser::Parse | ( | ) |
Parses the command line arguments.
Parsing the command line arguments will populate any command line Param (ProgParam, Option, ValueOption, PosParam, and MultiPosParam) that have been added or set on the Parser and that could be populated by one of the arguments.
|
inline |
Sets a MultiPosParam on the Parser.
A Parser can only have zero or one MultiPosParam at a time. By default, the Parser has no MultiPosParams unless a pointer to one is set. If the pointer is null, the Parser does not process the MultiPosParam.
p | The pointer to the MultiPosParam to set on the Parser. |
void CmdLine::Parser::Set | ( | Option::Style | s | ) |
Sets the Option::Style on all options added to the Parser.
An Option usually has its Style set during creation in its Option::Definition. However, it may be more convenient / less error prone to set the Option::Style for all options after they have been added to the Parser for the sake of consistency. That is the purpose of this method.
s | The Option::Style to set on each Option. |