FE 0.14.0
A C++23 toolkit for writing compiler/interpreter frontends.
Loading...
Searching...
No Matches

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

#include <fe/cli.h>

Public Types

using Row = std::pair<std::string, std::string>
using Rows = std::vector<Row>

Public Member Functions

 Cli ()=default
 Cli (std::string prog, std::string descr={})
std::optional< std::string > parse (int argc, const char *const *argv)
 Parses argc/argv; returns the error message - and nothing at all if all went well.
void help (std::ostream &) const
 Renders the help for a terminal.
void markdown (std::ostream &) const
 Renders the same information as Doxygen-flavored Markdown tables.
Declare Options and Arguments
template<class T>
Cliopt (T &target, std::string hint={}, std::string sname={}, std::string lname={}, std::string descr={})
 An option bound to target and named sname and/or lname - "-o" and "--output".
Cliopt (bool &target, std::string sname={}, std::string lname={}, std::string descr={})
template<class T>
Cliarg (T &target, std::string hint, std::string descr={})
 A positional argument; hint names it in the help as <hint>.
Clihelp (bool &target, std::string sname="-h", std::string lname="--help")
 The help flag - named -h/--help unless sname / lname say otherwise.
Clicardinality (size_t min, size_t max)
 The Cli::opt / Cli::arg declared last must occur at least min and at most max times.
Cligrp (std::string name)
 Opens a section named name that all following options are listed under.
Clisection (std::string title, std::string head={}, Rows rows={})
 A titled table of term/description rows that are not options - ENVIRONMENT, plugin arguments, ... Both backends render it below the options; head names the first column in Cli::markdown.
Cliepilog (std::string s)
 Text printed below the option list.

Friends

std::ostream & operator<< (std::ostream &os, const Cli &cli)

Detailed Description

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

Declare the switches with Cli::opt / Cli::arg 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("mim", "The MimIR compiler.")
.help(show_help)
.opt(verbose, "-V", "--verbose", "Be verbose.")
.opt(plugins, "plugin", "-p", "--plugin" , "Loads a plugin; repeatable.")
.grp("Output")
.opt(out , "file" , "-o", "--output" , "Where to write the result.")
.arg(in , "file" , "Input file.");
if (auto err = cli.parse(argc, argv)) throw std::invalid_argument(*err);
if (show_help) std::cout << cli;
A small command-line parser for a single command - no subcommands.
Definition cli.h:51
Cli & help(bool &target, std::string sname="-h", std::string lname="--help")
The help flag - named -h/--help unless sname / lname say otherwise.
Definition cli.h:94
Cli & arg(T &target, std::string hint, std::string descr={})
A positional argument; hint names it in the help as <hint>.
Definition cli.h:88
Cli & opt(T &target, std::string hint={}, std::string sname={}, std::string lname={}, std::string descr={})
An option bound to target and named sname and/or lname - "-o" and "--output".
Definition cli.h:66
Cli & grp(std::string name)
Opens a section named name that all following options are listed under.
Definition cli.h:105

A bool target - or a callable taking one - is a flag: it is set each time it occurs; a bool takes no hint at all, a callable an empty one. Any other one needs a hint, listed as <hint> in the help, and is assigned the value: a std::string, an integral type, a std::vector of those, or a callable, which may return a std::string to reject the value - a non-empty one becomes the error of Cli::parse.

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; Cli::grp splits both into sections. A description may cite code as `this` : Cli::help colors it like CodeDiag does, Cli::markdown turns it into a code span.

Definition at line 51 of file cli.h.

Member Typedef Documentation

◆ Row

using fe::Cli::Row = std::pair<std::string, std::string>

Definition at line 53 of file cli.h.

◆ Rows

using fe::Cli::Rows = std::vector<Row>

Definition at line 54 of file cli.h.

Constructor & Destructor Documentation

◆ Cli() [1/2]

fe::Cli::Cli ( )
default

◆ Cli() [2/2]

fe::Cli::Cli ( std::string prog,
std::string descr = {} )
inline

Definition at line 57 of file cli.h.

Member Function Documentation

◆ arg()

template<class T>
Cli & fe::Cli::arg ( T & target,
std::string hint,
std::string descr = {} )
inline

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

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

Definition at line 88 of file cli.h.

References Cli().

◆ cardinality()

Cli & fe::Cli::cardinality ( size_t min,
size_t max )
inline

The Cli::opt / Cli::arg declared last must occur at least min and at most max times.

Definition at line 99 of file cli.h.

References Cli().

◆ epilog()

Cli & fe::Cli::epilog ( std::string s)
inline

Text printed below the option list.

Definition at line 115 of file cli.h.

References Cli().

◆ grp()

Cli & fe::Cli::grp ( std::string name)
inline

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

Definition at line 105 of file cli.h.

References Cli().

◆ help() [1/2]

Cli & fe::Cli::help ( bool & target,
std::string sname = "-h",
std::string lname = "--help" )
inline

The help flag - named -h/--help unless sname / lname say otherwise.

Definition at line 94 of file cli.h.

References Cli(), and opt().

Referenced by operator<<.

◆ help() [2/2]

void fe::Cli::help ( std::ostream & ) const

Renders the help for a terminal.

◆ markdown()

void fe::Cli::markdown ( std::ostream & ) const

Renders the same information as Doxygen-flavored Markdown tables.

◆ opt() [1/2]

Cli & fe::Cli::opt ( bool & target,
std::string sname = {},
std::string lname = {},
std::string descr = {} )
inline

Definition at line 81 of file cli.h.

References Cli().

◆ opt() [2/2]

template<class T>
Cli & fe::Cli::opt ( T & target,
std::string hint = {},
std::string sname = {},
std::string lname = {},
std::string descr = {} )
inline

An option bound to target and named sname and/or lname - "-o" and "--output".

Definition at line 66 of file cli.h.

References Cli().

Referenced by help().

◆ parse()

std::optional< std::string > fe::Cli::parse ( int argc,
const char *const * argv )

Parses argc/argv; returns the error message - and nothing at all if all went well.

◆ section()

Cli & fe::Cli::section ( std::string title,
std::string head = {},
Rows rows = {} )
inline

A titled table of term/description rows that are not options - ENVIRONMENT, plugin arguments, ... Both backends render it below the options; head names the first column in Cli::markdown.

Pass no rows to get a bare header that groups the sections below it - one level up in Cli::markdown.

Definition at line 110 of file cli.h.

References Cli().

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Cli & cli )
friend

Definition at line 207 of file cli.h.

References Cli(), and help().


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