RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
RST::Parser::Argument Class Reference

A flag, option or positional argument, configured with chained calls. More...

#include <RST/parser/Argument.hpp>

Public Member Functions

template<typename T >
Argument & defaultValue (const T &value)
 Value used when the argument is not given.
 
Argument & required (bool isRequired=true)
 Makes the argument mandatory.
 
Argument & valueName (std::string name)
 Name of the value in the help, such as FILE.
 
Argument & nargs (int exactly)
 Takes exactly that many values.
 
Argument & choices (std::vector< std::string > values)
 Only accepts one of values.
 
Argument & integer ()
 Only accepts integers.
 
Argument & number ()
 Only accepts numbers.
 
Argument & range (double min, double max)
 Only accepts numbers from min to max, inclusive.
 
Argument & mustExist ()
 Only accepts paths that exist.
 
Argument & extension (std::vector< std::string > extensions)
 Only accepts paths ending with one of extensions, such as .json.
 
Argument & envFallback (std::string variable)
 Reads the environment variable variable when the argument is not given.
 
Argument & allowNegation ()
 Lets --no-name unset a flag declared as --name.
 
Argument & nargs (Nargs count)
 How many values to take, such as Nargs::oneOrMore().
 
Argument & validate (std::function< bool(const std::string &)> check, std::string message="")
 Custom check: values for which check returns false are rejected with message.
 
bool isSet () const noexcept
 Whether a value came from the command line, the environment or the config file.
 
std::size_t count () const noexcept
 How many times the argument was given, such as 3 for -vvv.
 
template<typename T = std::string>
std::optional< T > get () const
 The last value, or the default value, converted to T; std::nullopt if there is none or it does not convert.
 
template<typename T = std::string>
std::vector< T > getAll () const
 Every value converted to T, skipping those that do not convert, or the default value.
 

Friends

class ArgParser
 

Detailed Description

A flag, option or positional argument, configured with chained calls.

Keep the reference returned by the ArgParser to read the value after parsing:

auto& jobs = parser.addOption({"-j", "--jobs"}, "Parallel jobs").integer().range(1, 64).defaultValue(4);
// after parsing
int count = jobs.get<int>().value_or(4);
std::size_t count() const noexcept
How many times the argument was given, such as 3 for -vvv.
Definition Argument.hpp:91
Argument & integer()
Only accepts integers.
Definition Argument.hpp:57
std::optional< T > get() const
The last value, or the default value, converted to T; std::nullopt if there is none or it does not co...
Definition Argument.hpp:95
Argument & range(double min, double max)
Only accepts numbers from min to max, inclusive.
Definition Argument.hpp:61
Argument & defaultValue(const T &value)
Value used when the argument is not given.
Definition Argument.hpp:46

Member Function Documentation

◆ defaultValue()

template<typename T >
Argument & RST::Parser::Argument::defaultValue ( const T &  value)
inline

Value used when the argument is not given.

Examples
parser.cpp, and quickstart.cpp.

◆ required()

Argument & RST::Parser::Argument::required ( bool  isRequired = true)
inline

Makes the argument mandatory.

◆ valueName()

Argument & RST::Parser::Argument::valueName ( std::string  name)
inline

Name of the value in the help, such as FILE.

◆ nargs() [1/2]

Argument & RST::Parser::Argument::nargs ( int  exactly)
inline

Takes exactly that many values.

Examples
parser.cpp.

◆ choices()

Argument & RST::Parser::Argument::choices ( std::vector< std::string >  values)
inline

Only accepts one of values.

◆ integer()

Argument & RST::Parser::Argument::integer ( )
inline

Only accepts integers.

◆ number()

Argument & RST::Parser::Argument::number ( )
inline

Only accepts numbers.

◆ range()

Argument & RST::Parser::Argument::range ( double  min,
double  max 
)
inline

Only accepts numbers from min to max, inclusive.

Examples
parser.cpp, and quickstart.cpp.

◆ mustExist()

Argument & RST::Parser::Argument::mustExist ( )
inline

Only accepts paths that exist.

◆ extension()

Argument & RST::Parser::Argument::extension ( std::vector< std::string >  extensions)
inline

Only accepts paths ending with one of extensions, such as .json.

◆ envFallback()

Argument & RST::Parser::Argument::envFallback ( std::string  variable)
inline

Reads the environment variable variable when the argument is not given.

◆ allowNegation()

Argument & RST::Parser::Argument::allowNegation ( )
inline

Lets --no-name unset a flag declared as --name.

◆ nargs() [2/2]

Argument & RST::Parser::Argument::nargs ( Nargs  count)
inline

How many values to take, such as Nargs::oneOrMore().

◆ validate()

Argument & RST::Parser::Argument::validate ( std::function< bool(const std::string &)>  check,
std::string  message = "" 
)
inline

Custom check: values for which check returns false are rejected with message.

◆ isSet()

bool RST::Parser::Argument::isSet ( ) const
inlinenoexcept

Whether a value came from the command line, the environment or the config file.

◆ count()

std::size_t RST::Parser::Argument::count ( ) const
inlinenoexcept

How many times the argument was given, such as 3 for -vvv.

◆ get()

template<typename T = std::string>
std::optional< T > RST::Parser::Argument::get ( ) const
inline

The last value, or the default value, converted to T; std::nullopt if there is none or it does not convert.

◆ getAll()

template<typename T = std::string>
std::vector< T > RST::Parser::Argument::getAll ( ) const
inline

Every value converted to T, skipping those that do not convert, or the default value.

Friends And Related Symbol Documentation

◆ ArgParser

friend class ArgParser
friend

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