Configurable command-line interface. More...
#include <cli.h>
This class is experimental.
Default commands: help, get/set, quit, exit, '!', verbose, license, warranty, alias, run.
Note that if the shell command is allowed (as it is by default) there are some potential security issues which are not solved here.
Commands which begin with a '#' character are ignored.
Warn in run_interactive() when extra parameters are given
Include a "remove command" function
A replace command function, there's already some code in cli.cpp for this.
There's some code duplication between comm_option_run() and run_interactive()
Allow the user to set the tilde string
Disallow direct access to o2scl::cli::par_list in order to ensure parameter names do not contain whitespace
Concepts
As a matter of definition, the command-line arguments are simply called arguments. These arguments may be options (in which case they begin with either one dash or two) or parameters to these options. When run in interactive mode, these options are also commands.
Classes | |
class | parameter |
Parameter for o2scl::cli. More... | |
class | parameter_bool |
String parameter for o2scl::cli. More... | |
class | parameter_double |
Double parameter for o2scl::cli. More... | |
class | parameter_int |
Integer parameter for o2scl::cli. More... | |
class | parameter_size_t |
Integer parameter for o2scl::cli. More... | |
class | parameter_string |
String parameter for o2scl::cli. More... | |
Public Member Functions | |
int | set_function (comm_option_funct &usf) |
Function to call when a set command is issued. | |
virtual char * | cli_gets (const char *c) |
The function which obtains input from the user. More... | |
int | call_args (std::vector< cmd_line_arg > &ca, int debug=0) |
Call functions corresponding to command-line args. | |
int | process_args (int argc, char *argv[], std::vector< cmd_line_arg > &ca, int debug=0, bool also_call_args=false) |
Process command-line arguments from a const char array. More... | |
int | process_args (std::vector< std::string > &sv, std::vector< cmd_line_arg > &ca, int debug) |
Process command-line arguments from a vector of strings. More... | |
int | process_args (std::string s, std::vector< cmd_line_arg > &ca, int debug=0, bool also_call_args=false) |
Process command-line arguments from a string. More... | |
int | set_verbose (int v) |
Set verbosity. More... | |
int | run_interactive () |
Run the interactive mode. | |
int | set_alias (std::string alias, std::string str) |
Set an alias alias for the string str . More... | |
std::string | get_alias (std::string alias) |
Set an alias alias for the string str . More... | |
The hard-coded command functions | |
int | comm_option_alias (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_commands (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_get (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_help (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_license (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_no_intro (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_run (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_set (std::vector< std::string > &sv, bool itive_com) |
int | comm_option_warranty (std::vector< std::string > &sv, bool itive_com) |
Basic operation | |
int | set_comm_option (comm_option_s &ic) |
Add a new command. More... | |
void | remove_comm_option (std::string cmd) |
Remove a command with long name cmd . | |
template<class vec_t > | |
int | set_comm_option_vec (size_t list_size, vec_t &option_list) |
Add a vector containing new commands/options. | |
int | set_param_help (std::string param, std::string help) |
Set one-line help text for a parameter named param . | |
int | run_auto (int argc, char *argv[], int debug=0) |
Automatically parse arguments to main and call interactive mode if required. | |
Public Attributes | |
std::string | tilde_string |
String to replace tildes with. | |
bool | gnu_intro |
If true, output the usual GNU intro when run_interactive() is called (default true). More... | |
bool | sync_verbose |
If true, then sync cli::verbose, with a parameter of the same name. | |
bool | shell_cmd_allowed |
If true, allow the user to use ! to execute a shell command (default true) | |
std::string | prompt |
The prompt (default "> " ) | |
std::string | desc |
A one- or two-line description (default is empty string) | |
std::string | cmd_name |
The name of the command. | |
std::string | addl_help_cmd |
Additional help text for interactive mode (default is empty string) | |
std::string | addl_help_cli |
Additional help text for command-line (default is empty string) | |
The default command objects | |
comm_option_s | c_commands |
comm_option_s | c_help |
comm_option_s | c_quit |
comm_option_s | c_exit |
comm_option_s | c_license |
comm_option_s | c_warranty |
comm_option_s | c_set |
comm_option_s | c_get |
comm_option_s | c_run |
comm_option_s | c_no_intro |
comm_option_s | c_alias |
Protected Member Functions | |
int | output_param_list () |
Output the parameter list. | |
int | expand_tilde (std::vector< std::string > &sv) |
Attempt to expand a tilde to a user's home directory. More... | |
int | apply_alias (std::vector< std::string > &sv, std::string sold, std::string snew) |
Replace all occurences of sold with snew in sv . | |
bool | string_equal_dash (std::string s1, std::string s2) |
Compare two strings, treating dashes and underscores as equivalent. | |
Protected Attributes | |
int | verbose |
Control screen output. | |
char | buf [300] |
Storage for getline. | |
comm_option_funct * | user_set_func |
Storage for the function to call after setting a parameter. | |
std::vector< comm_option_s > | clist |
List of commands. | |
Help for parameters | |
std::vector< std::string > | ph_name |
std::vector< std::string > | ph_desc |
Parameter storage and associated iterator type | |
typedef std::map< std::string, parameter *, std::greater< std::string > >::iterator | par_t |
List iterator. | |
std::map< std::string, parameter *, std::less< std::string > > | par_list |
Parameter list. | |
Aliases | |
typedef std::map< std::string, std::string, std::greater< std::string > >::iterator | al_it |
std::map< std::string, std::string, std::greater< std::string > > | als |
|
virtual |
Reimplemented in o2scl::cli_readline.
|
protected |
Experimental and currently unused.
std::string o2scl::cli::get_alias | ( | std::string | alias | ) |
Aliases can also be set using the command 'alias'
, but that version allows only one-word aliases.
int o2scl::cli::process_args | ( | int | argc, |
char * | argv[], | ||
std::vector< cmd_line_arg > & | ca, | ||
int | debug = 0 , |
||
bool | also_call_args = false |
||
) |
This doesn't actually execute the functions for the corresponding options, but simply processes the parameters argv
and argv
and packs the information into ca
.
This function assumes that argc[0]
just contains the name of the command, and should thus be ignored.
int o2scl::cli::process_args | ( | std::vector< std::string > & | sv, |
std::vector< cmd_line_arg > & | ca, | ||
int | debug | ||
) |
This doesn't actually execute the functions for the corresponding options, but simply processes the arguments in sv
and packs the information into ca
.
int o2scl::cli::process_args | ( | std::string | s, |
std::vector< cmd_line_arg > & | ca, | ||
int | debug = 0 , |
||
bool | also_call_args = false |
||
) |
int o2scl::cli::set_alias | ( | std::string | alias, |
std::string | str | ||
) |
Aliases can also be set using the command 'alias'
, but that version allows only one-word aliases.
int o2scl::cli::set_comm_option | ( | comm_option_s & | ic | ) |
Each command/option must have either a short form in comm_option_s::shrt or a long from in comm_option_s::lng, which is unique from the other commands/options already present. You cannot add two commands/options with the same short form, even if they have different long forms, and vice versa.
int o2scl::cli::set_verbose | ( | int | v | ) |
Most errors are output to the screen even if verbose is zero.
bool o2scl::cli::gnu_intro |
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).