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;
|
fe::cli::opt(plugins,
"plugin" )[
"-p"][
"--plugin" ](
"Loads a plugin; repeatable.")
|
fe::cli::opt(out,
"file" )[
"-o"][
"--output" ](
"Where to write the result.")
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.
A small command-line parser for a single command - no subcommands.
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.
Opt opt(T &)
A flag: sets target to true - or invokes it with true - each time it occurs.
Opt arg(T &, std::string)
A positional argument; hint names it in the help as <hint>.
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.