SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
|
The SeqAn command line parser. More...
#include <seqan3/argument_parser/argument_parser.hpp>
Public Member Functions | |
argument_parser & | get_sub_parser () |
Returns a reference to the sub-parser instance if subcommand parsing was enabled. | |
template<typename id_type > requires std::same_as<id_type, char> || std::constructible_from<std::string, id_type> | |
bool | is_option_set (id_type const &id) const |
Checks whether the option identifier (id ) was set on the command line by the user. | |
void | parse () |
Initiates the actual command line parsing. | |
Constructors, destructor and assignment | |
argument_parser ()=delete | |
Deleted. | |
argument_parser (argument_parser const &)=delete | |
Deleted. Holds std::future. | |
argument_parser & | operator= (argument_parser const &)=delete |
Deleted. Holds std::future. | |
argument_parser (argument_parser &&)=default | |
Defaulted. | |
argument_parser & | operator= (argument_parser &&)=default |
Defaulted. | |
argument_parser (std::string const app_name, int const argc, char const *const *const argv, update_notifications version_updates=update_notifications::on, std::vector< std::string > subcommands={}) | |
Initializes an seqan3::argument_parser object from the command line arguments. | |
~argument_parser () | |
The destructor. | |
Adding options | |
Add (positional) options and flags to the parser. | |
template<typename option_type , validator validator_type = detail::default_validator<option_type>> requires (argument_parser_compatible_option<option_type> || argument_parser_compatible_option<std::ranges::range_value_t<option_type>>) && std::invocable<validator_type, option_type> | |
void | add_option (option_type &value, char const short_id, std::string const &long_id, std::string const &desc, option_spec const spec=option_spec::standard, validator_type option_validator=validator_type{}) |
Adds an option to the seqan3::argument_parser. | |
void | add_flag (bool &value, char const short_id, std::string const &long_id, std::string const &desc, option_spec const spec=option_spec::standard) |
Adds a flag to the seqan3::argument_parser. | |
template<typename option_type , validator validator_type = detail::default_validator<option_type>> requires (argument_parser_compatible_option<option_type> || argument_parser_compatible_option<std::ranges::range_value_t<option_type>>) && std::invocable<validator_type, option_type> | |
void | add_positional_option (option_type &value, std::string const &desc, validator_type option_validator=validator_type{}) |
Adds a positional option to the seqan3::argument_parser. | |
Structuring the Help Page | |
void | add_section (std::string const &title, option_spec const spec=option_spec::standard) |
Adds an help page section to the seqan3::argument_parser. | |
void | add_subsection (std::string const &title, option_spec const spec=option_spec::standard) |
Adds an help page subsection to the seqan3::argument_parser. | |
void | add_line (std::string const &text, bool is_paragraph=false, option_spec const spec=option_spec::standard) |
Adds an help page text line to the seqan3::argument_parser. | |
void | add_list_item (std::string const &key, std::string const &desc, option_spec const spec=option_spec::standard) |
Adds an help page list item (key-value) to the seqan3::argument_parser. | |
Public Attributes | |
argument_parser_meta_data | info |
Aggregates all parser related meta data (see seqan3::argument_parser_meta_data struct). | |
The SeqAn command line parser.
The seqan3::argument_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.
Furthermore common tool descriptor (CTD) files can be exported (soon) and a server be queried for available application updates.
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 seqan3::argument_parser::parse function in the end.
Now you can call your application via the command line:
You can also display the help page automatically:
There are two different kinds of errors: Developer errors and user errors.
Developer errors are those that violate the seqan3::argument_parser design (e.g. calling the seqan3::argument_parser::parse function twice or specifying two different options with the same identifier.) In this case, a seqan3::design_error is thrown.
The second kind are user errors, due to invalid command line calls. In this case a seqan3::argument_parser_error is thrown.
For example:
See the seqan3::argument_parser::parse documentation for a detailed list of which exceptions are caught.
SeqAn applications that are using the seqan3::argument_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://github.com/seqan/seqan3/wiki/Update-Notifications.
Users of applications that have this feature activated can opt-out, by either:
--version-check false/0
orSEQAN3_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.
|
inline |
Initializes an seqan3::argument_parser object from the command line arguments.
[in] | app_name | The name of the app that is displayed on the help page. |
[in] | argc | The number of command line arguments. |
[in] | argv | The command line arguments to parse. |
[in] | version_updates | Notify users about version updates (default seqan3::update_notifications::on). |
[in] | subcommands | A list of subcommands (see subcommand parsing ). |
seqan3::design_error | if the application name contains illegal characters. |
The application name must only contain alpha-numeric characters, _
or -
, i.e. the following regex must evaluate to true: "^[a-zA-Z0-9_-]+$"
.
See the argument parser tutorial for more information about the version check functionality.
|
inline |
Adds a flag to the seqan3::argument_parser.
[in,out] | value | The variable which shows whether the flag is turned off (default) or on. |
[in] | short_id | The short identifier for the flag (e.g. 'i'). |
[in] | long_id | The long identifier for the flag (e.g. "integer"). |
[in] | desc | The description of the flag to be shown in the help page. |
[in] | spec | Advanced flag specification, see seqan3::option_spec. |
seqan3::design_error | if value is true. |
|
inline |
Adds an help page text line to the seqan3::argument_parser.
[in] | text | The text to print. |
[in] | is_paragraph | Whether to insert as paragraph or just a line (Default: false). |
[in] | spec | Whether to always display this line (seqan3::option_spec::standard), only when showing the advanced help page (seqan3::option_spec::advanced) or never (seqan3::option_spec::hidden). |
If the line is not a paragraph (false), only one line break is appended, otherwise two line breaks are appended. This only affects the help page and other output formats.
|
inline |
Adds an help page list item (key-value) to the seqan3::argument_parser.
[in] | key | The key of the key-value pair of the list item. |
[in] | desc | The value of the key-value pair of the list item. |
[in] | spec | Whether to always display this list item (seqan3::option_spec::standard), only when showing the advanced help page (seqan3::option_spec::advanced) or never (seqan3::option_spec::hidden). |
Note: This only affects the help page and other output formats.
A list item is composed of a key (key
) and value (desc
) and usually used for option identifier-description-pairs. E.g.:
|
inline |
Adds an option to the seqan3::argument_parser.
option_type | Must have a formatted input function (stream >> value). If option_type is a container, its value type must have the formatted input function (exception: std::string is not regarded as a container). See FormattedInputFunction . |
validator_type | The type of validator to be applied to the option value. Must model seqan3::validator. |
[in,out] | value | The variable in which to store the given command line argument. |
[in] | short_id | The short identifier for the option (e.g. 'a'). |
[in] | long_id | The long identifier for the option (e.g. "age"). |
[in] | desc | The description of the option to be shown in the help page. |
[in] | spec | Advanced option specification, see seqan3::option_spec. |
[in] | option_validator | A seqan3::validator that verifies the value after parsing (callable). |
seqan3::design_error |
|
inline |
Adds a positional option to the seqan3::argument_parser.
option_type | Must have a formatted input function (stream >> value). If option_type is a container, its value type must have the formatted input function (exception: std::string is not regarded as a container). See FormattedInputFunction . |
validator_type | The type of validator to be applied to the option value. Must model seqan3::validator. |
[in,out] | value | The variable in which to store the given command line argument. |
[in] | desc | The description of the positional option to be shown in the help page. |
[in] | option_validator | A seqan3::validator that verifies the value after parsing (callable). |
seqan3::design_error |
The validator must be applicable to the given output variable (value
).
|
inline |
Adds an help page section to the seqan3::argument_parser.
[in] | title | The title of the section. |
[in] | spec | Whether to always display this section title (seqan3::option_spec::standard), only when showing the advanced help page (seqan3::option_spec::advanced) or never (seqan3::option_spec::hidden). |
This only affects the help page and other output formats.
|
inline |
Adds an help page subsection to the seqan3::argument_parser.
[in] | title | The title of the subsection. |
[in] | spec | Whether to always display this subsection title (seqan3::option_spec::standard), only when showing the advanced help page (seqan3::option_spec::advanced) or never (seqan3::option_spec::hidden). |
This only affects the help page and other output formats.
|
inline |
Checks whether the option identifier (id
) was set on the command line by the user.
id_type | Either type char or a type that a std::string is constructible from. |
[in] | id | The short (char ) or long (std::string ) option identifier to search for. |
true
if option identifier id
was set on the command line by the user. seqan3::design_error | if the function is used incorrectly (see details below). |
You can only ask for option identifiers that were added to the parser beforehand via seqan3::argument_parser::add_option
. As in the seqan3::argument_parser::add_option
call, pass short identifiers as a char
and long identifiers as a std::string
or a type that a std::string
is constructible from (e.g. a const char *
).
This function throws a seqan3::design_error if
seqan3::argument_parser::parse()
was not called before.std::string
) that only consists of a single character. If you mean to pass a short identifier, please pass it as a char
not a std::string
.seqan3::argument_parser::add_option()
calls beforehand.
|
inline |
Initiates the actual command line parsing.
seqan3::design_error | if this function was already called before. |
seqan3::option_declared_multiple_times | if an option that is not a list was declared multiple times. |
seqan3::user_input_error | if an incorrect argument is given as (positional) option value. |
seqan3::required_option_missing | if the user did not provide a required option. |
seqan3::too_many_arguments | if the command line call contained more arguments than expected. |
seqan3::too_few_arguments | if the command line call contained less arguments than expected. |
seqan3::validation_error | if the argument was not excepted by the provided validator. |
When no specific key words are supplied, the seqan3::argument_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:
argument_parser_meta_data seqan3::argument_parser::info |
Aggregates all parser related meta data (see seqan3::argument_parser_meta_data struct).
The meta information is assembled in a struct to provide a central access point that can be easily extended.
You can access the members directly: (see seqan3::argument_parser_meta_data for a list of the info members)
This will produce a nice help page when the user calls -h
or --help
: