Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Classes | Functions | Variables
shamcmdopt Namespace Reference

namespace for cli utilities More...

Classes

struct  Opts
 Struct for data related to an option. More...
 

Functions

bool is_ci_github_actions ()
 Check if the environment variable GITHUB_ACTIONS is set.
 
bool is_ci_travis ()
 Check if the environment variable TRAVIS is set.
 
bool is_ci_circle_ci ()
 Check if the environment variable CIRCLECI is set.
 
bool is_ci_gitlab_ci ()
 Check if the environment variable GITLAB_CI is set.
 
int get_argc ()
 Get the number of command line arguments.
 
char ** get_argv ()
 Get the command line arguments.
 
void register_opt (std::string name, std::optional< std::string > args, std::string description)
 Register a command line option.
 
void init (int argc, char *argv[])
 Initialize the command line option parser. We also process generic options in this call (color detection typically)
 
bool has_option (const std::string_view &option_name)
 Check if an option is present.
 
std::string_view get_option (const std::string_view &option_name)
 Get the value of an option.
 
void print_help ()
 Print the help message.
 
void print_completion_zsh ()
 print zsh completion script to be sourced
 
bool is_help_mode ()
 Check if the help mode is enabled.
 
void register_cmdopt_generic_opts ()
 Register generic cli and env variables options.
 
void process_cmdopt_generic_opts ()
 Process generic cli and env variables options.
 
std::optional< std::string > getenv_str (const char *env_var)
 Get the content of the environment variable if it exist.
 
std::string getenv_str_default (const char *env_var, std::string default_val)
 Get the content of the environment variable if it exist, otherwise return the default value.
 
void register_env_var_doc (std::string env_var, std::string desc)
 Register the documentation of an environment variable.
 
std::optional< std::string > getenv_str_register (const char *env_var, std::string desc)
 Get the content of the environment variable if it exist and register it documentation.
 
std::string getenv_str_default_register (const char *env_var, std::string default_val, std::string desc)
 Get the content of the environment variable if it exist and register it documentation, otherwise return the default value.
 
void print_help_env_var ()
 Print the documentation of the environment variables registered with register_env_var_doc()
 
bool is_a_tty ()
 Test if current terminal is a tty.
 
u32 get_tty_columns ()
 Get the number of columns of the current terminal.
 
u32 get_tty_lines ()
 Get the number of lines of the current terminal.
 
void set_tty_columns (u32 columns)
 Set the forced width of the terminal.
 
void reset_tty_columns ()
 Reset the forced width of the terminal.
 
bool is_name_registered (const std::string_view &name)
 Check if the option name is registered.
 
void check_args_registered ()
 Check if all argument passed to shamrock where registered otherwise throw.
 
void check_init ()
 Check if init has been performed otherwise throw.
 
void process_colors ()
 Detect if the current process should use colored output or not.
 
void process_tty ()
 Process the SHAMTTYCOL environment variable to set the number of columns for the terminal.
 
std::pair< u32, u32get_tty_dim ()
 Get the number of columns and lines of the current terminal.
 

Variables

auto err_str
 Error string to be printed in case of failure.
 
std::string_view executable_name
 Executable name.
 
std::vector< std::string_view > args
 Executable argument list (mapped from argv)
 
bool init_done
 Has cmdopt init been called.
 
std::vector< Optsregistered_opts
 Registered cli options.
 
int argc
 supplied argc from main
 
char ** argv
 supplied argv from main
 
u32 tty_forced_width = 0
 

Detailed Description

namespace for cli utilities

Function Documentation

◆ check_args_registered()

void shamcmdopt::check_args_registered ( )
protected

Check if all argument passed to shamrock where registered otherwise throw.

Definition at line 95 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ check_init()

void shamcmdopt::check_init ( )
protected

Check if init has been performed otherwise throw.

Definition at line 123 of file cmdopt.cpp.

◆ get_argc()

int shamcmdopt::get_argc ( )

Get the number of command line arguments.

Returns
int The number of command line arguments.

Definition at line 197 of file cmdopt.cpp.

◆ get_argv()

char ** shamcmdopt::get_argv ( )

Get the command line arguments.

Returns
char** The command line arguments.

Definition at line 203 of file cmdopt.cpp.

◆ get_option()

std::string_view shamcmdopt::get_option ( const std::string_view &  option_name)

Get the value of an option.

Parameters
option_nameThe name of the option.
Returns
std::string_view The value of the option.

Definition at line 146 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ get_tty_columns()

u32 shamcmdopt::get_tty_columns ( )

Get the number of columns of the current terminal.

If forced width is set (by set_tty_columns), this function returns the forced width. If the current terminal is not a tty, the function returns 100.

Returns
Number of columns if the current terminal

Definition at line 81 of file tty.cpp.

+ Here is the call graph for this function:

◆ get_tty_dim()

std::pair< u32, u32 > shamcmdopt::get_tty_dim ( )

Get the number of columns and lines of the current terminal.

If forced width is set (by set_tty_columns), this function returns the forced width. If the current terminal is not a tty, the function returns {10, 100}.

Returns
Number of columns and lines if the current terminal

Definition at line 53 of file tty.cpp.

+ Here is the call graph for this function:

◆ get_tty_lines()

u32 shamcmdopt::get_tty_lines ( )

Get the number of lines of the current terminal.

If the current terminal is not a tty, the function returns 10.

Returns
Number of lines if the current terminal

Definition at line 82 of file tty.cpp.

+ Here is the call graph for this function:

◆ getenv_str()

std::optional< std::string > shamcmdopt::getenv_str ( const char *  env_var)

Get the content of the environment variable if it exist.

Parameters
env_varthe name of the env variable
Returns
std::optional<std::string> return the value of the env variable if it exist, none otherwise

Definition at line 24 of file env.cpp.

◆ getenv_str_default()

std::string shamcmdopt::getenv_str_default ( const char *  env_var,
std::string  default_val 
)
inline

Get the content of the environment variable if it exist, otherwise return the default value.

Parameters
env_varthe name of the env variable
default_valthe default value to return if the env variable does not exist
Returns
std::string the value of the env variable if it exist, the default value otherwise

Definition at line 40 of file env.hpp.

+ Here is the call graph for this function:

◆ getenv_str_default_register()

std::string shamcmdopt::getenv_str_default_register ( const char *  env_var,
std::string  default_val,
std::string  desc 
)
inline

Get the content of the environment variable if it exist and register it documentation, otherwise return the default value.

This function is a shortcut for calling both getenv_str_default() and register_env_var_doc(). It is used to register the documentation of the environment variable and return its value if it exist, otherwise return the default value.

Parameters
env_varthe name of the env variable
default_valthe default value to return if the env variable does not exist
descthe description of the environment variable
Returns
std::string the value of the env variable if it exist, the default value otherwise

Definition at line 88 of file env.hpp.

+ Here is the call graph for this function:

◆ getenv_str_register()

std::optional< std::string > shamcmdopt::getenv_str_register ( const char *  env_var,
std::string  desc 
)
inline

Get the content of the environment variable if it exist and register it documentation.

This function is a shortcut for calling both getenv_str() and register_env_var_doc(). It is used to register the documentation of the environment variable and return its value if it exist.

Parameters
env_varthe name of the env variable
descthe description of the environment variable
Returns
std::optional<std::string> return the value of the env variable if it exist, none otherwise

Definition at line 70 of file env.hpp.

+ Here is the call graph for this function:

◆ has_option()

bool shamcmdopt::has_option ( const std::string_view &  option_name)

Check if an option is present.

Parameters
option_nameThe name of the option.
Returns
true If the option is present.
false If the option is not present.

Definition at line 128 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ init()

void shamcmdopt::init ( int  argc,
char *  argv[] 
)

Initialize the command line option parser. We also process generic options in this call (color detection typically)

Parameters
argcThe number of command line arguments.
argvThe command line arguments.

Definition at line 183 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ is_a_tty()

bool shamcmdopt::is_a_tty ( )

Test if current terminal is a tty.

Returns
true is a tty
false is not a tty

Definition at line 31 of file tty.cpp.

◆ is_ci_circle_ci()

bool shamcmdopt::is_ci_circle_ci ( )

Check if the environment variable CIRCLECI is set.

This is used to check if the code is run in a CircleCI job.

Returns
true if the environment variable is set, false otherwise

Definition at line 58 of file ci_env.cpp.

◆ is_ci_github_actions()

bool shamcmdopt::is_ci_github_actions ( )

Check if the environment variable GITHUB_ACTIONS is set.

This is used to check if the code is run in a GitHub Actions CI job.

Returns
true if the environment variable is set, false otherwise

Definition at line 40 of file ci_env.cpp.

◆ is_ci_gitlab_ci()

bool shamcmdopt::is_ci_gitlab_ci ( )

Check if the environment variable GITLAB_CI is set.

This is used to check if the code is run in a GitLab CI job.

Returns
true if the environment variable is set, false otherwise

Definition at line 67 of file ci_env.cpp.

◆ is_ci_travis()

bool shamcmdopt::is_ci_travis ( )

Check if the environment variable TRAVIS is set.

This is used to check if the code is run in a Travis CI job.

Returns
true if the environment variable is set, false otherwise

Definition at line 49 of file ci_env.cpp.

◆ is_help_mode()

bool shamcmdopt::is_help_mode ( )

Check if the help mode is enabled.

Returns
true If the help mode is enabled.
false If the help mode is not enabled.

Definition at line 280 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ is_name_registered()

bool shamcmdopt::is_name_registered ( const std::string_view &  name)
protected

Check if the option name is registered.

Parameters
namethe option name (including dashes)
Returns
true the option is registered
false the option is not registered

Definition at line 83 of file cmdopt.cpp.

◆ print_completion_zsh()

void shamcmdopt::print_completion_zsh ( )

print zsh completion script to be sourced

Definition at line 231 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ print_help()

void shamcmdopt::print_help ( )

Print the help message.

Definition at line 210 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ print_help_env_var()

void shamcmdopt::print_help_env_var ( )

Print the documentation of the environment variables registered with register_env_var_doc()

This function is used to print the documentation of the environment variables registered with register_env_var_doc(). It will print the name, the description and the value of each registered environment variable.

Definition at line 47 of file env.cpp.

+ Here is the call graph for this function:

◆ process_cmdopt_generic_opts()

void shamcmdopt::process_cmdopt_generic_opts ( )

Process generic cli and env variables options.

Definition at line 162 of file generic_opts.cpp.

+ Here is the call graph for this function:

◆ process_colors()

void shamcmdopt::process_colors ( )

Detect if the current process should use colored output or not.

Colors are disabled by the cli flag --nocolor or env variable NO_COLOR Colors are enabled by the cli flag --color or env variable CLICOLOR_FORCE If no options are set we enable colors if in tty mode and if the terminal support colored output

  • We first check cli args as they are dominant over env variables
  • We then check if some env variables are set
  • If nothing is set we enable color if term_support_color() and is_a_tty() returns true

Definition at line 103 of file generic_opts.cpp.

+ Here is the call graph for this function:

◆ process_tty()

void shamcmdopt::process_tty ( )

Process the SHAMTTYCOL environment variable to set the number of columns for the terminal.

If the variable is set, its value is parsed as an integer and used to set the terminal columns. If the value is less than the minimum size (10), it is set to the minimum size. If the value is not an integer or is out of range, an error message is printed.

Definition at line 141 of file generic_opts.cpp.

+ Here is the call graph for this function:

◆ register_cmdopt_generic_opts()

void shamcmdopt::register_cmdopt_generic_opts ( )

Register generic cli and env variables options.

Definition at line 76 of file generic_opts.cpp.

+ Here is the call graph for this function:

◆ register_env_var_doc()

void shamcmdopt::register_env_var_doc ( std::string  env_var,
std::string  desc 
)

Register the documentation of an environment variable.

This function is used to register the documentation of an environment variable. The documentation will be printed in the help message.

Exceptions
std::invalid_argumentif the environment variable is already registered
Parameters
env_varthe name of the environment variable
descthe description of the environment variable

Definition at line 35 of file env.cpp.

+ Here is the call graph for this function:

◆ register_opt()

void shamcmdopt::register_opt ( std::string  name,
std::optional< std::string >  args,
std::string  description 
)

Register a command line option.

Parameters
nameThe name of the option.
argsThe arguments of the option. If the option takes no argument, pass std::nullopt.
descriptionThe description of the option.

Definition at line 165 of file cmdopt.cpp.

+ Here is the call graph for this function:

◆ reset_tty_columns()

void shamcmdopt::reset_tty_columns ( )
inline

Reset the forced width of the terminal.

Calls set_tty_columns(0) to restore the default behavior of get_tty_columns().

Definition at line 67 of file tty.hpp.

+ Here is the call graph for this function:

◆ set_tty_columns()

void shamcmdopt::set_tty_columns ( u32  columns)

Set the forced width of the terminal.

By default, the function get_tty_columns() returns the number of columns of the current terminal. If this function is called with a non-zero value, get_tty_columns() will always return this value until set_tty_columns() is called again with a different value.

Parameters
columnsThe width of the terminal. If zero, the default behavior of get_tty_columns() is restored.

Definition at line 43 of file tty.cpp.

Variable Documentation

◆ argc

int shamcmdopt::argc
protected

supplied argc from main

Definition at line 178 of file cmdopt.cpp.

◆ args

std::vector<std::string_view> shamcmdopt::args
protected

Executable argument list (mapped from argv)

Definition at line 63 of file cmdopt.cpp.

◆ argv

char** shamcmdopt::argv
protected

supplied argv from main

Definition at line 181 of file cmdopt.cpp.

◆ err_str

auto shamcmdopt::err_str
protected
Initial value:
= []() {
return "[" + shambase::term_colors::col8b_red() + "Error" + shambase::term_colors::reset()
+ "]";
}

Error string to be printed in case of failure.

Definition at line 57 of file cmdopt.cpp.

◆ executable_name

std::string_view shamcmdopt::executable_name
protected

Executable name.

Definition at line 62 of file cmdopt.cpp.

◆ init_done

bool shamcmdopt::init_done
protected

Has cmdopt init been called.

Definition at line 64 of file cmdopt.cpp.

◆ registered_opts

std::vector<Opts> shamcmdopt::registered_opts
protected

Registered cli options.

Definition at line 74 of file cmdopt.cpp.

◆ tty_forced_width

u32 shamcmdopt::tty_forced_width = 0

Forced width of the terminal, if set by set_tty_columns. If set to 0, get_tty_dim returns the actual width of the terminal.

Definition at line 41 of file tty.cpp.