|
| | 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.
|
| template<class T> |
| 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".
|
| Cli & | opt (bool &target, std::string sname={}, std::string lname={}, std::string descr={}) |
| template<class T> |
| Cli & | arg (T &target, std::string hint, std::string descr={}) |
| | A positional argument; hint names it in the help as <hint>.
|
| Cli & | help (bool &target, std::string sname="-h", std::string lname="--help") |
| | The help flag - named -h/--help unless sname / lname say otherwise.
|
| Cli & | cardinality (size_t min, size_t max) |
| | The Cli::opt / Cli::arg declared last must occur at least min and at most max times.
|
| Cli & | grp (std::string name) |
| | Opens a section named name that all following options are listed under.
|
| Cli & | section (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.
|
| Cli & | epilog (std::string s) |
| | Text printed below the option list.
|
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.")
.
opt(verbose,
"-V",
"--verbose",
"Be verbose.")
.
opt(plugins,
"plugin",
"-p",
"--plugin" ,
"Loads a plugin; repeatable.")
.
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.
Cli & help(bool &target, std::string sname="-h", std::string lname="--help")
The help flag - named -h/--help unless sname / lname say otherwise.
Cli & arg(T &target, std::string hint, std::string descr={})
A positional argument; hint names it in the help as <hint>.
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".
Cli & grp(std::string name)
Opens a section named name that all following options are listed under.
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.