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

Main class of argument parser hold all user arguments from code and orchestrate other classes in order to parse command line input. More...

#include <argparse.h>

Public Member Functions

 ArgumentParser (const std::string &name) noexcept
 Constructor for ArgumentParser.
ArgumentParserSetDescription (const std::string &description) noexcept
 Overload default description for auto-generated command line.
ArgumentParserSetAllowAbbrev (bool allowAbbrev) noexcept
 Allows long options to be abbreviated if the abbreviation is unambiguous.
ArgumentParserSetIgnoreUnknownArgs (bool ignoreUnknownArgs) noexcept
 Setter to ignore unknown argument while parsing.
ArgumentParserSetAddHelp (bool addHelp) noexcept
 Add a - h / –help option to the parser.
ArgumentParserSetEpilogue (const std::string &epilogue) noexcept
 This function allows to set final message after program description and before argument list description.
ArgumentParserSetUsage (const std::string &usage) noexcept
 Program usage examples.
ArgumentParserSetPrefixChars (const char charSym) noexcept
 Function to override default prefix.
void AddArgument (const Argument &arg)
 Function to add arguments specification to command line parser.
ArgumentsObject ParseArgs (const std::vector< std::string > &args)
 Main function of parsing argument.
bool _unknownArgumentHit (ArgumentsObject &argObj, const size_t positionInInput, size_t &currentArgumentObjectIndex, bool &positionalArgsEndFlag, const std::string &el)
 function which resolves unknown arguments presence
ArgumentsObject ParseArgs (const int argc, char **argv)
 This function just converts argc and argv to vector of token.
std::string GetHelp (size_t width=kHelpWidth, size_t nameWidthPercent=kHelpNameWidthPercent)
 Function to get help string.

Static Public Member Functions

static std::string TrimTrailingSpacesPerLine (const std::string &text)
 Removes trailing spaces/tabs from every line of the help text.

Detailed Description

Main class of argument parser hold all user arguments from code and orchestrate other classes in order to parse command line input.

Definition at line 1239 of file argparse.h.

Constructor & Destructor Documentation

◆ ArgumentParser()

ARGPARSE_NAMESPACE_NAME::ArgumentParser::ArgumentParser ( const std::string & name)
inlinenoexcept

Constructor for ArgumentParser.

Parameters
nameProgram name which will appear in auto-generated help

Definition at line 1244 of file argparse.h.

Member Function Documentation

◆ _unknownArgumentHit()

bool ARGPARSE_NAMESPACE_NAME::ArgumentParser::_unknownArgumentHit ( ArgumentsObject & argObj,
const size_t positionInInput,
size_t & currentArgumentObjectIndex,
bool & positionalArgsEndFlag,
const std::string & el )
inline

function which resolves unknown arguments presence

Parameters
argObj
positionInInput
currentArgumentObjectIndex
positionalArgsEndFlag
el
Returns
bool - true for ignoring, false for stopping parse

Definition at line 1629 of file argparse.h.

◆ AddArgument()

void ARGPARSE_NAMESPACE_NAME::ArgumentParser::AddArgument ( const Argument & arg)
inline

Function to add arguments specification to command line parser.

Parameters
argArgument instance

Definition at line 1319 of file argparse.h.

◆ GetHelp()

std::string ARGPARSE_NAMESPACE_NAME::ArgumentParser::GetHelp ( size_t width = kHelpWidth,
size_t nameWidthPercent = kHelpNameWidthPercent )
inline

Function to get help string.

Parameters
widthcurrent terminal width (80 by default)
nameWidthPercentpercentage of current width, for naming parameters (30 by default)
Returns
help string with proper new lines

Definition at line 1664 of file argparse.h.

◆ ParseArgs() [1/2]

ArgumentsObject ARGPARSE_NAMESPACE_NAME::ArgumentParser::ParseArgs ( const int argc,
char ** argv )
inline

This function just converts argc and argv to vector of token.

Parameters
argccount of arguments
argvpointer to array of char*
Returns
ArgumentsObject, which contains valid ArgumentParsed if parsing successful or information about errors if not

Definition at line 1648 of file argparse.h.

◆ ParseArgs() [2/2]

ArgumentsObject ARGPARSE_NAMESPACE_NAME::ArgumentParser::ParseArgs ( const std::vector< std::string > & args)
inline

Main function of parsing argument.

Parameters
argsvector of input tokens
Returns
ArgumentsObject, which contains valid ArgumentParsed if parsing successful or information about errors if not

Definition at line 1361 of file argparse.h.

◆ SetAddHelp()

ArgumentParser & ARGPARSE_NAMESPACE_NAME::ArgumentParser::SetAddHelp ( bool addHelp)
inlinenoexcept

Add a - h / –help option to the parser.

Parameters
addHelpFlag to add or not (true by default)
Returns
reference to current parser

Definition at line 1279 of file argparse.h.

◆ SetAllowAbbrev()

ArgumentParser & ARGPARSE_NAMESPACE_NAME::ArgumentParser::SetAllowAbbrev ( bool allowAbbrev)
inlinenoexcept

Allows long options to be abbreviated if the abbreviation is unambiguous.

Parameters
allowAbbrevbool value true for allow (true by default)
Returns
reference to current parser

Definition at line 1260 of file argparse.h.

◆ SetDescription()

ArgumentParser & ARGPARSE_NAMESPACE_NAME::ArgumentParser::SetDescription ( const std::string & description)
inlinenoexcept

Overload default description for auto-generated command line.

Parameters
descriptionText to display before the argument help ("" by default)
Returns
reference to current parser

Definition at line 1251 of file argparse.h.

◆ SetEpilogue()

ArgumentParser & ARGPARSE_NAMESPACE_NAME::ArgumentParser::SetEpilogue ( const std::string & epilogue)
inlinenoexcept

This function allows to set final message after program description and before argument list description.

Parameters
epiloguestring of epilogue. Automatically adjusting to screen size. (empty by default)
Returns
reference to current parser

Definition at line 1291 of file argparse.h.

◆ SetIgnoreUnknownArgs()

ArgumentParser & ARGPARSE_NAMESPACE_NAME::ArgumentParser::SetIgnoreUnknownArgs ( bool ignoreUnknownArgs)
inlinenoexcept

Setter to ignore unknown argument while parsing.

If false parsing will be failed if parser detected unknown argument.

Parameters
ignoreUnknownArgsbool value for ignore or not (false by default)
Returns
reference to current parser

Definition at line 1270 of file argparse.h.

◆ SetPrefixChars()

ArgumentParser & ARGPARSE_NAMESPACE_NAME::ArgumentParser::SetPrefixChars ( const char charSym)
inlinenoexcept

Function to override default prefix.

Be careful when you choosing prefix. Single prefix will be used for short names of named argument. Double prefix - for long names. Positional arguments have no any prefix

Parameters
charSymcharacter which will be used for prefix. ('-' by default)
Returns

Definition at line 1311 of file argparse.h.

◆ SetUsage()

ArgumentParser & ARGPARSE_NAMESPACE_NAME::ArgumentParser::SetUsage ( const std::string & usage)
inlinenoexcept

Program usage examples.

Parameters
usagethe string describing the program usage. (default: generated from arguments added to parser)
Returns
reference to current parser

Definition at line 1300 of file argparse.h.

◆ TrimTrailingSpacesPerLine()

std::string ARGPARSE_NAMESPACE_NAME::ArgumentParser::TrimTrailingSpacesPerLine ( const std::string & text)
inlinestatic

Removes trailing spaces/tabs from every line of the help text.

The column-based layout leaves padding at the end of many lines; this keeps the rendered help clean without touching the wrapping logic.

Parameters
texthelp text possibly containing trailing whitespace
Returns
text with per-line trailing whitespace removed

Definition at line 1739 of file argparse.h.


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