Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
|
The Parser Module. More...
Classes | |
struct | sharg::parser_meta_data |
Stores all parser related meta information of the sharg::parser. More... | |
struct | sharg::config< validator_t > |
Option struct that is passed to the sharg::parser::add_option() function. More... | |
class | sharg::detail::format_base |
The format that contains all helper functions needed in all formats. More... | |
class | sharg::detail::format_help_base< derived_type > |
The format that contains all helper functions needed in all formats for printing the interface description of the application (to std::cout). More... | |
class | sharg::detail::format_help |
The format that prints the help page to std::cout. More... | |
class | sharg::detail::format_short_help |
The format that prints a short help message to std::cout. More... | |
class | sharg::detail::format_version |
The format that prints the version to std::cout. More... | |
class | sharg::detail::format_copyright |
The format that prints the copyright information to std::cout. More... | |
class | sharg::detail::format_html |
The format that prints the help page as html to std::cout. More... | |
class | sharg::detail::format_man |
The format that prints the help page information formatted for a man page to std::cout. More... | |
class | sharg::detail::format_parse |
The format that organizes the actual parsing of command line arguments. More... | |
class | sharg::detail::format_tdl |
A generalized format to create different tool description files. More... | |
class | sharg::detail::version_checker |
A functor whose operator() performs the server http request and version checks. More... | |
class | sharg::parser |
The Sharg command line parser. More... | |
Functions | |
bool | sharg::detail::stdin_is_terminal () |
Check whether the standard input is interactive. | |
bool | sharg::detail::stdout_is_terminal () |
Check whether the standard output is interactive. | |
bool | sharg::detail::stderr_is_terminal () |
Check whether the standard error output is interactive. | |
unsigned | sharg::detail::get_terminal_width () |
Retrieve size of terminal. | |
void | sharg::detail::call_server (std::string const &command, std::promise< bool > prom) |
Writes a timestamp file and performs the server call to get the newest version information. | |
The Parser Module.
The sharg::parser is a general purpose argument parser that provides convenient access to the command line arguments passed to the program. It automatically generates a help page and can export manual-pages as well as HTML documentation.
Since the terms option and arguments are frequently used in different contexts we want to first clarify our usage:
-i myfile
, --infile myfile
] refer to key-value pairs. The key is either a short indentifier, restricted to a single character -i
, or a long identifier --infile
.arg1
] refers to command line arguments that are specified without an identifier/key, are always required and are identified by their position.-b
] refers to identifiers that are not followed by a value (booleans) and therefore only indicate whether they are set or not.Adding an option is done in a single call. You simply need to provide a predeclared variable and some additional information like the identifier, description or advanced restrictions. To actually retrieve the value from the command line and enable every other mechanism you need to call the sharg::parser::parse function in the end.
Now you can call your application via the command line:
You can also display the help page automatically:
The sharg::parser follows the POSIX conventions. Note that this means among others:
-a -b
is equivalent to -ab
.-c foo
is equivalent to -cfoo
.--
terminates the options and signals that only positional options follow. This enables the user to use a positional option beginning with -
without it being misinterpreted as an option identifier.There are two different kinds of errors: Developer errors and user errors.
Developer errors are those that violate the sharg::parser design (e.g. calling the sharg::parser::parse function twice or specifying two different options with the same identifier.) In this case, a sharg::design_error is thrown.
The second kind are user errors, due to invalid command line calls. In this case a sharg::parser_error is thrown.
For example:
See the sharg::parser::parse documentation for a detailed list of which exceptions are caught.
SeqAn applications that are using the sharg::parser can check SeqAn servers for version updates. The functionality helps getting new versions out to users faster. It is also used to inform application developers of new versions of the SeqAn library which means that applications ship with less bugs. For privacy implications, please see: https://docs.seqan.de/sharg/main_user/about_update_notifications.html.
Developers that wish to disable this feature permanently can pass an extra constructor argument:
Users of applications that have this feature activated can opt-out, by either:
--version-check false/0
orSHARG_NO_VERSION_CHECK
environment variable.Note that in case there is no --version-check
option (display available options with -h/--help)
, then the developer already disabled the version check functionality.
sharg::design_error | if this function was already called before. |
sharg::design_error | if the application name or subcommands contain illegal characters. |
sharg::option_declared_multiple_times | if an option that is not a list was declared multiple times. |
sharg::user_input_error | if an incorrect argument is given as (positional) option value. |
sharg::required_option_missing | if the user did not provide a required option. |
sharg::too_many_arguments | if the command line call contained more arguments than expected. |
sharg::too_few_arguments | if the command line call contained less arguments than expected. |
sharg::validation_error | if the argument was not excepted by the provided validator. |
When no specific key words are supplied, the sharg::parser starts to process the command line for specified options, flags and positional options.
If the given command line input (argv
) contains the following keywords (in order of checking), the parser will exit (std::exit) with error code 0 after doing the following:
Example:
The code above gives the following output when calling --help
:
If the user does something wrong, it looks like this:
The Sharg Parser offers a validation mechanism for (positional_)options via callables. You can pass any functor that fulfils the sharg::validator and takes the value passed to the add_(positional_)option function call as a parameter. We provide some commonly used functor that might come in handy:
|
inline |
Writes a timestamp file and performs the server call to get the newest version information.
[in] | command | The system command as a string. See sharg::detail::version_checker::command for details. |
[in] | prom | A promise object used to track the detached thread which executes this command. |
This function performs a https server request by executing a hard coded command (string) as a system call.
|
inline |
Retrieve size of terminal.
Note: Only works on Linux/Unix. TIOCGWINSZ is the command (number) to trigger filling the winsize struct. STDOUT_FILENO is the default file descriptor (STDOUT_FILENO == fileno(stdout)).
|
inline |
Check whether the standard error output is interactive.
For example "./some_binary --help 2> cerr.out" will return false. "./some_binary --help" will return true.
|
inline |
Check whether the standard input is interactive.
For example "./some_binary --help | less" will return false. "./some_binary --help" will return true.
|
inline |
Check whether the standard output is interactive.
For example "./some_binary --help | less" will return false. "./some_binary --help" will return true.