47 virtual const char *
what() const noexcept {
return msg_.c_str(); }
58 return "[" + shambase::term_colors::col8b_red() +
"Error" + shambase::term_colors::reset()
63 std::vector<std::string_view>
args;
69 std::optional<std::string>
args;
85 if (opt.name == name) {
100 for (
auto arg :
args) {
101 if (arg.rfind(
"-", 0) == 0) {
104 err_str() +
" opts argument : " + std::string(arg) +
" is not registered");
133 err_str() +
" opts argument :" + std::string(option_name) +
" is not registered");
135 std::string(option_name) +
" option is not registered in ::opts");
138 for (
auto it =
args.begin(), end =
args.end(); it != end; ++it) {
139 if (*it == option_name)
146 std::string_view
get_option(
const std::string_view &option_name) {
151 err_str() +
" opts argument :" + std::string(option_name) +
"is not registered");
153 std::string(option_name) +
" option is not registered in ::opts");
156 for (
auto it =
args.begin(), end =
args.end(); it != end; ++it) {
157 if (*it == option_name)
165 void register_opt(std::string name, std::optional<std::string>
args, std::string description) {
170 shambase::format(
"The option {} is already registered", name));
183 void init(
int _argc,
char *_argv[]) {
213 fmt::println(
"\nUsage :");
217 return lhs.name < rhs.name;
222 std::string arg_print = arg.value_or(
"");
226 "%-15s %-15s : %s", n.c_str(), arg_print.c_str(), desc.c_str()));
236 exe = exe.substr(exe.find_last_of(
'/') + 1);
248 return lhs.name < rhs.name;
256 if (arg.has_value()) {
259 entry =
"'" + name +
"[" + desc +
"]:" + arg.value() +
"'";
263 entry =
"'" + name +
"[" + desc +
"]'";
Exception handler for exeption in this lib.
virtual ~ShamCmdOptException() noexcept
Destructor.
std::string msg_
Held message.
ShamCmdOptException(const char *message)
Exception CTOR from a message (cstring)
virtual const char * what() const noexcept
Get the message attached to the exception.
ShamCmdOptException(const std::string &message)
Exception CTOR from a message (string)
This header file contains utility functions related to exception handling in the code.
This file handler generic cli & env options.
void println(std::string_view sv)
Prints a string to the console followed by a newline.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for cli utilities
bool is_help_mode()
Check if the help mode is enabled.
int argc
supplied argc from main
std::vector< Opts > registered_opts
Registered cli options.
void print_help_env_var()
Print the documentation of the environment variables registered with register_env_var_doc()
void process_cmdopt_generic_opts()
Process generic cli and env variables options.
bool has_option(const std::string_view &option_name)
Check if an option is present.
char ** argv
supplied argv from main
std::string_view get_option(const std::string_view &option_name)
Get the value of an option.
std::vector< std::string_view > args
Executable argument list (mapped from argv)
char ** get_argv()
Get the command line arguments.
int get_argc()
Get the number of command line arguments.
void register_cmdopt_generic_opts()
Register generic cli and env variables options.
void init(int argc, char *argv[])
Initialize the command line option parser. We also process generic options in this call (color detect...
void check_args_registered()
Check if all argument passed to shamrock where registered otherwise throw.
std::string_view executable_name
Executable name.
bool init_done
Has cmdopt init been called.
bool is_name_registered(const std::string_view &name)
Check if the option name is registered.
auto err_str
Error string to be printed in case of failure.
void check_init()
Check if init has been performed otherwise throw.
void print_completion_zsh()
print zsh completion script to be sourced
void register_opt(std::string name, std::optional< std::string > args, std::string description)
Register a command line option.
void print_help()
Print the help message.
Struct for data related to an option.
std::optional< std::string > args
Documention of the option argument.
std::string name
Name of the option (including dashes)
std::string description
Description of the otion.