183 char const *
const *
const argv,
186 version_check_dev_decision{version_updates},
187 subcommands{std::move(subcommands)}
191 throw design_error{(
"The application name must only contain alpha-numeric characters or '_' and '-' "
192 "(regex: \"^[a-zA-Z0-9_-]+$\").")};
195 for (
auto & sub : this->subcommands)
199 throw design_error{
"The subcommand name must only contain alpha-numeric characters or '_' and '-' "
200 "(regex: \"^[a-zA-Z0-9_-]+$\")."};
213 if (version_check_future.valid())
241 template <
typename option_type, val
idator val
idator_type = detail::default_val
idator<option_type>>
245 std::invocable<validator_type, option_type>
252 validator_type option_validator = validator_type{})
254 if (sub_parser !=
nullptr)
255 throw design_error{
"You may only specify flags for the top-level parser."};
257 verify_identifiers(short_id, long_id);
260 std::visit([=, &value] (
auto & f) { f.add_option(value, short_id, long_id, desc, spec, option_validator); },
278 verify_identifiers(short_id, long_id);
281 std::visit([=, &value] (
auto & f) { f.add_flag(value, short_id, long_id, desc, spec); }, format);
304 template <
typename option_type, val
idator val
idator_type = detail::default_val
idator<option_type>>
308 std::invocable<validator_type, option_type>
312 validator_type option_validator = validator_type{})
314 if (sub_parser !=
nullptr)
315 throw design_error{
"You may only specify flags for the top-level parser."};
317 if (has_positional_list_option)
318 throw design_error{
"You added a positional option with a list value before so you cannot add "
319 "any other positional options."};
322 has_positional_list_option =
true;
326 std::visit([=, &value] (
auto & f) { f.add_positional_option(value, desc, option_validator); }, format);
397 if (parse_was_called)
398 throw design_error(
"The function parse() must only be called once!");
402 if (std::holds_alternative<detail::format_parse>(format) && !subcommands.empty() && sub_parser ==
nullptr)
404 throw too_few_arguments{detail::to_string(
"You either forgot or misspelled the subcommand! Please specify"
405 " which sub-program you want to use: one of ", subcommands,
406 ". Use -h/--help for more information.")};
409 if (app_version.decide_if_check_is_performed(version_check_dev_decision, version_check_user_decision))
413 version_check_future = app_version_prom.
get_future();
414 app_version(std::move(app_version_prom));
418 parse_was_called =
true;
425 if (sub_parser ==
nullptr)
427 throw design_error(
"No subcommand was provided at the construction of the argument parser!");
459 template <
typename id_type>
461 requires std::same_as<id_type, char> || std::constructible_from<std::string, id_type>
465 if (!parse_was_called)
466 throw design_error{
"You can only ask which options have been set after calling the function `parse()`."};
470 char_or_string_t short_or_long_id = {
id};
472 if constexpr (!std::same_as<id_type, char>)
474 if (short_or_long_id.size() == 1)
476 throw design_error{
"Long option identifiers must be longer than one character! If " + short_or_long_id +
477 "' was meant to be a short identifier, please pass it as a char ('') not a string"
482 if (
std::find(used_option_ids.begin(), used_option_ids.end(),
std::string{id}) == used_option_ids.end())
483 throw design_error{
"You can only ask for option identifiers that you added with add_option() before."};
486 auto end_of_options =
std::find(cmd_arguments.begin(), cmd_arguments.end(), end_of_options_indentifier);
487 auto option_it = detail::format_parse::find_option_id(cmd_arguments.begin(), end_of_options, short_or_long_id);
488 return option_it != end_of_options;
502 std::visit([&] (
auto & f) { f.add_section(title, spec); }, format);
513 std::visit([&] (
auto & f) { f.add_subsection(title, spec); }, format);
527 std::visit([&] (
auto & f) { f.add_line(text, is_paragraph, spec); }, format);
552 std::visit([&] (
auto & f) { f.add_list_item(key, desc, spec); }, format);
608 bool parse_was_called{
false};
611 bool has_positional_list_option{
false};
620 friend struct ::seqan3::detail::test_accessor;
626 std::regex app_name_regex{
"^[a-zA-Z0-9_-]+$"};
646 detail::format_short_help,
647 detail::format_version,
650 detail::format_copyright
651> format{detail::format_help{{},
false}};
654 std::set<std::string> used_option_ids{
"h",
"hh",
"help",
"advanced-help",
"export-help",
"version",
"copyright"};
691 void init(
int argc,
char const *
const *
const argv)
695 format = detail::format_short_help{};
699 bool special_format_was_set{
false};
702 for (
int i = 1, argv_len = argc; i < argv_len; ++i)
708 sub_parser = std::make_unique<argument_parser>(
info.
app_name +
"-" + arg,
715 if (arg ==
"-h" || arg ==
"--help")
717 format = detail::format_help{subcommands,
false};
718 init_standard_options();
719 special_format_was_set =
true;
721 else if (arg ==
"-hh" || arg ==
"--advanced-help")
723 format = detail::format_help{subcommands,
true};
724 init_standard_options();
725 special_format_was_set =
true;
727 else if (arg ==
"--version")
729 format = detail::format_version{};
730 special_format_was_set =
true;
732 else if (arg.substr(0, 13) ==
"--export-help")
738 export_format = arg.
substr(14);
742 if (argv_len <= i + 1)
743 throw too_few_arguments{
"Option --export-help must be followed by a value."};
744 export_format = {argv[i+1]};
747 if (export_format ==
"html")
748 format = detail::format_html{subcommands};
749 else if (export_format ==
"man")
750 format = detail::format_man{subcommands};
755 throw validation_error{
"Validation failed for option --export-help: "
756 "Value must be one of [html, man]"};
757 init_standard_options();
758 special_format_was_set =
true;
760 else if (arg ==
"--copyright")
762 format = detail::format_copyright{};
763 special_format_was_set =
true;
765 else if (arg ==
"--version-check")
768 throw too_few_arguments{
"Option --version-check must be followed by a value."};
772 if (arg ==
"1" || arg ==
"true")
773 version_check_user_decision =
true;
774 else if (arg ==
"0" || arg ==
"false")
775 version_check_user_decision =
false;
777 throw validation_error{
"Value for option --version-check must be true (1) or false (0)."};
784 cmd_arguments.push_back(std::move(arg));
788 if (!special_format_was_set)
789 format = detail::format_parse(argc, cmd_arguments);
793 void init_standard_options()
796 add_list_item(
"\\fB-h\\fP, \\fB--help\\fP",
"Prints the help page.");
798 "Prints the help page including advanced options.");
799 add_list_item(
"\\fB--version\\fP",
"Prints the version information.");
800 add_list_item(
"\\fB--copyright\\fP",
"Prints the copyright/license information.");
802 "Export the help page information. Value must be one of [html, man].");
804 add_list_item(
"\\fB--version-check\\fP (bool)",
"Whether to check for the newest app version. Default: true.");
812 template <
typename id_type>
813 bool id_exists(id_type
const &
id)
815 if (detail::format_parse::is_empty_id(
id))
817 return (!(used_option_ids.insert(
std::string({id}))).second);
829 void verify_identifiers(
char const short_id,
std::string const & long_id)
831 auto constexpr allowed =
is_alnum || is_char<'_'> || is_char<'@'>;
833 if (id_exists(short_id))
834 throw design_error(
"Option Identifier '" +
std::string(1, short_id) +
"' was already used before.");
835 if (id_exists(long_id))
836 throw design_error(
"Option Identifier '" + long_id +
"' was already used before.");
837 if (long_id.
length() == 1)
838 throw design_error(
"Long IDs must be either empty, or longer than one character.");
839 if (!allowed(short_id) && !is_char<'\0'>(short_id))
840 throw design_error(
"Option identifiers may only contain alphanumeric characters, '_', or '@'.");
841 if (long_id.
size() > 0 && is_char<'-'>(long_id[0]))
842 throw design_error(
"First character of long ID cannot be '-'.");
846 if (!(allowed(c) || is_char<
'-'>(c)))
847 throw design_error(
"Long identifiers may only contain alphanumeric characters, '_', '-', or '@'.");
849 if (detail::format_parse::is_empty_id(short_id) && detail::format_parse::is_empty_id(long_id))
850 throw design_error(
"Option Identifiers cannot both be empty.");
The SeqAn command line parser.
Definition: argument_parser.hpp:154
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:272
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:310
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:181
argument_parser & operator=(argument_parser &&)=default
Defaulted.
~argument_parser()
The destructor.
Definition: argument_parser.hpp:210
argument_parser_meta_data info
Aggregates all parser related meta data (see seqan3::argument_parser_meta_data struct).
Definition: argument_parser.hpp:604
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:463
argument_parser(argument_parser &&)=default
Defaulted.
void parse()
Initiates the actual command line parsing.
Definition: argument_parser.hpp:395
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:247
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:525
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:548
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:500
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:511
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:423
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:249
@ standard
The default were no checking or special displaying is happening.
Definition: auxiliary.hpp:250
constexpr auto 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.
A more refined container concept than seqan3::container.
The main SeqAn3 namespace.
Definition: cigar_operation_table.hpp:2
update_notifications
Indicates whether application allows automatic update notifications by the seqan3::argument_parser.
Definition: auxiliary.hpp:268
@ off
Automatic update notifications should be disabled.
@ on
Automatic update notifications should be enabled.
Provides character predicates for tokenisation.
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.