libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
CmdLineParser Class Reference

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 Stringarg (int index) const
 Returns a non-option argument by index.
 
StringList generateUsage () const
 Generates usage/help text for all registered options.
 

Detailed Description

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

Example
bool verbose = false;
parser.registerOptions({
{'v', "verbose", "Enable verbose output",
CmdLineParser::OptionCallback([&]() { verbose = true; return 0; })},
{'o', "output", "Output file path",
CmdLineParser::OptionStringCallback([&](const String &s) { output = s; return 0; })},
});
parser.parseMain(argc, argv);
// Non-option args: parser.arg(0), parser.arg(1), ...
Command-line argument parser with callback-driven option handling.
Definition cmdlineparser.h:44
std::function< int()> OptionCallback
Callback for options that take no argument.
Definition cmdlineparser.h:47
std::function< int(const String &)> OptionStringCallback
Callback for options that take a string argument.
Definition cmdlineparser.h:53
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
is encountered during parsing. Non-option arguments are collected and accessible after parsing completes.

Member Typedef Documentation

◆ OptionCallbackVariant

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.

Member Function Documentation

◆ arg()

const String & CmdLineParser::arg ( int  index) const
inline

Returns a non-option argument by index.

Parameters
indexThe zero-based argument index.
Returns
A const reference to the argument string.

◆ argCount()

int CmdLineParser::argCount ( ) const
inline

Returns the number of non-option arguments remaining after parsing.

Returns
The argument count.

◆ generateUsage()

StringList CmdLineParser::generateUsage ( ) const

Generates usage/help text for all registered options.

Returns
A StringList where each entry describes one option.

◆ parse()

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().

Parameters
argsThe argument list to parse.
Returns
0 on success, or a non-zero value returned by an option callback.

◆ parseMain()

int CmdLineParser::parseMain ( int  argc,
char **  argv 
)
inline

Parses command-line arguments from main().

Wraps parse() for convenience, converting argc/argv and stripping the program name (argv[0]).

Parameters
argcArgument count from main().
argvArgument vector from main().
Returns
0 on success, or a non-zero value returned by an option callback.

◆ registerOptions()

void CmdLineParser::registerOptions ( const std::initializer_list< Option > &  options)
inline

Registers a list of options with the parser.

Each option is indexed by its short and/or long name for lookup during parsing.

Parameters
optionsAn initializer list of Option objects to register.

The documentation for this class was generated from the following file: