Class cli (o2scl)¶
-
class
o2scl
::
cli
¶ Configurable command-line interface.
This class is experimental.
Default commands: help, get, set, quit, exit, license, no-intro, commands, warranty, alias, run, shell, and ‘!’.
Note that if the shell command is allowed (as it is by default) there are some potential security issues which are not solved here.
Interactive commands which begin with a ‘#’ character are ignored.
- Idea for Future:
Warn in run_interactive() when extra parameters are given.
- Idea for Future:
A replace command function, there’s already some code in cli.cpp for this.
- Idea for Future:
There’s some code duplication between comm_option_run() and run_interactive().
- Note
In interactive mode, commands are limited to 300 characters, but this limit does not apply when readline is used.
- Idea for Future:
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. They are separated in commands (which begin with either one dash or two) and parameters to these commands. The word “parameter” is also used to refer to quantities which are modifiable by
get
andset
.Subclassed by o2scl::cli_readline
Parameter storage and associated iterator type
-
typedef std::map<std::string, parameter*, std::greater<std::string>>::iterator
par_t
¶ List iterator.
-
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.
-
int
print_option_list
()¶ Desc.
-
int
output_param_list
()¶ Output the 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
¶
-
bool
string_equal_dash
(std::string s1, std::string s2)¶ Compare two strings, treating dashes and underscores as equivalent.
-
cli
()¶
-
~cli
()¶
The hard-coded command functions
-
bool
gnu_intro
¶ If true, output the usual GNU intro when run_interactive() is called (default true).
In order to conform to GNU standards, this ought not be set to false by default.
-
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_shell
(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)¶
-
int
apply_aliases
(std::vector<std::string> &sv, size_t istart, bool debug = false)¶ Proceed through
sv
, applying all previously defined aliases.
-
int
set_function
(comm_option_funct &usf)¶ Function to call when a
set
command is issued.
Value to indicate whether commands are also command-line options
-
const int
comm_option_command
= 0¶
-
const int
comm_option_cl_param
= 1¶
-
const int
comm_option_both
= 2¶
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_shell
¶
-
comm_option_s
c_no_intro
¶
-
comm_option_s
c_alias
¶
-
bool
sync_verbose
¶ If true, then sync cli::verbose, with a parameter of the same name (default true)
-
bool
shell_cmd_allowed
¶ If true, allow the user to executeshell commands (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)
Basic operation
-
int
set_comm_option
(comm_option_s &ic)¶ Add a new command.
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.
-
void
remove_comm_option
(std::string cmd)¶ Remove a command with long name
cmd
.
-
template<class
vec_t
>
intset_comm_option_vec
(size_t list_size, vec_t &option_list)¶ Add new commands specified in a list.
-
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.
-
char *
cli_gets
(const char *c)¶ The function which obtains input from the user.
- Idea for Future:
Think about whether or not this should be protected? (Possibly not, as it’s extensively used by acolm.cpp)
-
int
call_args
(std::vector<cmd_line_arg> &ca, int debug = 0)¶ Call functions corresponding to command-line args.
-
int
process_args_c
(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.
This doesn’t actually execute the functions for the corresponding options, but simply processes the parameters
argv
andargv
and packs the information intoca
.This function assumes that
argc[0]
just contains the name of the command, and should thus be ignored.
-
int
parse_for_aliases
(std::vector<std::string> &svsv, bool allow_undashed)¶ Go through the argument list in
sv
and add any alias definitions which appear.Note, if verbose is greater than 1, then new alias definitions will be output to the screen.
-
int
process_args
(std::vector<std::string> &sv, std::vector<cmd_line_arg> &ca, int debug, bool also_call_args = false)¶ Process command-line arguments from a vector of strings.
This doesn’t actually execute the functions for the corresponding options, but simply processes the arguments in
sv
and packs the information intoca
.
-
int
process_args_str
(std::string s, std::vector<cmd_line_arg> &ca, int debug = 0, bool also_call_args = false)¶ Process command-line arguments from a string.
- Idea for Future:
There’s a typecast in this function to (char *) from (const char *) which needs reworking.
-
int
set_verbose
(int v)¶ Set verbosity.
Most errors are output to the screen even if verbose is zero.
-
int
run_interactive
()¶ Run the interactive mode.
-
int
set_alias
(std::string alias, std::string str)¶ Set an alias
alias
for the stringstr
.Aliases can also be set using the command
'alias'
, but that version allows only one-word aliases.
-
std::string
get_alias
(std::string alias)¶ Set an alias
alias
for the stringstr
.Aliases can also be set using the command
'alias'
, but that version allows only one-word aliases.
-
bool
is_valid_option
(std::string str)¶ Return true if
str
is a valid option or command.
-
bool
is_parameter
(std::string name)¶ Return true if
name
is a valid parameter name.
-
std::vector<std::string>
get_parameter_list
()¶ Get the list of parameters.
-
std::vector<std::string>
get_option_list
()¶ Get the list of options/commands.
-
std::string
parameter_desc
(std::string name)¶ Get the description of parameter named
name
.
-
std::string
option_short_desc
(std::string name)¶ Get the short description of option named
name
.
-
class
parameter
¶ Parameter for o2scl::cli.
Subclassed by o2scl::cli::parameter_bool, o2scl::cli::parameter_double, o2scl::cli::parameter_int, o2scl::cli::parameter_size_t, o2scl::cli::parameter_string
Public Functions
-
~parameter
()¶
-
int
set
(std::string s) = 0¶ Set from string.
-
std::string
get
() = 0¶ Convert to string.
Public Members
-
std::string
help
¶ Help description.
-
-
class
parameter_bool
: public o2scl::cli::parameter¶ String parameter for o2scl::cli.
Public Functions
-
~parameter_bool
()¶
-
int
set
(std::string s)¶ Set from string.
-
std::string
get
()¶ Convert to string.
Public Members
-
bool *
b
¶ Parameter.
-
-
class
parameter_double
: public o2scl::cli::parameter¶ Double parameter for o2scl::cli.
Public Functions
-
parameter_double
()¶
-
~parameter_double
()¶
-
int
set
(std::string s)¶ Set from string.
-
std::string
get
()¶ Convert to string.
Public Members
-
double *
d
¶ Parameter.
-
bool
parse_strings
¶ If true, use value_spec() to convert strings to doubles.
-
-
class
parameter_int
: public o2scl::cli::parameter¶ Integer parameter for o2scl::cli.
Public Functions
-
parameter_int
()¶
-
~parameter_int
()¶
-
int
set
(std::string s)¶ Set from string.
-
std::string
get
()¶ Convert to string.
Public Members
-
bool
parse_strings
¶ If true, use value_spec() to convert strings to integers.
-
int *
i
¶ Parameter.
-
-
class
parameter_size_t
: public o2scl::cli::parameter¶ Integer parameter for o2scl::cli.
Public Functions
-
parameter_size_t
()¶
-
~parameter_size_t
()¶
-
int
set
(std::string st)¶ Set from string.
-
std::string
get
()¶ Convert to string.
Public Members
-
bool
parse_strings
¶ If true, use value_spec() to convert strings to integers.
-
size_t *
s
¶ Parameter.
-