This document provides example recipes on how to carry out particular tasks using the Sharg functionalities in C++. Please note that these recipes are not ordered. You can use the links in the table of contents or the search function of your browser to navigate them.
It will take some time, but we hope to expand this document into containing numerous great examples. If you have suggestions for how to improve the Cookbook and/or examples you would like included, please feel free to contact us.
Constructing a basic parser
{
std::cerr <<
"reference_file_path: " << reference_path <<
'\n';
std::cerr <<
"index_path " << index_path <<
'\n';
}
struct cmd_arguments
{
};
void initialise_parser(
sharg::parser & parser, cmd_arguments & args)
{
.long_id = "reference",
.description = "The path to the reference.",
.required = true,
.validator = sharg::input_file_validator{{"fa", "fasta"}}});
args.index_path,
.long_id = "output",
.description = "The output index file path.",
.validator =
sharg::output_file_validator{sharg::output_file_open_options::create_new, {"index"}}});
}
int main(int argc, char const ** argv)
{
cmd_arguments args{};
initialise_parser(parser, args);
try
{
parser.parse();
}
{
return -1;
}
run_program(args.reference_path, args.index_path);
return 0;
}
Meta-header for the Parser module .
Parser exception that is thrown whenever there is an error while parsing the command line arguments.
Definition: exceptions.hpp:43
The Sharg command line parser.
Definition: parser.hpp:156
void add_option(option_type &value, config< validator_type > const &config)
Adds an option to the sharg::parser.
Definition: parser.hpp:251
parser_meta_data info
Aggregates all parser related meta data (see sharg::parser_meta_data struct).
Definition: parser.hpp:661
Option struct that is passed to the sharg::parser::add_option() function.
Definition: config.hpp:46
Constructing a subcommand parser
struct pull_arguments
{
bool progress{false};
};
{
pull_arguments args{};
try
{
}
{
return -1;
}
std::cerr <<
"Git pull with repository " << args.repository <<
" and branch " << args.branch <<
'\n';
return 0;
}
struct push_arguments
{
bool push_all{false};
};
{
push_arguments args{};
try
{
}
{
return -1;
}
std::cerr <<
"Git push with repository " << args.repository <<
" and branches ";
for (auto && branch : args.branches)
return 0;
}
int main(int argc, char const ** argv)
{
top_level_parser.info.description.push_back("You can push or pull from a remote repository.");
bool flag{false};
top_level_parser.add_flag(flag,
sharg::config{.
short_id =
'f', .long_id =
"flag", .description =
"some flag"});
try
{
top_level_parser.parse();
}
{
return -1;
}
return run_git_pull(sub_parser);
return run_git_push(sub_parser);
else
return 0;
}
void add_positional_option(option_type &value, config< validator_type > const &config)
Adds a positional option to the sharg::parser.
Definition: parser.hpp:319
void parse()
Initiates the actual command line parsing.
Definition: parser.hpp:404
parser & get_sub_parser()
Returns a reference to the sub-parser instance if subcommand parsing was enabled.
Definition: parser.hpp:449
@ on
Automatic update notifications should be enabled.
char short_id
The short identifier for the option (e.g. 'a', making the option callable via -a).
Definition: config.hpp:56
Write a custom validator
This recipe implements a validator that checks whether a numeric argument is an integral square (i.e. 0, 1, 4, 9...). Invalid values throw a sharg::validation_error.
struct custom_validator
{
using option_value_type = double;
void operator()(option_value_type const & val) const
{
if ((std::round(val) != val) ||
(std::pow(std::round(std::sqrt(val)), 2) != val))
{
}
}
{
return "Value must be the square of an integral number.";
}
};
Parser exception thrown when an argument could not be casted to the according type.
Definition: exceptions.hpp:183