Polyhedral-net Splines
Loading...
Searching...
No Matches
SimpleArg Struct Reference

A simple command-line argument parser. More...

#include <simple_arg.hpp>

Classes

struct  Opt
 An option (named or positional). More...

Public Member Functions

 SimpleArg (std::string p)
 Construct a new Simple Arg object.
template<typename T>
void add (char s, std::string l, std::string d, bool req=false, T def=T{}, std::vector< std::string > choices={})
 Add a named argument.
template<typename T>
void addPositional (std::string l, std::string d, std::vector< std::string > choices={})
 Add a positional argument.
bool parse (int argc, char **argv)
 Parse the command-line arguments.
template<typename T>
get (const std::string &name) const
 Get the value of a named argument.
bool help () const
 Check if help was requested.
bool errors () const
 Check if there were any errors during parsing.
std::string errorMsg () const
 Get the error message.
template<typename T>
getPositional (size_t idx) const
 Get the Positional object.
void printHelp (std::ostream &os=std::cout) const
 Print the help message.

Public Attributes

std::string prog
 The name of the program (usually argv[0]).
std::vector< Optopts
 The list of named options.
std::vector< Optpositionals
 The list of positional options.
std::map< std::string, std::string > vals
 The parsed values.
std::map< std::string, bool > flags
 The parsed flags.
std::vector< std::string > errs
 The list of errors encountered during parsing.

Private Member Functions

bool store (const Opt &o, const std::string &key, const std::string &val)

Detailed Description

A simple command-line argument parser.

Constructor & Destructor Documentation

◆ SimpleArg()

SimpleArg::SimpleArg ( std::string p)
inline

Construct a new Simple Arg object.

Parameters
pName of the program (usually argv[0]).

Member Function Documentation

◆ add()

template<typename T>
void SimpleArg::add ( char s,
std::string l,
std::string d,
bool req = false,
T def = T{},
std::vector< std::string > choices = {} )
inline

Add a named argument.

Template Parameters
TType of the argument. Only bool, int, float, and std::string are supported.
Parameters
sThe short name (e.g. 'h' for -h).
lThe long name (e.g. "help" for –help).
dDescription of the argument.
reqWhether the argument is required (true) or optional (false).
defDefault value (only if req is false).
choicesAllowed choices (only for enum types).

◆ addPositional()

template<typename T>
void SimpleArg::addPositional ( std::string l,
std::string d,
std::vector< std::string > choices = {} )
inline

Add a positional argument.

Template Parameters
TType of the argument. Only int, float, and std::string are supported.
Parameters
lThe name of the positional argument (for help messages).
dDescription of the argument.
choicesAllowed choices (only for enum types).

◆ errorMsg()

std::string SimpleArg::errorMsg ( ) const
inline

Get the error message.

Returns
std::string The error message, or an empty string if there were no errors.

◆ errors()

bool SimpleArg::errors ( ) const
inline

Check if there were any errors during parsing.

Returns
true if there were errors, false otherwise.

◆ get()

template<typename T>
T SimpleArg::get ( const std::string & name) const
inline

Get the value of a named argument.

Template Parameters
TType of the argument. Only bool, int, float, and std::string are supported.
Parameters
nameThe long name of the argument (e.g. "help" for –help).
Returns
T The value of the argument.

◆ getPositional()

template<typename T>
T SimpleArg::getPositional ( size_t idx) const
inline

Get the Positional object.

Template Parameters
TType of the positional argument. Only int, float, and std::string are supported.
Parameters
idx
Returns
T The value of the positional argument at index idx.

◆ help()

bool SimpleArg::help ( ) const
inline

Check if help was requested.

Returns
true if help was requested, false otherwise.

◆ parse()

bool SimpleArg::parse ( int argc,
char ** argv )
inline

Parse the command-line arguments.

Parameters
argcThe number of arguments (from main).
argvThe argument values (from main).
Returns
true if parsing was successful, false otherwise.

◆ printHelp()

void SimpleArg::printHelp ( std::ostream & os = std::cout) const
inline

Print the help message.

Parameters
osThe output stream to print to (default: std::cout).

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