lintel 0.2.0
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
lintel::Command Class Reference

#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)
 

Detailed Description

Represents a CLI command with named arguments and automatic parsing

Member Typedef Documentation

◆ Value

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> *>

Constructor & Destructor Documentation

◆ Command()

lintel::Command::Command ( std::string  command,
std::string  description 
)
Parameters
commandthe command name
descriptiondescription printed in help

Member Function Documentation

◆ addArgument() [1/2]

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.

◆ addArgument() [2/2]

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.

Parameters
flagslist of flag aliases (e.g. {"--help", "-h"})
defaultValuepointer to the target variable
helphelp text for this argument
optionalwhether the argument is optional

◆ getCommand()

std::string lintel::Command::getCommand ( ) const
Returns
the command name

◆ parse()

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.

Parameters
commandthe command string
flagsthe flag values to parse
Exceptions
std::runtime_errorif a value is missing for a given option

◆ parseValueFromStream()

static void lintel::Command::parseValueFromStream ( const std::string &  value,
const Value &  m_value 
)
static

Parse a single value from string into the variant target

◆ printHelp()

void lintel::Command::printHelp ( std::ostream &  os = std::cout) const

Print the command description and help for each option

◆ resetOptionals()

void lintel::Command::resetOptionals ( ) const

Reset all optional values to their defaults


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