args  6.2.0
A simple single-header C++11 STL-only argument parser library
Public Member Functions | Public Attributes | Protected Types | Protected Member Functions | Friends | List of all members
args::ArgumentParser Class Reference

The main user facing command line argument parser class. More...

#include <args.hxx>

Inheritance diagram for args::ArgumentParser:
args::Command args::Group args::Base

Public Member Functions

 ArgumentParser (const std::string &description_, const std::string &epilog_=std::string())
 
void AddCompletion (CompletionFlag &completionFlag)
 
const std::string & Prog () const
 The program name for help generation.
 
void Prog (const std::string &prog_)
 The program name for help generation.
 
const std::string & LongPrefix () const
 The prefix for long flags.
 
void LongPrefix (const std::string &longprefix_)
 The prefix for long flags.
 
const std::string & ShortPrefix () const
 The prefix for short flags.
 
void ShortPrefix (const std::string &shortprefix_)
 The prefix for short flags.
 
const std::string & LongSeparator () const
 The separator for long flags.
 
void LongSeparator (const std::string &longseparator_)
 The separator for long flags.
 
const std::string & Terminator () const
 The terminator that forcibly separates flags from positionals.
 
void Terminator (const std::string &terminator_)
 The terminator that forcibly separates flags from positionals.
 
void GetArgumentSeparations (bool &allowJoinedShortValue_, bool &allowJoinedLongValue_, bool &allowSeparateShortValue_, bool &allowSeparateLongValue_) const
 Get the current argument separation parameters. More...
 
void SetArgumentSeparations (const bool allowJoinedShortValue_, const bool allowJoinedLongValue_, const bool allowSeparateShortValue_, const bool allowSeparateLongValue_)
 Change allowed option separation. More...
 
void Help (std::ostream &help_) const
 Pass the help menu into an ostream.
 
std::string Help () const
 Generate a help menu as a string. More...
 
virtual void Reset () noexcept override
 
template<typename It >
It ParseArgs (It begin, It end)
 Parse all arguments. More...
 
template<typename T >
auto ParseArgs (const T &args) -> decltype(std::begin(args))
 Parse all arguments. More...
 
bool ParseCLI (const int argc, const char *const *argv)
 Convenience function to parse the CLI from argc and argv. More...
 
template<typename T >
bool ParseCLI (const T &args)
 
- Public Member Functions inherited from args::Command
 Command (Group &base_, std::string name_, std::string help_, std::function< void(Subparser &)> coroutine_={})
 
const std::string & ProglinePostfix () const
 The description that appears on the prog line after options.
 
void ProglinePostfix (const std::string &proglinePostfix_)
 The description that appears on the prog line after options.
 
const std::string & Description () const
 The description that appears above options.
 
void Description (const std::string &description_)
 The description that appears above options.
 
const std::string & Epilog () const
 The description that appears below options.
 
void Epilog (const std::string &epilog_)
 The description that appears below options.
 
const std::string & Name () const
 The name of command.
 
const std::string & Help () const
 The description of command.
 
void RequireCommand (bool value)
 If value is true, parser will fail if no command was parsed. More...
 
virtual bool IsGroup () const override
 
virtual bool Matched () const noexcept override
 Whether or not this group matches validation.
 
 operator bool () const noexcept
 
void Match () noexcept
 
void SelectCommand (Command *c) noexcept
 
virtual FlagBaseMatch (const EitherFlag &flag) override
 Return the first FlagBase that matches flag, or nullptr. More...
 
virtual std::vector< FlagBase * > GetAllFlags () override
 
virtual PositionalBaseGetNextPositional () override
 Get the next ready positional, or nullptr if there is none. More...
 
virtual bool HasFlag () const override
 Get whether this has any FlagBase children. More...
 
virtual bool HasPositional () const override
 Get whether this has any PositionalBase children. More...
 
virtual bool HasCommand () const override
 Get whether this has any Command children. More...
 
std::vector< std::string > GetCommandProgramLine (const HelpParams &params) const
 
virtual std::vector< std::string > GetProgramLine (const HelpParams &params) const override
 Get the names of positional parameters.
 
virtual std::vector< Command * > GetCommands () override
 
virtual std::vector< std::tuple< std::string, std::string, unsigned > > GetDescription (const HelpParams &params, const unsigned int indent) const override
 Get all the child descriptions for help generation.
 
virtual void Validate (const std::string &shortprefix, const std::string &longprefix) const override
 
- Public Member Functions inherited from args::Group
 Group (const std::string &help_=std::string(), const std::function< bool(const Group &)> &validator_=Validators::DontCare, Options options_={})
 If help is empty, this group will not be printed in help output.
 
 Group (Group &group_, const std::string &help_=std::string(), const std::function< bool(const Group &)> &validator_=Validators::DontCare, Options options_={})
 If help is empty, this group will not be printed in help output.
 
void Add (Base &child)
 Append a child to this Group.
 
const std::vector< Base * > & Children () const
 Get all this group's children.
 
std::vector< Base * >::size_type MatchedChildren () const
 Count the number of matched children this group has.
 
bool Get () const
 Get validation.
 
- Public Member Functions inherited from args::Base
 Base (const std::string &help_, Options options_={})
 
Options GetOptions () const noexcept
 
bool IsRequired () const noexcept
 
 operator bool () const noexcept
 
virtual std::vector< std::tuple< std::string, std::string, unsigned > > GetDescription (const HelpParams &, const unsigned indentLevel) const
 
void KickOut (bool kickout_) noexcept
 Sets a kick-out value for building subparsers.
 
bool KickOut () const noexcept
 Gets the kick-out value for building subparsers.
 

Public Attributes

HelpParams helpParams
 

Protected Types

enum class  OptionType { LongFlag , ShortFlag , Positional }
 

Protected Member Functions

OptionType ParseOption (const std::string &s, bool allowEmpty=false)
 
template<typename It >
bool Complete (FlagBase &flag, It it, It end)
 
template<typename It >
std::string ParseArgsValues (FlagBase &flag, const std::string &arg, It &it, It end, const bool allowSeparate, const bool allowJoined, const bool hasJoined, const std::string &joinedArg, const bool canDiscardJoined, std::vector< std::string > &values)
 (INTERNAL) Parse flag's values More...
 
template<typename It >
bool ParseLong (It &it, It end)
 
template<typename It >
bool ParseShort (It &it, It end)
 
bool AddCompletionReply (const std::string &cur, const std::string &choice)
 
template<typename It >
bool Complete (It it, It end)
 
template<typename It >
It Parse (It begin, It end)
 
- Protected Member Functions inherited from args::Command
std::function< void(Subparser &)> & GetCoroutine ()
 
CommandSelectedCommand ()
 
const CommandSelectedCommand () const
 
void UpdateSubparserHelp (const HelpParams &params) const
 

Friends

class Subparser
 

Additional Inherited Members

- Protected Attributes inherited from args::Base
bool matched = false
 
const std::string help
 

Detailed Description

The main user facing command line argument parser class.

Member Function Documentation

◆ GetArgumentSeparations()

void args::ArgumentParser::GetArgumentSeparations ( bool &  allowJoinedShortValue_,
bool &  allowJoinedLongValue_,
bool &  allowSeparateShortValue_,
bool &  allowSeparateLongValue_ 
) const
inline

Get the current argument separation parameters.

See SetArgumentSeparations for details on what each one means.

◆ Help()

std::string args::ArgumentParser::Help ( ) const
inline

Generate a help menu as a string.

Returns
the help text as a single string

◆ ParseArgs() [1/2]

template<typename T >
auto args::ArgumentParser::ParseArgs ( const T &  args) -> decltype(std::begin(args))
inline

Parse all arguments.

Parameters
argsan iterable of the arguments
Returns
the iterator after the last parsed value. Only useful for kick-out

◆ ParseArgs() [2/2]

template<typename It >
It args::ArgumentParser::ParseArgs ( It  begin,
It  end 
)
inline

Parse all arguments.

Parameters
beginan iterator to the beginning of the argument list
endan iterator to the past-the-end element of the argument list
Returns
the iterator after the last parsed value. Only useful for kick-out

◆ ParseArgsValues()

template<typename It >
std::string args::ArgumentParser::ParseArgsValues ( FlagBase flag,
const std::string &  arg,
It &  it,
It  end,
const bool  allowSeparate,
const bool  allowJoined,
const bool  hasJoined,
const std::string &  joinedArg,
const bool  canDiscardJoined,
std::vector< std::string > &  values 
)
inlineprotected

(INTERNAL) Parse flag's values

Parameters
argThe string to display in error message as a flag name
[in,out]itThe iterator to first value. It will point to the last value
endThe end iterator
joinedArgJoined value (e.g. bar in –foo=bar)
canDiscardJoinedIf true joined value can be parsed as flag not as a value (as in -abcd)
[out]valuesThe vector to store parsed arg's values

◆ ParseCLI()

bool args::ArgumentParser::ParseCLI ( const int  argc,
const char *const *  argv 
)
inline

Convenience function to parse the CLI from argc and argv.

Just assigns the program name and vectorizes arguments for passing into ParseArgs()

Returns
whether or not all arguments were parsed. This works for detecting kick-out, but is generally useless as it can't do anything with it.

◆ SetArgumentSeparations()

void args::ArgumentParser::SetArgumentSeparations ( const bool  allowJoinedShortValue_,
const bool  allowJoinedLongValue_,
const bool  allowSeparateShortValue_,
const bool  allowSeparateLongValue_ 
)
inline

Change allowed option separation.

Parameters
allowJoinedShortValue_Allow a short flag that accepts an argument to be passed its argument immediately next to it (ie. in the same argv field)
allowJoinedLongValue_Allow a long flag that accepts an argument to be passed its argument separated by the longseparator (ie. in the same argv field)
allowSeparateShortValue_Allow a short flag that accepts an argument to be passed its argument separated by whitespace (ie. in the next argv field)
allowSeparateLongValue_Allow a long flag that accepts an argument to be passed its argument separated by whitespace (ie. in the next argv field)

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