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;
76 template <
typename option_type,
typename val
idator_type>
77 void add_option(option_type & value,
82 validator_type && option_validator)
84 option_calls.push_back(
85 [
this, &value, short_id, long_id, spec, option_validator]()
87 get_option(value, short_id, long_id, spec, option_validator);
94 void add_flag(
bool & value,
100 flag_calls.push_back(
101 [
this, &value, short_id, long_id]()
103 get_flag(value, short_id, long_id);
110 template <
typename option_type,
typename val
idator_type>
111 void add_positional_option(option_type & value,
113 validator_type && option_validator)
115 positional_option_calls.push_back(
116 [
this, &value, option_validator]()
118 get_positional_option(value, option_validator);
123 void parse(argument_parser_meta_data
const & )
125 end_of_options_it =
std::find(argv.begin(), argv.end(),
"--");
129 for (
auto && f : option_calls)
132 for (
auto && f : flag_calls)
135 check_for_unknown_ids();
137 if (end_of_options_it != argv.end())
138 *end_of_options_it =
"";
140 for (
auto && f : positional_option_calls)
143 check_for_left_over_args();
159 template <
typename id_type>
160 static bool is_empty_id(id_type
const &
id)
162 if constexpr (std::same_as<std::remove_cvref_t<id_type>,
std::string>)
165 return is_char<'\0'>(
id);
189 template <
typename iterator_type,
typename id_type>
190 static iterator_type find_option_id(iterator_type begin_it, iterator_type end_it, id_type
const &
id)
201 if constexpr (std::same_as<id_type, char>)
205 return current_arg.
substr(0, full_id.
size()) == full_id;
210 return current_arg.
substr(0, full_id.
size()) == full_id &&
211 (current_arg.
size() == full_id.
size()
212 || current_arg[full_id.
size()] ==
'=');
219 enum class option_parse_result
232 return {
"--" + long_id};
239 static std::string prepend_dash(
char const short_id)
241 return {
'-', short_id};
251 if (short_id ==
'\0')
252 return prepend_dash(long_id);
253 else if (long_id.
empty())
254 return prepend_dash(short_id);
256 return prepend_dash(short_id) +
"/" + prepend_dash(long_id);
264 auto it =
std::find(argv.begin(), end_of_options_it, prepend_dash(long_id));
266 if (it != end_of_options_it)
269 return (it != end_of_options_it);
275 bool flag_is_set(
char const short_id)
280 if (arg[0] ==
'-' && arg.size() > 1 && arg[1] !=
'-')
282 auto pos = arg.find(short_id);
284 if (pos != std::string::npos)
305 template <
typename option_t>
307 option_parse_result parse_option_value(option_t & value,
std::string const & in)
312 if (stream.fail() || !stream.eof())
313 return option_parse_result::error;
315 return option_parse_result::success;
325 template <named_enumeration option_t>
326 option_parse_result parse_option_value(option_t & value,
std::string const & in)
328 auto map = seqan3::enumeration_names<option_t>;
330 if (
auto it = map.find(in); it == map.end())
333 std::ranges::sort(key_value_pairs,
334 [](
auto pair1,
auto pair2)
336 if constexpr (std::totally_ordered<option_t>)
338 if (pair1.second != pair2.second)
339 return pair1.second < pair2.second;
341 return pair1.first < pair2.first;
344 throw user_input_error{detail::to_string(
"You have chosen an invalid input value: ",
346 ". Please use one of: ",
347 key_value_pairs | std::views::keys)};
354 return option_parse_result::success;
361 return option_parse_result::success;
376 template <detail::is_container_option container_option_t,
typename format_parse_t = format_parse>
377 requires requires (format_parse_t fp,
378 typename container_option_t::value_type & container_value,
381 fp.parse_option_value(container_value, in)
382 } -> std::same_as<option_parse_result>;
384 option_parse_result parse_option_value(container_option_t & value,
std::string const & in)
386 typename container_option_t::value_type tmp{};
388 auto res = parse_option_value(tmp, in);
390 if (res == option_parse_result::success)
391 value.push_back(tmp);
408 template <arithmetic option_t>
410 option_parse_result parse_option_value(option_t & value,
std::string const & in)
412 auto res = std::from_chars(&in[0], &in[in.
size()], value);
414 if (res.ec == std::errc::result_out_of_range)
415 return option_parse_result::overflow_error;
416 else if (res.ec == std::errc::invalid_argument || res.ptr != &in[in.
size()])
417 return option_parse_result::error;
419 return option_parse_result::success;
432 option_parse_result parse_option_value(
bool & value,
std::string const & in)
438 else if (in ==
"true")
440 else if (in ==
"false")
443 return option_parse_result::error;
445 return option_parse_result::success;
455 template <
typename option_type>
456 void throw_on_input_error(option_parse_result
const res,
460 std::string msg{
"Value parse failed for " + option_name +
": "};
462 if (res == option_parse_result::error)
464 throw user_input_error{msg +
"Argument " + input_value +
" could not be parsed as type "
465 + get_type_name_as_string(option_type{}) +
"."};
470 if (res == option_parse_result::overflow_error)
472 throw user_input_error{msg +
"Numeric argument " + input_value +
" is not in the valid range ["
478 assert(res == option_parse_result::success);
498 template <
typename option_type,
typename id_type>
499 bool identify_and_retrieve_option_value(option_type & value,
503 if (option_it != end_of_options_it)
506 size_t id_size = (prepend_dash(
id)).
size();
508 if ((*option_it).size() > id_size)
510 if ((*option_it)[id_size] ==
'=')
512 if ((*option_it).size() == id_size + 1)
513 throw too_few_arguments(
"Missing value for option " + prepend_dash(
id));
514 input_value = (*option_it).
substr(id_size + 1);
518 input_value = (*option_it).
substr(id_size);
527 if (option_it == end_of_options_it)
528 throw too_few_arguments(
"Missing value for option " + prepend_dash(
id));
529 input_value = *option_it;
533 auto res = parse_option_value(value, input_value);
534 throw_on_input_error<option_type>(res, prepend_dash(
id), input_value);
558 template <
typename option_type,
typename id_type>
559 bool get_option_by_id(option_type & value, id_type
const &
id)
561 auto it = find_option_id(argv.begin(), end_of_options_it,
id);
563 if (it != end_of_options_it)
564 identify_and_retrieve_option_value(value, it,
id);
566 if (find_option_id(it, end_of_options_it,
id) != end_of_options_it)
567 throw option_declared_multiple_times(
"Option " + prepend_dash(
id)
568 +
" is no list/container but declared multiple times.");
570 return (it != end_of_options_it);
584 template <detail::is_container_option option_type,
typename id_type>
585 bool get_option_by_id(option_type & value, id_type
const &
id)
587 auto it = find_option_id(argv.begin(), end_of_options_it,
id);
588 bool seen_at_least_once{it != end_of_options_it};
590 if (seen_at_least_once)
593 while (it != end_of_options_it)
595 identify_and_retrieve_option_value(value, it,
id);
596 it = find_option_id(it, end_of_options_it,
id);
599 return seen_at_least_once;
615 void check_for_unknown_ids()
617 for (
auto it = argv.begin(); it != end_of_options_it; ++it)
620 if (!arg.empty() && arg[0] ==
'-')
626 else if (arg[1] !=
'-' && arg.size() > 2)
628 throw unknown_option(
"Unknown flags " + expand_multiple_flags(arg)
629 +
". In case this is meant to be a non-option/argument/parameter, "
630 +
"please specify the start of arguments with '--'. "
631 +
"See -h/--help for program information.");
635 throw unknown_option(
"Unknown option " + arg
636 +
". In case this is meant to be a non-option/argument/parameter, "
637 +
"please specify the start of non-options with '--'. "
638 +
"See -h/--help for program information.");
655 void check_for_left_over_args()
664 throw too_many_arguments(
"Too many arguments provided. Please see -h/--help for more information.");
687 template <
typename option_type,
typename val
idator_type>
688 void get_option(option_type & value,
694 bool short_id_is_set{get_option_by_id(value, short_id)};
695 bool long_id_is_set{get_option_by_id(value, long_id)};
698 if (short_id_is_set && long_id_is_set && !detail::is_container_option<option_type>)
699 throw option_declared_multiple_times(
"Option " + combine_option_names(short_id, long_id)
700 +
" is no list/container but specified multiple times");
702 if (short_id_is_set || long_id_is_set)
710 throw validation_error(
std::string(
"Validation failed for option ")
711 + combine_option_names(short_id, long_id) +
": " + ex.
what());
718 throw required_option_missing(
"Option " + combine_option_names(short_id, long_id)
719 +
" is required but not set.");
730 void get_flag(
bool & value,
char const short_id,
std::string const & long_id)
732 value = flag_is_set(short_id) || flag_is_set(long_id);
757 template <
typename option_type,
typename val
idator_type>
758 void get_positional_option(option_type & value, validator_type &&
validator)
760 ++positional_option_count;
768 if (it == argv.end())
769 throw too_few_arguments(
"Not enough positional arguments provided (Need at least "
771 +
"). See -h/--help for more information.");
773 if constexpr (detail::is_container_option<
776 assert(positional_option_count == positional_option_calls.size());
780 while (it != argv.end())
782 auto res = parse_option_value(value, *it);
784 throw_on_input_error<option_type>(res,
id, *it);
793 ++positional_option_count;
798 auto res = parse_option_value(value, *it);
800 throw_on_input_error<option_type>(res,
id, *it);
811 throw validation_error(
"Validation failed for positional option " +
std::to_string(positional_option_count)
823 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 type_pack/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.