FE 0.13.1
Header-only C++ frontend library
Loading...
Searching...
No Matches
fe::cli Namespace Reference

A small command-line parser for a single command - no subcommands. More...

Classes

struct  Group
 Starts a new section in the help output; see fe::cli::group. More...
class  Opt
 One option or positional argument; build one with fe::cli::opt, fe::cli::arg, or fe::cli::help. More...
class  Cli
 Holds the Opts, parses argc/argv, and renders the help. More...

Functions

template<class T>
Opt opt (T &target)
 A flag: sets target to true - or invokes it with true - each time it occurs.
template<class T>
Opt opt (T &target, std::string hint)
 An option that takes a value; hint names it in the help as <hint>.
template<class T>
Opt arg (T &target, std::string hint)
 A positional argument; hint names it in the help as <hint>.
Group group (std::string name)
 Opens a section named name that all following Cli options are listed under.
Opt help (bool &target)
 The -h/--help flag; chain ["-?"] to give it further names.

Detailed Description

A small command-line parser for a single command - no subcommands.

Declare the switches by piping fe::cli::opt / fe::cli::arg into an fe::cli::Cli and bind each one to a variable:

bool show_help = false, verbose = false;
std::string out, in;
std::vector<std::string> plugins;
auto cli = fe::cli::Cli("mim", "The MimIR compiler.")
| fe::cli::help(show_help)
| fe::cli::opt(verbose )["-V"]["--verbose"]("Be verbose.")
| fe::cli::opt(plugins, "plugin" )["-p"]["--plugin" ]("Loads a plugin; repeatable.")
| fe::cli::group("Output")
| fe::cli::opt(out, "file" )["-o"]["--output" ]("Where to write the result.")
| fe::cli::arg(in, "file" ) ("Input file.");
if (auto err = cli.parse(argc, argv)) throw std::invalid_argument(*err);
if (show_help) std::cout << cli;
Holds the Opts, parses argc/argv, and renders the help.
Definition cli.h:196
A small command-line parser for a single command - no subcommands.
Definition cli.h:45
Group group(std::string name)
Opens a section named name that all following Cli options are listed under.
Definition cli.h:60
Opt help(bool &target)
The -h/--help flag; chain ["-?"] to give it further names.
Definition cli.h:193
Opt opt(T &)
A flag: sets target to true - or invokes it with true - each time it occurs.
Definition cli.h:159
Opt arg(T &, std::string)
A positional argument; hint names it in the help as <hint>.
Definition cli.h:186

A target may be a bool (for a flag), a std::string, an integral type, a std::vector of those, or a callable - invoked with true for a flag and with the std::string value otherwise. Such a callable may return a std::string to reject that value; a non-empty one becomes the error of Cli::parse. An fe::cli::opt without a hint is a flag; one with a hint takes a value.

The parser understands --name value, --name=value, -n value, -nvalue, clustered short flags (-abc), and -- to end option processing.

Cli::help lays the switches out for a terminal - wrapped to its width and colored via fe::term - whereas Cli::markdown renders the same information as Markdown tables; fe::cli::group splits both into sections.


Class Documentation

◆ fe::cli::Group

struct fe::cli::Group

Starts a new section in the help output; see fe::cli::group.

Definition at line 55 of file cli.h.

Class Members
string name

Function Documentation

◆ arg()

template<class T>
Opt fe::cli::arg ( T & target,
std::string hint )

A positional argument; hint names it in the help as <hint>.

Bind a std::vector to soak up all remaining ones.

Definition at line 186 of file cli.h.

◆ group()

Group fe::cli::group ( std::string name)
inline

Opens a section named name that all following Cli options are listed under.

Definition at line 60 of file cli.h.

References group().

Referenced by group().

◆ help()

Opt fe::cli::help ( bool & target)
inline

The -h/--help flag; chain ["-?"] to give it further names.

Definition at line 193 of file cli.h.

References help(), and opt().

Referenced by help().

◆ opt() [1/2]

template<class T>
Opt fe::cli::opt ( T & target)

A flag: sets target to true - or invokes it with true - each time it occurs.

Definition at line 159 of file cli.h.

Referenced by help().

◆ opt() [2/2]

template<class T>
Opt fe::cli::opt ( T & target,
std::string hint )

An option that takes a value; hint names it in the help as <hint>.

Definition at line 173 of file cli.h.