|
lintel 0.2.0
|
#include <Command.h>
Public Types | |
| using | Value = std::variant< int32_t *, uint32_t *, double *, float *, bool *, std::string *, std::vector< std::string > *, std::optional< int32_t > *, std::optional< uint32_t > *, std::optional< double > *, std::optional< float > *, std::optional< bool > *, std::optional< std::string > * > |
Public Member Functions | |
| Command (std::string command, std::string description) | |
| Command & | addArgument (const std::vector< std::string > &flags, Value defaultValue, const std::string &help, bool optional=false) |
| Command & | addArgument (const std::vector< std::string > &&flags, Value defaultValue, std::string help, bool optional=false) |
| void | printHelp (std::ostream &os=std::cout) const |
| std::string | getCommand () const |
| void | parse (const std::string &command, const std::vector< std::string > &flags) const |
| void | resetOptionals () const |
Static Public Member Functions | |
| static void | parseValueFromStream (const std::string &value, const Value &m_value) |
Represents a CLI command with named arguments and automatic parsing
| using lintel::Command::Value = std::variant<int32_t *, uint32_t *, double *, float *, bool *, std::string *, std::vector<std::string> *, std::optional<int32_t> *, std::optional<uint32_t> *, std::optional<double> *, std::optional<float> *, std::optional<bool> *, std::optional<std::string> *> |
| lintel::Command::Command | ( | std::string | command, |
| std::string | description | ||
| ) |
| command | the command name |
| description | description printed in help |
| Command & lintel::Command::addArgument | ( | const std::vector< std::string > && | flags, |
| Value | defaultValue, | ||
| std::string | help, | ||
| bool | optional = false |
||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| Command & lintel::Command::addArgument | ( | const std::vector< std::string > & | flags, |
| Value | defaultValue, | ||
| const std::string & | help, | ||
| bool | optional = false |
||
| ) |
Adds a possible option. A typical call would be like this: bool printHelp = false; cmd.addArgument({"--help", "-h"}, &printHelp, "Print this help message"); Then, after parse() has been called, printHelp will be true if the user provided the flag.
| flags | list of flag aliases (e.g. {"--help", "-h"}) |
| defaultValue | pointer to the target variable |
| help | help text for this argument |
| optional | whether the argument is optional |
| std::string lintel::Command::getCommand | ( | ) | const |
| void lintel::Command::parse | ( | const std::string & | command, |
| const std::vector< std::string > & | flags | ||
| ) | const |
Parse command-line arguments. Arguments are traversed from start to end; if an option is set multiple times, the last one wins.
| command | the command string |
| flags | the flag values to parse |
| std::runtime_error | if a value is missing for a given option |
|
static |
Parse a single value from string into the variant target
| void lintel::Command::printHelp | ( | std::ostream & | os = std::cout | ) | const |
Print the command description and help for each option
| void lintel::Command::resetOptionals | ( | ) | const |
Reset all optional values to their defaults