181 char const *
const *
const argv,
184 version_check_dev_decision{version_updates},
185 subcommands{std::move(subcommands)}
189 throw design_error{(
"The application name must only contain alpha-numeric characters or '_' and '-' "
190 "(regex: \"^[a-zA-Z0-9_-]+$\").")};
193 for (
auto & sub : this->subcommands)
197 throw design_error{
"The subcommand name must only contain alpha-numeric characters or '_' and '-' "
198 "(regex: \"^[a-zA-Z0-9_-]+$\")."};
211 if (version_check_future.
valid())
239 template <
typename option_type, val
idator val
idator_type = detail::default_val
idator<option_type>>
243 std::invocable<validator_type, option_type>
250 validator_type option_validator = validator_type{})
252 if (sub_parser !=
nullptr)
253 throw design_error{
"You may only specify flags for the top-level parser."};
255 verify_identifiers(short_id, long_id);
258 std::visit([=, &value] (
auto & f) { f.add_option(value, short_id, long_id, desc, spec, option_validator); },
280 throw design_error(
"A flag's default value must be false.");
282 verify_identifiers(short_id, long_id);
285 std::visit([=, &value] (
auto & f) { f.add_flag(value, short_id, long_id, desc, spec); }, format);
308 template <
typename option_type, val
idator val
idator_type = detail::default_val
idator<option_type>>
312 std::invocable<validator_type, option_type>
316 validator_type option_validator = validator_type{})
318 if (sub_parser !=
nullptr)
319 throw design_error{
"You may only specify flags for the top-level parser."};
321 if (has_positional_list_option)
322 throw design_error{
"You added a positional option with a list value before so you cannot add "
323 "any other positional options."};
325 if constexpr (detail::is_container_option<option_type>)
326 has_positional_list_option =
true;
330 std::visit([=, &value] (
auto & f) { f.add_positional_option(value, desc, option_validator); }, format);
401 if (parse_was_called)
402 throw design_error(
"The function parse() must only be called once!");
406 if (std::holds_alternative<detail::format_parse>(format) && !subcommands.empty() && sub_parser ==
nullptr)
408 throw too_few_arguments{detail::to_string(
"You either forgot or misspelled the subcommand! Please specify"
409 " which sub-program you want to use: one of ", subcommands,
410 ". Use -h/--help for more information.")};
413 if (app_version.decide_if_check_is_performed(version_check_dev_decision, version_check_user_decision))
417 version_check_future = app_version_prom.
get_future();
418 app_version(std::move(app_version_prom));
422 parse_was_called =
true;
429 if (sub_parser ==
nullptr)
431 throw design_error(
"No subcommand was provided at the construction of the argument parser!");
463 template <
typename id_type>
465 requires std::same_as<id_type, char> || std::constructible_from<std::string, id_type>
469 if (!parse_was_called)
470 throw design_error{
"You can only ask which options have been set after calling the function `parse()`."};
474 char_or_string_t short_or_long_id = {
id};
476 if constexpr (!std::same_as<id_type, char>)
478 if (short_or_long_id.size() == 1)
480 throw design_error{
"Long option identifiers must be longer than one character! If " + short_or_long_id +
481 "' was meant to be a short identifier, please pass it as a char ('') not a string"
487 throw design_error{
"You can only ask for option identifiers that you added with add_option() before."};
490 auto end_of_options =
std::find(cmd_arguments.
begin(), cmd_arguments.
end(), end_of_options_indentifier);
491 auto option_it = detail::format_parse::find_option_id(cmd_arguments.
begin(), end_of_options, short_or_long_id);
492 return option_it != end_of_options;
506 std::visit([&] (
auto & f) { f.add_section(title, spec); }, format);
517 std::visit([&] (
auto & f) { f.add_subsection(title, spec); }, format);
531 std::visit([&] (
auto & f) { f.add_line(text, is_paragraph, spec); }, format);
556 std::visit([&] (
auto & f) { f.add_list_item(key, desc, spec); }, format);
612 bool parse_was_called{
false};
615 bool has_positional_list_option{
false};
624 friend struct ::seqan3::detail::test_accessor;
630 std::regex app_name_regex{
"^[a-zA-Z0-9_-]+$"};
650 detail::format_short_help,
651 detail::format_version,
654 detail::format_copyright
655> format{detail::format_help{{},
false}};
658 std::set<std::string> used_option_ids{
"h",
"hh",
"help",
"advanced-help",
"export-help",
"version",
"copyright"};
695 void init(
int argc,
char const *
const *
const argv)
699 format = detail::format_short_help{};
703 bool special_format_was_set{
false};
706 for (
int i = 1, argv_len = argc; i < argv_len; ++i)
712 sub_parser = std::make_unique<argument_parser>(
info.
app_name +
"-" + arg,
719 if (arg ==
"-h" || arg ==
"--help")
721 format = detail::format_help{subcommands,
false};
722 init_standard_options();
723 special_format_was_set =
true;
725 else if (arg ==
"-hh" || arg ==
"--advanced-help")
727 format = detail::format_help{subcommands,
true};
728 init_standard_options();
729 special_format_was_set =
true;
731 else if (arg ==
"--version")
733 format = detail::format_version{};
734 special_format_was_set =
true;
736 else if (arg.substr(0, 13) ==
"--export-help")
742 export_format = arg.
substr(14);
746 if (argv_len <= i + 1)
747 throw too_few_arguments{
"Option --export-help must be followed by a value."};
748 export_format = {argv[i+1]};
751 if (export_format ==
"html")
752 format = detail::format_html{subcommands};
753 else if (export_format ==
"man")
754 format = detail::format_man{subcommands};
759 throw validation_error{
"Validation failed for option --export-help: "
760 "Value must be one of [html, man]"};
761 init_standard_options();
762 special_format_was_set =
true;
764 else if (arg ==
"--copyright")
766 format = detail::format_copyright{};
767 special_format_was_set =
true;
769 else if (arg ==
"--version-check")
772 throw too_few_arguments{
"Option --version-check must be followed by a value."};
776 if (arg ==
"1" || arg ==
"true")
777 version_check_user_decision =
true;
778 else if (arg ==
"0" || arg ==
"false")
779 version_check_user_decision =
false;
781 throw validation_error{
"Value for option --version-check must be true (1) or false (0)."};
792 if (!special_format_was_set)
793 format = detail::format_parse(argc, cmd_arguments);
797 void init_standard_options()
800 add_list_item(
"\\fB-h\\fP, \\fB--help\\fP",
"Prints the help page.");
802 "Prints the help page including advanced options.");
803 add_list_item(
"\\fB--version\\fP",
"Prints the version information.");
804 add_list_item(
"\\fB--copyright\\fP",
"Prints the copyright/license information.");
806 "Export the help page information. Value must be one of [html, man].");
808 add_list_item(
"\\fB--version-check\\fP (bool)",
"Whether to check for the newest app version. Default: true.");
816 template <
typename id_type>
817 bool id_exists(id_type
const &
id)
819 if (detail::format_parse::is_empty_id(
id))
833 void verify_identifiers(
char const short_id,
std::string const & long_id)
835 auto constexpr allowed =
is_alnum || is_char<'_'> || is_char<'@'>;
837 if (id_exists(short_id))
838 throw design_error(
"Option Identifier '" +
std::string(1, short_id) +
"' was already used before.");
839 if (id_exists(long_id))
840 throw design_error(
"Option Identifier '" + long_id +
"' was already used before.");
841 if (long_id.
length() == 1)
842 throw design_error(
"Long IDs must be either empty, or longer than one character.");
843 if (!allowed(short_id) && !is_char<'\0'>(short_id))
844 throw design_error(
"Option identifiers may only contain alphanumeric characters, '_', or '@'.");
845 if (long_id.
size() > 0 && is_char<'-'>(long_id[0]))
846 throw design_error(
"First character of long ID cannot be '-'.");
850 if (!(allowed(c) || is_char<
'-'>(c)))
851 throw design_error(
"Long identifiers may only contain alphanumeric characters, '_', '-', or '@'.");
853 if (detail::format_parse::is_empty_id(short_id) && detail::format_parse::is_empty_id(long_id))
854 throw design_error(
"Option Identifiers cannot both be empty.");
The SeqAn command line parser.
Definition: argument_parser.hpp:152
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.
Definition: argument_parser.hpp:273
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.
Definition: argument_parser.hpp:314
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.
Definition: argument_parser.hpp:179
argument_parser & operator=(argument_parser &&)=default
Defaulted.
~argument_parser()
The destructor.
Definition: argument_parser.hpp:208
argument_parser_meta_data info
Aggregates all parser related meta data (see seqan3::argument_parser_meta_data struct).
Definition: argument_parser.hpp:608
bool is_option_set(id_type const &id) const
Checks whether the option identifier (id) was set on the command line by the user.
Definition: argument_parser.hpp:467
argument_parser(argument_parser &&)=default
Defaulted.
void parse()
Initiates the actual command line parsing.
Definition: argument_parser.hpp:399
argument_parser()=delete
Deleted.
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.
Definition: argument_parser.hpp:245
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.
Definition: argument_parser.hpp:529
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.
Definition: argument_parser.hpp:552
void add_section(std::string const &title, option_spec const spec=option_spec::standard)
Adds an help page section to the seqan3::argument_parser.
Definition: argument_parser.hpp:504
void add_subsection(std::string const &title, option_spec const spec=option_spec::standard)
Adds an help page subsection to the seqan3::argument_parser.
Definition: argument_parser.hpp:515
argument_parser & operator=(argument_parser const &)=default
Defaulted.
argument_parser & get_sub_parser()
Returns a reference to the sub-parser instance if subcommand parsing was enabled.
Definition: argument_parser.hpp:427
argument_parser(argument_parser const &)=default
Defaulted.
Argument parser exception that is thrown whenever there is an design error directed at the developer ...
Definition: exceptions.hpp:143
Argument parser exception thrown when too few arguments are provided.
Definition: exceptions.hpp:76
option_spec
Used to further specify argument_parser options/flags.
Definition: auxiliary.hpp:247
@ standard
The default were no checking or special displaying is happening.
Definition: auxiliary.hpp:248
auto constexpr is_alnum
Checks whether c is a alphanumeric character.
Definition: predicate.hpp:202
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition: traits.hpp:187
Checks whether the the type can be used in an add_(positional_)option call on the argument parser.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
update_notifications
Indicates whether application allows automatic update notifications by the seqan3::argument_parser.
Definition: auxiliary.hpp:266
@ off
Automatic update notifications should be disabled.
@ on
Automatic update notifications should be enabled.
Checks if program is run interactively and retrieves dimensions of terminal (Transferred from seqan2)...
Forward declares seqan3::detail::test_accessor.
Auxiliary for pretty printing of exception messages.
Provides the version check functionality.