24namespace seqan3::detail
53class format_parse :
public format_base
59 format_parse() =
delete;
60 format_parse(format_parse
const & pf) =
default;
61 format_parse & operator=(format_parse
const & pf) =
default;
62 format_parse(format_parse &&) =
default;
63 format_parse & operator=(format_parse &&) =
default;
64 ~format_parse() =
default;
77 template <
typename option_type,
typename val
idator_type>
78 void add_option(option_type & value,
83 validator_type && option_validator)
85 option_calls.push_back(
86 [
this, &value, short_id, long_id, spec, option_validator]()
88 get_option(value, short_id, long_id, spec, option_validator);
95 void add_flag(
bool & value,
101 flag_calls.push_back(
102 [
this, &value, short_id, long_id]()
104 get_flag(value, short_id, long_id);
111 template <
typename option_type,
typename val
idator_type>
112 void add_positional_option(option_type & value,
114 validator_type && option_validator)
116 positional_option_calls.push_back(
117 [
this, &value, option_validator]()
119 get_positional_option(value, option_validator);
124 void parse(argument_parser_meta_data
const & )
126 end_of_options_it =
std::find(argv.begin(), argv.end(),
"--");
130 for (
auto && f : option_calls)
133 for (
auto && f : flag_calls)
136 check_for_unknown_ids();
138 if (end_of_options_it != argv.end())
139 *end_of_options_it =
"";
141 for (
auto && f : positional_option_calls)
144 check_for_left_over_args();
160 template <
typename id_type>
161 static bool is_empty_id(id_type
const &
id)
163 if constexpr (std::same_as<std::remove_cvref_t<id_type>,
std::string>)
166 return is_char<'\0'>(
id);
190 template <
typename iterator_type,
typename id_type>
191 static iterator_type find_option_id(iterator_type begin_it, iterator_type end_it, id_type
const &
id)
202 if constexpr (std::same_as<id_type, char>)
206 return current_arg.
substr(0, full_id.
size()) == full_id;
211 return current_arg.
substr(0, full_id.
size()) == full_id &&
212 (current_arg.
size() == full_id.
size()
213 || current_arg[full_id.
size()] ==
'=');
220 enum class option_parse_result
233 return {
"--" + long_id};
240 static std::string prepend_dash(
char const short_id)
242 return {
'-', short_id};
252 if (short_id ==
'\0')
253 return prepend_dash(long_id);
254 else if (long_id.
empty())
255 return prepend_dash(short_id);
257 return prepend_dash(short_id) +
"/" + prepend_dash(long_id);
265 auto it =
std::find(argv.begin(), end_of_options_it, prepend_dash(long_id));
267 if (it != end_of_options_it)
270 return (it != end_of_options_it);
276 bool flag_is_set(
char const short_id)
281 if (arg[0] ==
'-' && arg.size() > 1 && arg[1] !=
'-')
283 auto pos = arg.find(short_id);
285 if (pos != std::string::npos)
306 template <
typename option_t>
308 option_parse_result parse_option_value(option_t & value,
std::string const & in)
313 if (stream.fail() || !stream.eof())
314 return option_parse_result::error;
316 return option_parse_result::success;
326 template <named_enumeration option_t>
327 option_parse_result parse_option_value(option_t & value,
std::string const & in)
329 auto map = seqan3::enumeration_names<option_t>;
331 if (
auto it = map.find(in); it == map.end())
335 [](
auto pair1,
auto pair2)
337 if constexpr (std::totally_ordered<option_t>)
339 if (pair1.second != pair2.second)
340 return pair1.second < pair2.second;
342 return pair1.first < pair2.first;
345 throw user_input_error{detail::to_string(
"You have chosen an invalid input value: ",
347 ". Please use one of: ",
348 key_value_pairs | std::views::keys)};
355 return option_parse_result::success;
362 return option_parse_result::success;
377 template <detail::is_container_option container_option_t,
typename format_parse_t = format_parse>
378 requires requires (format_parse_t fp,
379 typename container_option_t::value_type & container_value,
382 fp.parse_option_value(container_value, in)
383 } -> std::same_as<option_parse_result>;
385 option_parse_result parse_option_value(container_option_t & value,
std::string const & in)
387 typename container_option_t::value_type tmp{};
389 auto res = parse_option_value(tmp, in);
391 if (res == option_parse_result::success)
392 value.push_back(tmp);
409 template <arithmetic option_t>
411 option_parse_result parse_option_value(option_t & value,
std::string const & in)
415 if (res.ec == std::errc::result_out_of_range)
416 return option_parse_result::overflow_error;
417 else if (res.ec == std::errc::invalid_argument || res.ptr != &in[in.
size()])
418 return option_parse_result::error;
420 return option_parse_result::success;
433 option_parse_result parse_option_value(
bool & value,
std::string const & in)
439 else if (in ==
"true")
441 else if (in ==
"false")
444 return option_parse_result::error;
446 return option_parse_result::success;
456 template <
typename option_type>
457 void throw_on_input_error(option_parse_result
const res,
461 std::string msg{
"Value parse failed for " + option_name +
": "};
463 if (res == option_parse_result::error)
465 throw user_input_error{msg +
"Argument " + input_value +
" could not be parsed as type "
466 + get_type_name_as_string(option_type{}) +
"."};
471 if (res == option_parse_result::overflow_error)
473 throw user_input_error{msg +
"Numeric argument " + input_value +
" is not in the valid range ["
479 assert(res == option_parse_result::success);
499 template <
typename option_type,
typename id_type>
500 bool identify_and_retrieve_option_value(option_type & value,
504 if (option_it != end_of_options_it)
507 size_t id_size = (prepend_dash(
id)).
size();
509 if ((*option_it).size() > id_size)
511 if ((*option_it)[id_size] ==
'=')
513 if ((*option_it).size() == id_size + 1)
514 throw too_few_arguments(
"Missing value for option " + prepend_dash(
id));
515 input_value = (*option_it).
substr(id_size + 1);
519 input_value = (*option_it).
substr(id_size);
528 if (option_it == end_of_options_it)
529 throw too_few_arguments(
"Missing value for option " + prepend_dash(
id));
530 input_value = *option_it;
534 auto res = parse_option_value(value, input_value);
535 throw_on_input_error<option_type>(res, prepend_dash(
id), input_value);
559 template <
typename option_type,
typename id_type>
560 bool get_option_by_id(option_type & value, id_type
const &
id)
562 auto it = find_option_id(argv.begin(), end_of_options_it,
id);
564 if (it != end_of_options_it)
565 identify_and_retrieve_option_value(value, it,
id);
567 if (find_option_id(it, end_of_options_it,
id) != end_of_options_it)
568 throw option_declared_multiple_times(
"Option " + prepend_dash(
id)
569 +
" is no list/container but declared multiple times.");
571 return (it != end_of_options_it);
585 template <detail::is_container_option option_type,
typename id_type>
586 bool get_option_by_id(option_type & value, id_type
const &
id)
588 auto it = find_option_id(argv.begin(), end_of_options_it,
id);
589 bool seen_at_least_once{it != end_of_options_it};
591 if (seen_at_least_once)
594 while (it != end_of_options_it)
596 identify_and_retrieve_option_value(value, it,
id);
597 it = find_option_id(it, end_of_options_it,
id);
600 return seen_at_least_once;
616 void check_for_unknown_ids()
618 for (
auto it = argv.begin(); it != end_of_options_it; ++it)
621 if (!arg.empty() && arg[0] ==
'-')
627 else if (arg[1] !=
'-' && arg.size() > 2)
629 throw unknown_option(
"Unknown flags " + expand_multiple_flags(arg)
630 +
". In case this is meant to be a non-option/argument/parameter, "
631 +
"please specify the start of arguments with '--'. "
632 +
"See -h/--help for program information.");
636 throw unknown_option(
"Unknown option " + arg
637 +
". In case this is meant to be a non-option/argument/parameter, "
638 +
"please specify the start of non-options with '--'. "
639 +
"See -h/--help for program information.");
656 void check_for_left_over_args()
665 throw too_many_arguments(
"Too many arguments provided. Please see -h/--help for more information.");
688 template <
typename option_type,
typename val
idator_type>
689 void get_option(option_type & value,
695 bool short_id_is_set{get_option_by_id(value, short_id)};
696 bool long_id_is_set{get_option_by_id(value, long_id)};
699 if (short_id_is_set && long_id_is_set && !detail::is_container_option<option_type>)
700 throw option_declared_multiple_times(
"Option " + combine_option_names(short_id, long_id)
701 +
" is no list/container but specified multiple times");
703 if (short_id_is_set || long_id_is_set)
711 throw validation_error(
std::string(
"Validation failed for option ")
712 + combine_option_names(short_id, long_id) +
": " + ex.
what());
719 throw required_option_missing(
"Option " + combine_option_names(short_id, long_id)
720 +
" is required but not set.");
731 void get_flag(
bool & value,
char const short_id,
std::string const & long_id)
733 value = flag_is_set(short_id) || flag_is_set(long_id);
758 template <
typename option_type,
typename val
idator_type>
759 void get_positional_option(option_type & value, validator_type &&
validator)
761 ++positional_option_count;
769 if (it == argv.end())
770 throw too_few_arguments(
"Not enough positional arguments provided (Need at least "
772 +
"). See -h/--help for more information.");
774 if constexpr (detail::is_container_option<
777 assert(positional_option_count == positional_option_calls.size());
781 while (it != argv.end())
783 auto res = parse_option_value(value, *it);
785 throw_on_input_error<option_type>(res,
id, *it);
794 ++positional_option_count;
799 auto res = parse_option_value(value, *it);
801 throw_on_input_error<option_type>(res,
id, *it);
812 throw validation_error(
"Validation failed for positional option " +
std::to_string(positional_option_count)
824 unsigned positional_option_count{0};
The <charconv> header from C++17's standard library.
option_spec
Used to further specify argument_parser options/flags.
Definition: auxiliary.hpp:248
@ required
Definition: auxiliary.hpp:250
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:146
A type that satisfies std::is_arithmetic_v<t>.
The concept for option validators passed to add_option/positional_option.
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.