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

Command-line parser with flags, options, positionals, subcommands and generated help. More...

#include <RST/parser/ArgParser.hpp>

Public Member Functions

 ArgParser (std::string programName="", std::string description="")
 Creates a parser; programName defaults to the executable name.
 
Argument & addFlag (std::vector< std::string > flags, std::string description)
 Adds a flag without value; short flags can be bundled, like -abc.
 
Argument & addOption (std::vector< std::string > flags, std::string description)
 Adds an option taking a value: --name value, --name=value, -n value or -n5.
 
Argument & addPositional (std::string name, std::string description)
 Adds a positional argument; positionals are filled in declaration order.
 
ArgParser & addSubcommand (std::string name, std::string description)
 Adds a subcommand with its own arguments, and returns its parser.
 
void setVersion (std::string version)
 Enables --version, which prints the program name and version.
 
void setEpilog (std::string text)
 Text printed at the end of the help.
 
bool loadConfig (const std::filesystem::path &file)
 Loads fallback values from a file of name = value lines, name being a flag without dashes.
 
ParseResult parse (int argc, const char *const *argv)
 Parses the arguments of main.
 
ParseResult parse (const std::vector< std::string > &args)
 Parses args, without the program name.
 
bool isSet (std::string_view name) const
 Shortcut for Argument::isSet(); name is a flag (-v, --verbose) or a name (verbose).
 
std::size_t count (std::string_view name) const
 Shortcut for Argument::count().
 
template<typename T = std::string>
std::optional< T > get (std::string_view name) const
 Shortcut for Argument::get().
 
template<typename T = std::string>
std::vector< T > getAll (std::string_view name) const
 Shortcut for Argument::getAll().
 
const std::string & activeSubcommand () const noexcept
 Name of the subcommand given on the command line, or an empty string.
 
const ArgParser * activeSubparser () const
 Parser of the active subcommand, or nullptr.
 
std::string help () const
 The generated help text.
 
void printHelp () const
 Prints help() to stdout.
 

Detailed Description

Command-line parser with flags, options, positionals, subcommands and generated help.

-h/--help and --version are built in. Values not given on the command line fall back on their environment variable, then the config file, then their default value.

RST::Parser::ArgParser parser("app", "Does useful things.");
parser.addFlag({"-v", "--verbose"}, "Print more details");
parser.addOption({"-n", "--count"}, "How many times").integer().defaultValue(1);
if (auto result = parser.parse(argc, argv); !result) {
result.print();
return result.exitCode();
}
int count = parser.get<int>("--count").value_or(1);
Command-line parser with flags, options, positionals, subcommands and generated help.
Definition ArgParser.hpp:34
std::size_t count(std::string_view name) const
Shortcut for Argument::count().
Definition ArgParser.hpp:69
Examples
parser.cpp, and quickstart.cpp.

Constructor & Destructor Documentation

◆ ArgParser()

RST::Parser::ArgParser::ArgParser ( std::string  programName = "",
std::string  description = "" 
)
explicit

Creates a parser; programName defaults to the executable name.

Member Function Documentation

◆ addFlag()

Argument & RST::Parser::ArgParser::addFlag ( std::vector< std::string >  flags,
std::string  description 
)

Adds a flag without value; short flags can be bundled, like -abc.

Examples
parser.cpp, and quickstart.cpp.

◆ addOption()

Argument & RST::Parser::ArgParser::addOption ( std::vector< std::string >  flags,
std::string  description 
)

Adds an option taking a value: --name value, --name=value, -n value or -n5.

Examples
parser.cpp, and quickstart.cpp.

◆ addPositional()

Argument & RST::Parser::ArgParser::addPositional ( std::string  name,
std::string  description 
)

Adds a positional argument; positionals are filled in declaration order.

Examples
parser.cpp.

◆ addSubcommand()

ArgParser & RST::Parser::ArgParser::addSubcommand ( std::string  name,
std::string  description 
)

Adds a subcommand with its own arguments, and returns its parser.

Examples
parser.cpp.

◆ setVersion()

void RST::Parser::ArgParser::setVersion ( std::string  version)
inline

Enables --version, which prints the program name and version.

Examples
parser.cpp.

◆ setEpilog()

void RST::Parser::ArgParser::setEpilog ( std::string  text)
inline

Text printed at the end of the help.

◆ loadConfig()

bool RST::Parser::ArgParser::loadConfig ( const std::filesystem::path &  file)

Loads fallback values from a file of name = value lines, name being a flag without dashes.

Lines starting with # or ; are comments. Returns false if the file cannot be read.

◆ parse() [1/2]

ParseResult RST::Parser::ArgParser::parse ( int  argc,
const char *const *  argv 
)

Parses the arguments of main.

Examples
parser.cpp, and quickstart.cpp.

◆ parse() [2/2]

ParseResult RST::Parser::ArgParser::parse ( const std::vector< std::string > &  args)

Parses args, without the program name.

◆ isSet()

bool RST::Parser::ArgParser::isSet ( std::string_view  name) const
inline

Shortcut for Argument::isSet(); name is a flag (-v, --verbose) or a name (verbose).

Examples
parser.cpp, and quickstart.cpp.

◆ count()

std::size_t RST::Parser::ArgParser::count ( std::string_view  name) const
inline

Shortcut for Argument::count().

◆ get()

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

Shortcut for Argument::get().

Examples
parser.cpp, and quickstart.cpp.

◆ getAll()

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

Shortcut for Argument::getAll().

Examples
parser.cpp.

◆ activeSubcommand()

const std::string & RST::Parser::ArgParser::activeSubcommand ( ) const
inlinenoexcept

Name of the subcommand given on the command line, or an empty string.

Examples
parser.cpp.

◆ activeSubparser()

const ArgParser * RST::Parser::ArgParser::activeSubparser ( ) const

Parser of the active subcommand, or nullptr.

◆ help()

std::string RST::Parser::ArgParser::help ( ) const

The generated help text.

◆ printHelp()

void RST::Parser::ArgParser::printHelp ( ) const

Prints help() to stdout.

Examples
parser.cpp.

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