YesChief!
C++ argv parser
Loading...
Searching...
No Matches
yeschief::CLI Class Referencefinal

#include <yeschief.h>

Public Member Functions

 CLI (std::string name, std::string description)
 
auto addCommand (Command *command) -> CLI &
 
auto addGroup (const std::string &name) -> OptionGroup &
 
template<typename T = bool>
auto addOption (const std::string &name, const std::string &description, const OptionConfiguration &configuration={}) -> CLI &
 
auto help (std::ostream &out=std::cout) const -> void
 
template<typename... Tail>
auto parsePositional (const std::string &option_name, Tail &&...options) -> void
 
auto run (int argc, char **argv) const -> std::expected< CLIResults, Fault >
 

Detailed Description

Main class of the library. It represents the program itself and manage options and commands

Constructor & Destructor Documentation

◆ CLI()

yeschief::CLI::CLI ( std::string name,
std::string description )
Parameters
nameName of your program
descriptionDescription of your program

Member Function Documentation

◆ addCommand()

auto yeschief::CLI::addCommand ( Command * command) -> CLI &

Add a command to your program

The added command must be a child class of Command. The object passed in parameters will be the one used when launching CLI::run

Parameters
commandThe Command instance
Returns
The CLI object itself to chain calls

◆ addGroup()

auto yeschief::CLI::addGroup ( const std::string & name) -> OptionGroup &
nodiscard

Add a group of options to your program. You can then add options to your group the exact same way you add them to the current class

Parameters
nameName of the group
Returns
The created OptionGroup, you can then customize it

◆ addOption()

template<typename T>
auto yeschief::CLI::addOption ( const std::string & name,
const std::string & description,
const OptionConfiguration & configuration = {} ) -> CLI &

Add an option to your program.

The name of the option can be written in 2 ways:

  • "name" -> for --name
  • "name,n" -> for --name and -n

For the second way, the format should always be <long>,<short>. For the long name you can have any length but the short name must be only 1 letter

Parameters
nameName of the option
descriptionDescription of the option
configurationAdvanced configuration
Returns
The CLI object itself to chain calls

◆ help()

auto yeschief::CLI::help ( std::ostream & out = std::cout) const -> void

Display a help message built from defined options or commands with this template:

usage:
<program name> [OPTIONS] <REQUIRED OPTIONS> <POSITIONAL ARGUMENTS...> <- if options
<program name> [COMMAND] [OPTIONS] <- if commands
<program description>
Positional arguments: <- if options and positional arguments not empty
These arguments come after options and in the order they are listed here.
Only <REQUIRED POSITIONAL ARGUMENTS> is required.
<LIST OF POSITIONAL ARGUMENT>
<DESCRIPTION OF EACH ARGUMENT>
<Option group name>: <- if options (if there is no option group, the group will be named "Options")
<List of options> [REQUIRED]
<Description of each option>
--help
Display this help message
Commands: <- if commands
<List of commands usage>
<Description of each command>
help [COMMAND]
Display this help message. If command is specified, display help for this command

To see example of outputs, please refer to the usage documentation: Usage documentation

Parameters
outOut stream on which help message is printed (default to console output STDOUT)

◆ parsePositional()

template<typename... Tail>
auto yeschief::CLI::parsePositional ( const std::string & option_name,
Tail &&... options ) -> void

Allow to set some options as positional arguments. This way the user will no longer need to use option name to set its value

Please note that if an option is repeatable it must be placed as the last one

Parameters
option_name
optionsList of options names to parse as positional arguments

◆ run()

auto yeschief::CLI::run ( int argc,
char ** argv ) const -> std::expected< CLIResults, Fault >

Parse argv against defined options and commands.

  • In case you specified options, it will return a CLIResult instance with values associated to each option
  • In case you specified commands, it will launch run method of corresponding command with a CLIResult instance with values associated to each options of the command. If the command have sub-commands, it will chain the calls recursively

In both case if something went wrong during argv parsing, the method will return a Fault with the corresponding message

Parameters
argcCount of argument
argvArray of argument
Returns
The result of the parsing or a Fault if something went wrong

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