scsl 1.0.1
Shimmering Clarity Standard Library
|
Basic facility for processing command line flags. More...
#include <Flags.h>
Public Types | |
enum class | ParseStatus : uint8_t { Unknown = 0 , OK = 1 , EndOfFlags = 2 , NotRegistered = 3 , NotEnoughArgs = 4 } |
Public Member Functions | |
Flags (std::string fName) | |
Flags (std::string fName, std::string fDescription) | |
~Flags () | |
bool | Register (std::string fName, FlagType fType, std::string fDescription) |
bool | Register (std::string fName, bool defaultValue, std::string fDescription) |
bool | Register (std::string fName, int defaultValue, std::string fDescription) |
bool | Register (std::string fName, unsigned int defaultValue, std::string fDescription) |
bool | Register (std::string fName, size_t defaultValue, std::string fDescription) |
bool | Register (std::string fName, std::string defaultValue, std::string fDescription) |
bool | Register (std::string fName, const char *defaultValue, std::string fDescription) |
size_t | Size () |
Return the number of registered flags. | |
Flag * | Lookup (std::string fName) |
bool | ValueOf (std::string fName, FlagValue &value) |
ValueOf returns the value of the flag in the. | |
ParseStatus | Parse (int argc, char **argv, bool skipFirst=true) |
void | Usage (std::ostream &os, int exitCode) |
Print a usage message to the output stream. | |
size_t | NumArgs () |
std::vector< std::string > | Args () |
Return the arguments as a vector. | |
std::string | Arg (size_t index) |
bool | GetBool (std::string fName, bool &flagValue) |
bool | GetUnsignedInteger (std::string fName, unsigned int &flagValue) |
bool | GetInteger (std::string fName, int &flagValue) |
bool | GetString (std::string fName, std::string &flagValue) |
bool | GetSizeT (std::string fName, std::size_t &flagValue) |
Static Public Member Functions | |
static std::string | ParseStatusToString (ParseStatus status) |
Basic facility for processing command line flags.
Any remaining arguments after the args are added to the parser as arguments that can be accessed with NumArgs, Args, and Arg.
Arguments are named with their leading dash, e.g. "-f". For example,
A short example program is below:
|
strong |
ParseStatus describes the result of parsing command-line arguments.
scsl::Flags::Flags | ( | std::string | fName | ) |
Create a new flags parser for the named program.
fName | The program name, |
scsl::Flags::Flags | ( | std::string | fName, |
std::string | fDescription | ||
) |
Create a new flags parser for the named program.
fName | The program name. |
fDescription | A short description of the program. |
scsl::Flags::~Flags | ( | ) |
std::string scsl::Flags::Arg | ( | size_t | index | ) |
Return a particular argument.
index | The argument number to extract. |
std::vector< std::string > scsl::Flags::Args | ( | ) |
Return the arguments as a vector.
bool scsl::Flags::GetBool | ( | std::string | fName, |
bool & | flagValue | ||
) |
Retrieve the state of a boolean flag.
fName | The flag to look up. |
flagValue | The value to store. |
bool scsl::Flags::GetInteger | ( | std::string | fName, |
int & | flagValue | ||
) |
Retrieve the value of an integer flag.
fName | The flag to look up. |
flagValue | The value to store. |
bool scsl::Flags::GetSizeT | ( | std::string | fName, |
std::size_t & | flagValue | ||
) |
Retrieve the value of a size_t flag.
fName | The flag to look up. |
flagValue | The value to store. |
bool scsl::Flags::GetString | ( | std::string | fName, |
std::string & | flagValue | ||
) |
Retrieve the value of a string flag.
fName | The flag to look up. |
flagValue | The value to store. |
bool scsl::Flags::GetUnsignedInteger | ( | std::string | fName, |
unsigned int & | flagValue | ||
) |
Retrieve the value of an unsigned integer flag.
fName | The flag to look up. |
flagValue | The value to store. |
Flag * scsl::Flags::Lookup | ( | std::string | fName | ) |
Lookup a flag.
fName | The flag name. |
size_t scsl::Flags::NumArgs | ( | ) |
Return the number of arguments processed. These are any remaining elements in argv that are not flags.
ParseStatus scsl::Flags::Parse | ( | int | argc, |
char ** | argv, | ||
bool | skipFirst = true |
||
) |
Process a list of arguments into flags.
argc | The number of arguments. |
argv | The arguments passed to the program. |
skipFirst | Flags expects to receive arguments directly from those passed to main , and defaults to skipping the first argument. Set to false if this is not the case. |
|
static |
ParseStatusToString returns a string message describing the result of parsing command line args.
bool scsl::Flags::Register | ( | std::string | fName, |
bool | defaultValue, | ||
std::string | fDescription | ||
) |
Register a new boolean command line flag with a default value.
fName | The name of the flag, including a leading dash. |
defaultValue | The default value for the flag. |
fDescription | A short description of the flag. |
bool scsl::Flags::Register | ( | std::string | fName, |
const char * | defaultValue, | ||
std::string | fDescription | ||
) |
Register a new string command line flag with a default value.
fName | The name of the flag, including a leading dash. |
defaultValue | The default value for the flag. |
fDescription | A short description of the flag. |
bool scsl::Flags::Register | ( | std::string | fName, |
FlagType | fType, | ||
std::string | fDescription | ||
) |
Register a new command line flag.
fName | The name of the flag, including a leading dash. |
fType | The type of the argument to parse. |
fDescription | A description of the flag. |
bool scsl::Flags::Register | ( | std::string | fName, |
int | defaultValue, | ||
std::string | fDescription | ||
) |
Register a new integer command line flag with a default value.
fName | The name of the flag, including a leading dash. |
defaultValue | The default value for the flag. |
fDescription | A short description of the flag. |
bool scsl::Flags::Register | ( | std::string | fName, |
size_t | defaultValue, | ||
std::string | fDescription | ||
) |
Register a new size_t command line flag with a default value.
fName | The name of the flag, including a leading dash. |
defaultValue | The default value for the flag. |
fDescription | A short description of the flag. |
bool scsl::Flags::Register | ( | std::string | fName, |
std::string | defaultValue, | ||
std::string | fDescription | ||
) |
Register a new string command line flag with a default value.
fName | The name of the flag, including a leading dash. |
defaultValue | The default value for the flag. |
fDescription | A short description of the flag. |
bool scsl::Flags::Register | ( | std::string | fName, |
unsigned int | defaultValue, | ||
std::string | fDescription | ||
) |
Register a new unsigned integer command line flag with a default value.
fName | The name of the flag, including a leading dash. |
defaultValue | The default value for the flag. |
fDescription | A short description of the flag. |
size_t scsl::Flags::Size | ( | ) |
Return the number of registered flags.
void scsl::Flags::Usage | ( | std::ostream & | os, |
int | exitCode | ||
) |
Print a usage message to the output stream.
bool scsl::Flags::ValueOf | ( | std::string | fName, |
FlagValue & | value | ||
) |
ValueOf returns the value of the flag in the.