Fancy argparser
Single file argument parser for c++
Loading...
Searching...
No Matches
ARGPARSE_NAMESPACE_NAME::Argument Class Reference

This class represents argument configuration which should be passed to ArgumentParser objects instance. More...

#include <argparse.h>

Public Member Functions

Argument & SetRequired (bool required)
 Setter function to flag,.
Argument & SetNumberOfArguments (int amount)
 setter function for m_nargs with desired amount
Argument & SetAnyNumberOfArgumentsButAtLeastOne ()
 Handy setter for argument count with self declared name.
Argument & SetAnyNumberOfArguments ()
 Handy setter for argument count with self declared name.
Argument & SetArgumentIsFlag ()
 Handy setter for argument which is actually a flag (e.g.
Argument & SetType (ArgTypeCast argType)
 Setter function for type of current argument.
Argument & SetPositionalName (const std::string &name)
 Handy setter for positional argument.
Argument & SetShortName (const std::string &name)
 Handy setter for short named argument.
Argument & SetLongName (const std::string &name)
 Handy setter for long named argument.
Argument & SetHelp (const std::string &help)
 Handy setter for additional help.
Argument & SetChoices (const std::vector< std::string > &choices)
 Handy setter of valid choices for arguments with string type.
Argument & SetChoices (std::initializer_list< const char * > choices)
 Overload so a braced list of string literals – e.g.
Argument & SetChoices (const std::vector< int > &choices)
 Handy setter of valid choices for arguments with int type.
Argument & SetChoices (const std::vector< long long > &choices)
 Handy setter of valid choices for arguments with long long type.
Argument & SetChoices (const std::vector< double > &choices)
 Handy setter of valid choices for arguments with double type.
Argument & SetDefault (bool defaultArg)
 Handy setter for single default argument of bool type.
Argument & SetDefault (int defaultArg)
 Handy setter for single default argument of int type.
Argument & SetDefault (long long defaultArg)
 Handy setter for single default argument of long long type.
Argument & SetDefault (double defaultArg)
 Handy setter for single default argument of double type.
Argument & SetDefault (std::string defaultArg)
 Handy setter for single default argument of string type.
Argument & SetDefault (const std::vector< bool > &defaultArg)
 Handy setter for vector of default arguments of bool type.
Argument & SetDefault (const std::vector< int > &defaultArg)
 Handy setter for vector of default arguments of int type.
Argument & SetDefault (const std::vector< long long > &defaultArg)
 Handy setter for vector of default arguments of long long type.
Argument & SetDefault (const std::vector< double > &defaultArg)
 Handy setter for vector of default arguments of double type.
Argument & SetDefault (const std::vector< std::string > &defaultArg)
 Handy setter for vector of default arguments of string type.
bool HasDefault () const
 Getter to indicate does argument has any default value.

Static Public Member Functions

static Argument CreateNamedArgument (const std::string &shortName="", const std::string &longName="", const int argsCount=1, ArgTypeCast argType=ArgTypeCast::e_String, const bool required=true, const std::string &help="")
 Default constructor positional arguments.
static Argument CreatePositionalArgument (const std::string &positionalName="", const int argsCount=1, ArgTypeCast argType=ArgTypeCast::e_String, const bool required=true, const std::string &help="")
 Default function for named arguments.

Public Attributes

bool m_required = true
 required flag argument If required argument is not set in command line then parsing will fail.
int m_nargs = 1
 variable that indicates count of argument in input use "kAnyArgCount" or "kFromOneToInfiniteArgCount" constants for arguments with variable count.
ArgTypeCast m_type = ArgTypeCast::e_String
 Variable that hold type of argument.
std::string m_positionalName = ""
 name of positional argument positional arguments name used only to access desired argument from code
std::string m_shortName = ""
 arguments short name for non positional argument only shot name used in input with 1 prefix
std::string m_longName = ""
 arguments long name.
std::string m_help = ""
 additional help info for argument Will be part of generated help
std::vector< std::string > m_choicesString = {}
 vector of strings to validate arguments input data.
std::vector< int > m_choicesInt = {}
 vector of integers to validate arguments input data.
std::vector< long long > m_choicesLongLong = {}
 vector of long longs to validate arguments input data.
std::vector< double > m_choicesDouble = {}
 vector of double to validate arguments input data.

Detailed Description

This class represents argument configuration which should be passed to ArgumentParser objects instance.

Definition at line 125 of file argparse.h.

Member Function Documentation

◆ CreateNamedArgument()

Argument ARGPARSE_NAMESPACE_NAME::Argument::CreateNamedArgument ( const std::string & shortName = "",
const std::string & longName = "",
const int argsCount = 1,
ArgTypeCast argType = ArgTypeCast::e_String,
const bool required = true,
const std::string & help = "" )
inlinestatic

Default constructor positional arguments.

You can use class Setters or pass your own values to public members directly.

Parameters
shortNamerepresents short name for named argument which should be passed with one prefix.
longNamerepresents long name for named argument which should be passed with double prefix.
argsCountinteger number. You can use "kAnyArgCount", "kFromOneToInfiniteArgCount" for non strict count or any int constant.
argTypetype of argument. Defined via enum. Supported types are: int, long long, double and bool and string for all other cases.
requiredIs argument required. Will fail parsing, if required argument are not present.
helpYour own custom help string start.

Definition at line 159 of file argparse.h.

◆ CreatePositionalArgument()

Argument ARGPARSE_NAMESPACE_NAME::Argument::CreatePositionalArgument ( const std::string & positionalName = "",
const int argsCount = 1,
ArgTypeCast argType = ArgTypeCast::e_String,
const bool required = true,
const std::string & help = "" )
inlinestatic

Default function for named arguments.

You can use class Setters or pass your own values to public members directly.

Parameters
positionalNamerepresents name for positional argument.
argsCountinteger number. You can use "kAnyArgCount", "kFromOneToInfiniteArgCount" for non strict count or any int constant.
argTypetype of argument. Defined via enum. Supported types are: int, long long, double and bool and string for all other cases.
requiredIs argument required. Will fail parsing, if required argument are not present.
helpYour own custom help string start.

Definition at line 175 of file argparse.h.

◆ HasDefault()

bool ARGPARSE_NAMESPACE_NAME::Argument::HasDefault ( ) const
inline

Getter to indicate does argument has any default value.

Returns
reference to current argument

Definition at line 540 of file argparse.h.

◆ SetAnyNumberOfArguments()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetAnyNumberOfArguments ( )
inline

Handy setter for argument count with self declared name.

Returns
reference to current argument

Definition at line 232 of file argparse.h.

◆ SetAnyNumberOfArgumentsButAtLeastOne()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetAnyNumberOfArgumentsButAtLeastOne ( )
inline

Handy setter for argument count with self declared name.

Returns
reference to current argument

Definition at line 224 of file argparse.h.

◆ SetArgumentIsFlag()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetArgumentIsFlag ( )
inline

Handy setter for argument which is actually a flag (e.g.

has no any parameters)

Returns
reference to current argument

Definition at line 240 of file argparse.h.

◆ SetChoices() [1/5]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetChoices ( const std::vector< double > & choices)
inline

Handy setter of valid choices for arguments with double type.

Parameters
choicesvector or initializer list of valid double
Returns
reference to current argument

Definition at line 387 of file argparse.h.

◆ SetChoices() [2/5]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetChoices ( const std::vector< int > & choices)
inline

Handy setter of valid choices for arguments with int type.

Parameters
choicesvector or initializer list of valid ints
Returns
reference to current argument

Definition at line 353 of file argparse.h.

◆ SetChoices() [3/5]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetChoices ( const std::vector< long long > & choices)
inline

Handy setter of valid choices for arguments with long long type.

Parameters
choicesvector or initializer list of valid long longs
Returns
reference to current argument

Definition at line 370 of file argparse.h.

◆ SetChoices() [4/5]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetChoices ( const std::vector< std::string > & choices)
inline

Handy setter of valid choices for arguments with string type.

Parameters
choicesvector or initializer list of valid strings
Returns
reference to current argument

Definition at line 326 of file argparse.h.

◆ SetChoices() [5/5]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetChoices ( std::initializer_list< const char * > choices)
inline

Overload so a braced list of string literals – e.g.

SetChoices({"+", "-"}) – resolves unambiguously to the string choices instead of colliding with the int/double/long long overloads.

Parameters
choicesinitializer list of string literals
Returns
reference to current argument

Definition at line 341 of file argparse.h.

◆ SetDefault() [1/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( bool defaultArg)
inline

Handy setter for single default argument of bool type.

Parameters
defaultArgdefault boolean value
Returns
reference to current argument

Definition at line 400 of file argparse.h.

◆ SetDefault() [2/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( const std::vector< bool > & defaultArg)
inline

Handy setter for vector of default arguments of bool type.

Parameters
defaultArgvector of default boolean values
Returns
reference to current argument

Definition at line 470 of file argparse.h.

◆ SetDefault() [3/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( const std::vector< double > & defaultArg)
inline

Handy setter for vector of default arguments of double type.

Parameters
defaultArgvector of default double values
Returns
reference to current argument

Definition at line 512 of file argparse.h.

◆ SetDefault() [4/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( const std::vector< int > & defaultArg)
inline

Handy setter for vector of default arguments of int type.

Parameters
defaultArgvector of default int values
Returns
reference to current argument

Definition at line 484 of file argparse.h.

◆ SetDefault() [5/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( const std::vector< long long > & defaultArg)
inline

Handy setter for vector of default arguments of long long type.

Parameters
defaultArgvector of default long values
Returns
reference to current argument

Definition at line 498 of file argparse.h.

◆ SetDefault() [6/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( const std::vector< std::string > & defaultArg)
inline

Handy setter for vector of default arguments of string type.

Parameters
defaultArgvector of default string values
Returns
reference to current argument

Definition at line 526 of file argparse.h.

◆ SetDefault() [7/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( double defaultArg)
inline

Handy setter for single default argument of double type.

Parameters
defaultArgdefault double value
Returns
reference to current argument

Definition at line 442 of file argparse.h.

◆ SetDefault() [8/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( int defaultArg)
inline

Handy setter for single default argument of int type.

Parameters
defaultArgdefault int value
Returns
reference to current argument

Definition at line 414 of file argparse.h.

◆ SetDefault() [9/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( long long defaultArg)
inline

Handy setter for single default argument of long long type.

Parameters
defaultArgdefault long long value
Returns
reference to current argument

Definition at line 428 of file argparse.h.

◆ SetDefault() [10/10]

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetDefault ( std::string defaultArg)
inline

Handy setter for single default argument of string type.

Parameters
defaultArgdefault string value
Returns
reference to current argument

Definition at line 456 of file argparse.h.

◆ SetHelp()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetHelp ( const std::string & help)
inline

Handy setter for additional help.

Parameters
helpstring with additional help. Empty by default.
Returns
reference to current argument

Definition at line 313 of file argparse.h.

◆ SetLongName()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetLongName ( const std::string & name)
inline

Handy setter for long named argument.

Should been used with double prefix in command line. Can be used for auto-generation of short name if it possible and m_allowAbbrev in ArgumentParsed is "true".

Parameters
namename for positional argument. Empty by default
Returns
reference to current argument

Definition at line 300 of file argparse.h.

◆ SetNumberOfArguments()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetNumberOfArguments ( int amount)
inline

setter function for m_nargs with desired amount

Parameters
amountint value that indicates amount of argument. Could be "kAnyArgCount" or "kFromOneToInfiniteArgCount", 0 or any other positive integer.
Returns
reference to current argument

Definition at line 216 of file argparse.h.

◆ SetPositionalName()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetPositionalName ( const std::string & name)
inline

Handy setter for positional argument.

Parameters
namename for positional argument. Empty by default
Returns
reference to current argument

Definition at line 267 of file argparse.h.

◆ SetRequired()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetRequired ( bool required)
inline

Setter function to flag,.

Parameters
required- bool value to indicate is argument required or not
Returns
reference to current argument

Definition at line 198 of file argparse.h.

◆ SetShortName()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetShortName ( const std::string & name)
inline

Handy setter for short named argument.

Should been used with ordinary prefix in command line. Can be auto-generated if possible when m_allowAbbrev in ArgumentParsed set to true.

Parameters
namename for positional argument. Empty by default
Returns
reference to current argument

Definition at line 283 of file argparse.h.

◆ SetType()

Argument & ARGPARSE_NAMESPACE_NAME::Argument::SetType ( ArgTypeCast argType)
inline

Setter function for type of current argument.

Parameters
argTypesetter for type of current argument data. Any non string types will be casted while parsing.
Returns
reference to current argument

Definition at line 254 of file argparse.h.

Member Data Documentation

◆ m_choicesDouble

std::vector<double> ARGPARSE_NAMESPACE_NAME::Argument::m_choicesDouble = {}

vector of double to validate arguments input data.

Empty by default. Will fail parsing if double not is in input list

Definition at line 382 of file argparse.h.

◆ m_choicesInt

std::vector<int> ARGPARSE_NAMESPACE_NAME::Argument::m_choicesInt = {}

vector of integers to validate arguments input data.

Empty by default. Will fail parsing if ints not is in input list

Definition at line 348 of file argparse.h.

◆ m_choicesLongLong

std::vector<long long> ARGPARSE_NAMESPACE_NAME::Argument::m_choicesLongLong = {}

vector of long longs to validate arguments input data.

Empty by default. Will fail parsing if long longs not is in input list

Definition at line 365 of file argparse.h.

◆ m_choicesString

std::vector<std::string> ARGPARSE_NAMESPACE_NAME::Argument::m_choicesString = {}

vector of strings to validate arguments input data.

Empty by default. Will fail parsing if string not is in input list

Definition at line 321 of file argparse.h.

◆ m_help

std::string ARGPARSE_NAMESPACE_NAME::Argument::m_help = ""

additional help info for argument Will be part of generated help

Definition at line 308 of file argparse.h.

◆ m_longName

std::string ARGPARSE_NAMESPACE_NAME::Argument::m_longName = ""

arguments long name.

for non positional arguments only argument long name start with double prefix

Definition at line 292 of file argparse.h.

◆ m_nargs

int ARGPARSE_NAMESPACE_NAME::Argument::m_nargs = 1

variable that indicates count of argument in input use "kAnyArgCount" or "kFromOneToInfiniteArgCount" constants for arguments with variable count.

Any other arguments count will be passed as strict arguments count. 0 is for flags (arguments that doesn't carry any data) set to 1 by default.

Definition at line 210 of file argparse.h.

◆ m_positionalName

std::string ARGPARSE_NAMESPACE_NAME::Argument::m_positionalName = ""

name of positional argument positional arguments name used only to access desired argument from code

Definition at line 262 of file argparse.h.

◆ m_required

bool ARGPARSE_NAMESPACE_NAME::Argument::m_required = true

required flag argument If required argument is not set in command line then parsing will fail.

IMPORTANT: ALL arguments – named AND positional – are REQUIRED by default. This differs from Python's argparse, where named options are optional by default. To make an argument optional call SetRequired(false) (or pass required=false to the factory function). Optional flags in particular almost always want SetRequired(false).

Definition at line 193 of file argparse.h.

◆ m_shortName

std::string ARGPARSE_NAMESPACE_NAME::Argument::m_shortName = ""

arguments short name for non positional argument only shot name used in input with 1 prefix

Definition at line 276 of file argparse.h.

◆ m_type

ArgTypeCast ARGPARSE_NAMESPACE_NAME::Argument::m_type = ArgTypeCast::e_String

Variable that hold type of argument.

string by default

Definition at line 248 of file argparse.h.


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