183 char const *
const *
const argv,
184 bool version_check =
true,
186 version_check_dev_decision{version_check}
189 throw parser_design_error{
"The application name must only contain alpha-numeric characters " 190 "or '_' and '-' (regex: \"^[a-zA-Z0-9_-]+$\")."};
191 for (
auto & sub : subcommands)
193 throw parser_design_error{
"The subcommand name must only contain alpha-numeric characters or '_'."};
196 init(argc, argv,
std::move(subcommands));
203 if (version_check_future.
valid())
231 template <
typename option_type, val
idator val
idator_type = detail::default_val
idator<option_type>>
242 validator_type
validator = validator_type{})
244 if (sub_parser !=
nullptr)
247 verify_identifiers(short_id, long_id);
250 std::visit([=, &value] (
auto & f) { f.add_option(value, short_id, long_id, desc, spec,
validator); }, format);
267 verify_identifiers(short_id, long_id);
270 std::visit([=, &value] (
auto & f) { f.add_flag(value, short_id, long_id, desc, spec); }, format);
293 template <
typename option_type, val
idator val
idator_type = detail::default_val
idator<option_type>>
301 validator_type
validator = validator_type{})
303 if (sub_parser !=
nullptr)
306 if (has_positional_list_option)
307 throw parser_design_error{
"You added a positional option with a list value before so you cannot add " 308 "any other positional options."};
311 has_positional_list_option =
true;
315 std::visit([=, &value] (
auto & f) { f.add_positional_option(value, desc,
validator); }, format);
391 if (parse_was_called)
396 if (app_version.decide_if_check_is_performed(version_check_dev_decision, version_check_user_decision))
400 version_check_future = app_version_prom.
get_future();
401 app_version(
std::move(app_version_prom));
405 parse_was_called =
true;
412 if (sub_parser ==
nullptr)
415 "so you cannot access the sub-parser!");
430 std::visit([&] (
auto & f) { f.add_section(title); }, format);
439 std::visit([&] (
auto & f) { f.add_subsection(title); }, format);
452 std::visit([&] (
auto & f) { f.add_line(text, line_is_paragraph); }, format);
473 std::visit([&] (
auto & f) { f.add_list_item(key, desc); }, format);
529 bool parse_was_called{
false};
532 bool has_positional_list_option{
false};
535 bool version_check_dev_decision{};
541 friend struct ::seqan3::detail::test_accessor;
547 std::regex app_name_regex{
"^[a-zA-Z0-9_-]+$"};
588 format = detail::format_short_help{};
592 bool special_format_was_set{
false};
594 for(
int i = 1, argv_len = argc; i < argv_len; ++i)
600 sub_parser = std::make_unique<argument_parser>(info.
app_name +
"-" + arg, argc - i, argv + i,
false);
603 if (arg ==
"-h" || arg ==
"--help")
605 format = detail::format_help{subcommands,
false};
606 init_standard_options();
607 special_format_was_set =
true;
609 else if (arg ==
"-hh" || arg ==
"--advanced-help")
611 format = detail::format_help{subcommands,
true};
612 init_standard_options();
613 special_format_was_set =
true;
615 else if (arg ==
"--version")
617 format = detail::format_version{};
618 special_format_was_set =
true;
620 else if (arg.substr(0, 13) ==
"--export-help")
626 export_format = arg.
substr(14);
630 if (argv_len <= i + 1)
632 export_format = {argv[i+1]};
635 if (export_format ==
"html")
636 format = detail::format_html{subcommands};
637 else if (export_format ==
"man")
638 format = detail::format_man{subcommands};
644 "Value must be one of [html, man]"};
645 init_standard_options();
646 special_format_was_set =
true;
648 else if (arg ==
"--copyright")
650 format = detail::format_copyright{};
651 special_format_was_set =
true;
653 else if (arg ==
"--version-check")
661 version_check_user_decision =
true;
663 version_check_user_decision =
false;
675 if (!special_format_was_set)
677 if (!subcommands.
empty() && sub_parser ==
nullptr)
680 "(one of ", subcommands,
"). Use -h/--help for more information.")};
683 format = detail::format_parse(argc,
std::move(argv_new));
688 void init_standard_options()
691 add_list_item(
"\\fB-h\\fP, \\fB--help\\fP",
"Prints the help page.");
693 "Prints the help page including advanced options.");
694 add_list_item(
"\\fB--version\\fP",
"Prints the version information.");
695 add_list_item(
"\\fB--copyright\\fP",
"Prints the copyright/license information.");
697 "Export the help page information. Value must be one of [html, man].");
698 if (version_check_dev_decision)
699 add_list_item(
"\\fB--version-check\\fP (bool)",
"Whether to to check for the newest app version. Default: 1.");
708 template <
typename id_type>
709 bool id_exists(id_type
const &
id)
711 if (detail::format_parse::is_empty_id(
id))
725 void verify_identifiers(
char const short_id,
std::string const & long_id)
727 auto constexpr allowed =
is_alnum || is_char<'_'> || is_char<'@'>;
729 if (id_exists(short_id))
731 if (id_exists(long_id))
733 if (long_id.
length() == 1)
735 if (!allowed(short_id) && !is_char<'\0'>(short_id))
736 throw parser_design_error(
"Option identifiers may only contain alphanumeric characters, '_', or '@'.");
737 if (long_id.
size() > 0 &&
is_char<
'-'>(long_id[0]))
742 if (!(allowed(c) || is_char<'-'>(c)))
743 throw parser_design_error(
"Long identifiers may only contain alphanumeric characters, '_', '-', or '@'.");
745 if (detail::format_parse::is_empty_id(short_id) && detail::format_parse::is_empty_id(long_id))
755 detail::format_short_help,
756 detail::format_version,
759 detail::format_copyright
760 > format{detail::format_help{{},
false}};
763 std::set<std::string> used_option_ids{
"h",
"hh",
"help",
"advanced-help",
"export-help",
"version",
"copyright"};
void add_line(std::string const &text, bool line_is_paragraph=false)
Adds an help page text line to the seqan3::argument_parser.
Definition: argument_parser.hpp:450
Checks whether the the type can be used in an add_(positional_)option call on the argument parser...
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition: predicate.hpp:83
Argument parser exception thrown when an argument could not be casted to the according type...
Definition: exceptions.hpp:117
void add_subsection(std::string const &title)
Adds an help page subsection to the seqan3::argument_parser.
Definition: argument_parser.hpp:437
void add_section(std::string const &title)
Adds an help page section to the seqan3::argument_parser.
Definition: argument_parser.hpp:428
Specifies whether the given callable is invocable with the given arguments.
The SeqAn command line parser.
Definition: argument_parser.hpp:153
The main SeqAn3 namespace.
Auxiliary for pretty printing of exception messages.
Checks if program is run interactively and retrieves dimensions of terminal (Transferred from seqan2)...
Argument parser exception that is thrown whenever there is an design error directed at the developer ...
Definition: exceptions.hpp:136
The concept for option validators passed to add_option/positional_option.
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition: traits.hpp:152
void add_positional_option(option_type &value, std::string const &desc, validator_type validator=validator_type{})
Adds a positional option to the seqan3::argument_parser.
Definition: argument_parser.hpp:299
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:410
argument_parser()=delete
Deleted.
void add_list_item(std::string const &key, std::string const &desc)
Adds an help page list item (key-value) to the seqan3::argument_parser.
Definition: argument_parser.hpp:471
Argument parser exception that is thrown whenever there is an error while parsing the command line ar...
Definition: exceptions.hpp:37
argument_parser_meta_data info
Aggregates all parser related meta data (see seqan3::argument_parser_meta_data struct).
Definition: argument_parser.hpp:525
~argument_parser()
The destructor.
Definition: argument_parser.hpp:200
Provides character predicates for tokenisation.
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
argument_parser(std::string const app_name, int const argc, char const *const *const argv, bool version_check=true, std::vector< std::string > subcommands={})
Initializes an argument_parser object from the command line arguments.
Definition: argument_parser.hpp:181
Provides the version check functionality.
option_spec
Used to further specify argument_parser options/flags.
Definition: auxiliary.hpp:231
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::DEFAULT, validator_type validator=validator_type{})
Adds an option to the seqan3::argument_parser.
Definition: argument_parser.hpp:237
void add_flag(bool &value, char const short_id, std::string const &long_id, std::string const &desc, option_spec const &spec=option_spec::DEFAULT)
Adds a flag to the seqan3::argument_parser.
Definition: argument_parser.hpp:261
Forward declares seqan3::detail::test_accessor.
The concept std::same_as<T, U> is satisfied if and only if T and U denote the same type...
A more refined container concept than seqan3::container.
void parse()
Initiates the actual command line parsing.
Definition: argument_parser.hpp:389
auto constexpr is_alnum
Checks whether c is a alphanumeric character.
Definition: predicate.hpp:220
The default were no checking or special displaying is happening.
Definition: auxiliary.hpp:233