Command-line argument parser with callback-driven option handling. More...
#include <cmdlineparser.h>
Classes | |
| class | Option |
| Describes a single command-line option. More... | |
Public Types | |
| using | OptionCallback = std::function< int()> |
| Callback for options that take no argument. | |
| using | OptionBoolCallback = std::function< int(bool)> |
| Callback for options that take a boolean argument. | |
| using | OptionStringCallback = std::function< int(const String &)> |
| Callback for options that take a string argument. | |
| using | OptionIntCallback = std::function< int(int)> |
| Callback for options that take an integer argument. | |
| using | OptionDoubleCallback = std::function< int(double)> |
| Callback for options that take a double argument. | |
| using | OptionCallbackVariant = std::variant< OptionCallback, OptionBoolCallback, OptionStringCallback, OptionIntCallback, OptionDoubleCallback > |
| Variant type holding any of the supported option callbacks. | |
Public Member Functions | |
| void | registerOptions (const std::initializer_list< Option > &options) |
| Registers a list of options with the parser. | |
| CmdLineParser ()=default | |
| Default constructor. | |
| void | clear () |
| Clears all registered options, mappings, and collected arguments. | |
| int | parseMain (int argc, char **argv) |
| Parses command-line arguments from main(). | |
| int | parse (StringList args) |
| Parses the given argument list against registered options. | |
| int | argCount () const |
| Returns the number of non-option arguments remaining after parsing. | |
| const String & | arg (int index) const |
| Returns a non-option argument by index. | |
| StringList | generateUsage () const |
| Generates usage/help text for all registered options. | |
Command-line argument parser with callback-driven option handling.
Supports short (-x) and long (–name) options with typed arguments. Each option is associated with a callback that is invoked when the option
| using CmdLineParser::OptionCallbackVariant = std::variant< OptionCallback, OptionBoolCallback, OptionStringCallback, OptionIntCallback, OptionDoubleCallback> |
Variant type holding any of the supported option callbacks.
Always add new callback types to the end, as the variant index is used to infer the argument data type.
Returns a non-option argument by index.
| index | The zero-based argument index. |
|
inline |
Returns the number of non-option arguments remaining after parsing.
| StringList CmdLineParser::generateUsage | ( | ) | const |
Generates usage/help text for all registered options.
| int CmdLineParser::parse | ( | StringList | args | ) |
Parses the given argument list against registered options.
Options are consumed and their callbacks invoked. Remaining non-option arguments are stored and accessible via arg() and argCount().
| args | The argument list to parse. |
|
inline |
Parses command-line arguments from main().
Wraps parse() for convenience, converting argc/argv and stripping the program name (argv[0]).
| argc | Argument count from main(). |
| argv | Argument vector from main(). |
Registers a list of options with the parser.
Each option is indexed by its short and/or long name for lookup during parsing.
| options | An initializer list of Option objects to register. |