21namespace seqan3::detail
50class format_parse :
public format_base
56 format_parse() =
delete;
57 format_parse(format_parse
const & pf) =
default;
58 format_parse & operator=(format_parse
const & pf) =
default;
59 format_parse(format_parse &&) =
default;
60 format_parse & operator=(format_parse &&) =
default;
61 ~format_parse() =
default;
73 template <
typename option_type,
typename val
idator_type>
74 void add_option(option_type & value,
79 validator_type && option_validator)
81 option_calls.push_back(
82 [
this, &value, short_id, long_id, spec, option_validator]()
84 get_option(value, short_id, long_id, spec, option_validator);
91 void add_flag(
bool & value,
98 [
this, &value, short_id, long_id]()
100 get_flag(value, short_id, long_id);
107 template <
typename option_type,
typename val
idator_type>
108 void add_positional_option(option_type & value,
110 validator_type && option_validator)
112 positional_option_calls.push_back(
113 [
this, &value, option_validator]()
115 get_positional_option(value, option_validator);
120 void parse(argument_parser_meta_data
const & )
122 end_of_options_it =
std::find(argv.begin(), argv.end(),
"--");
126 for (
auto && f : option_calls)
129 for (
auto && f : flag_calls)
132 check_for_unknown_ids();
134 if (end_of_options_it != argv.end())
135 *end_of_options_it =
"";
137 for (
auto && f : positional_option_calls)
140 check_for_left_over_args();
156 template <
typename id_type>
157 static bool is_empty_id(id_type
const &
id)
159 if constexpr (std::same_as<std::remove_cvref_t<id_type>,
std::string>)
162 return is_char<'\0'>(
id);
186 template <
typename iterator_type,
typename id_type>
187 static iterator_type find_option_id(iterator_type begin_it, iterator_type end_it, id_type
const &
id)
198 if constexpr (std::same_as<id_type, char>)
202 return current_arg.
substr(0, full_id.
size()) == full_id;
207 return current_arg.
substr(0, full_id.
size()) == full_id &&
208 (current_arg.
size() == full_id.
size()
209 || current_arg[full_id.
size()] ==
'=');
216 enum class option_parse_result
229 return {
"--" + long_id};
236 static std::string prepend_dash(
char const short_id)
238 return {
'-', short_id};
248 if (short_id ==
'\0')
249 return prepend_dash(long_id);
250 else if (long_id.
empty())
251 return prepend_dash(short_id);
253 return prepend_dash(short_id) +
"/" + prepend_dash(long_id);
261 auto it =
std::find(argv.begin(), end_of_options_it, prepend_dash(long_id));
263 if (it != end_of_options_it)
266 return (it != end_of_options_it);
272 bool flag_is_set(
char const short_id)
277 if (arg[0] ==
'-' && arg.size() > 1 && arg[1] !=
'-')
279 auto pos = arg.find(short_id);
281 if (pos != std::string::npos)
302 template <
typename option_t>
304 option_parse_result parse_option_value(option_t & value,
std::string const & in)
309 if (stream.fail() || !stream.eof())
310 return option_parse_result::error;
312 return option_parse_result::success;
322 template <named_enumeration option_t>
323 option_parse_result parse_option_value(option_t & value,
std::string const & in)
325 auto map = seqan3::enumeration_names<option_t>;
327 if (
auto it = map.find(in); it == map.end())
331 [](
auto pair1,
auto pair2)
333 if constexpr (std::totally_ordered<option_t>)
335 if (pair1.second != pair2.second)
336 return pair1.second < pair2.second;
338 return pair1.first < pair2.first;
341 throw user_input_error{detail::to_string(
"You have chosen an invalid input value: ",
343 ". Please use one of: ",
344 key_value_pairs | std::views::keys)};
351 return option_parse_result::success;
358 return option_parse_result::success;
373 template <detail::is_container_option container_option_t,
typename format_parse_t = format_parse>
374 requires requires (format_parse_t fp,
375 typename container_option_t::value_type & container_value,
377 { fp.parse_option_value(container_value, in) } -> std::same_as<option_parse_result>;
379 option_parse_result parse_option_value(container_option_t & value,
std::string const & in)
381 typename container_option_t::value_type tmp{};
383 auto res = parse_option_value(tmp, in);
385 if (res == option_parse_result::success)
386 value.push_back(tmp);
403 template <arithmetic option_t>
405 option_parse_result parse_option_value(option_t & value,
std::string const & in)
409 if (res.ec == std::errc::result_out_of_range)
410 return option_parse_result::overflow_error;
411 else if (res.ec == std::errc::invalid_argument || res.ptr != &in[in.
size()])
412 return option_parse_result::error;
414 return option_parse_result::success;
427 option_parse_result parse_option_value(
bool & value,
std::string const & in)
433 else if (in ==
"true")
435 else if (in ==
"false")
438 return option_parse_result::error;
440 return option_parse_result::success;
450 template <
typename option_type>
451 void throw_on_input_error(option_parse_result
const res,
455 std::string msg{
"Value parse failed for " + option_name +
": "};
457 if (res == option_parse_result::error)
459 throw user_input_error{msg +
"Argument " + input_value +
" could not be parsed as type "
460 + get_type_name_as_string(option_type{}) +
"."};
465 if (res == option_parse_result::overflow_error)
467 throw user_input_error{msg +
"Numeric argument " + input_value +
" is not in the valid range ["
473 assert(res == option_parse_result::success);
493 template <
typename option_type,
typename id_type>
494 bool identify_and_retrieve_option_value(option_type & value,
498 if (option_it != end_of_options_it)
501 size_t id_size = (prepend_dash(
id)).
size();
503 if ((*option_it).size() > id_size)
505 if ((*option_it)[id_size] ==
'=')
507 if ((*option_it).size() == id_size + 1)
508 throw too_few_arguments(
"Missing value for option " + prepend_dash(
id));
509 input_value = (*option_it).
substr(id_size + 1);
513 input_value = (*option_it).
substr(id_size);
522 if (option_it == end_of_options_it)
523 throw too_few_arguments(
"Missing value for option " + prepend_dash(
id));
524 input_value = *option_it;
528 auto res = parse_option_value(value, input_value);
529 throw_on_input_error<option_type>(res, prepend_dash(
id), input_value);
553 template <
typename option_type,
typename id_type>
554 bool get_option_by_id(option_type & value, id_type
const &
id)
556 auto it = find_option_id(argv.begin(), end_of_options_it,
id);
558 if (it != end_of_options_it)
559 identify_and_retrieve_option_value(value, it,
id);
561 if (find_option_id(it, end_of_options_it,
id) != end_of_options_it)
562 throw option_declared_multiple_times(
"Option " + prepend_dash(
id)
563 +
" is no list/container but declared multiple times.");
565 return (it != end_of_options_it);
579 template <detail::is_container_option option_type,
typename id_type>
580 bool get_option_by_id(option_type & value, id_type
const &
id)
582 auto it = find_option_id(argv.begin(), end_of_options_it,
id);
583 bool seen_at_least_once{it != end_of_options_it};
585 if (seen_at_least_once)
588 while (it != end_of_options_it)
590 identify_and_retrieve_option_value(value, it,
id);
591 it = find_option_id(it, end_of_options_it,
id);
594 return seen_at_least_once;
610 void check_for_unknown_ids()
612 for (
auto it = argv.begin(); it != end_of_options_it; ++it)
615 if (!arg.empty() && arg[0] ==
'-')
621 else if (arg[1] !=
'-' && arg.size() > 2)
623 throw unknown_option(
"Unknown flags " + expand_multiple_flags(arg)
624 +
". In case this is meant to be a non-option/argument/parameter, "
625 +
"please specify the start of arguments with '--'. "
626 +
"See -h/--help for program information.");
630 throw unknown_option(
"Unknown option " + arg
631 +
". In case this is meant to be a non-option/argument/parameter, "
632 +
"please specify the start of non-options with '--'. "
633 +
"See -h/--help for program information.");
650 void check_for_left_over_args()
659 throw too_many_arguments(
"Too many arguments provided. Please see -h/--help for more information.");
682 template <
typename option_type,
typename val
idator_type>
683 void get_option(option_type & value,
689 bool short_id_is_set{get_option_by_id(value, short_id)};
690 bool long_id_is_set{get_option_by_id(value, long_id)};
693 if (short_id_is_set && long_id_is_set && !detail::is_container_option<option_type>)
694 throw option_declared_multiple_times(
"Option " + combine_option_names(short_id, long_id)
695 +
" is no list/container but specified multiple times");
697 if (short_id_is_set || long_id_is_set)
705 throw validation_error(
std::string(
"Validation failed for option ")
706 + combine_option_names(short_id, long_id) +
": " + ex.
what());
713 throw required_option_missing(
"Option " + combine_option_names(short_id, long_id)
714 +
" is required but not set.");
725 void get_flag(
bool & value,
char const short_id,
std::string const & long_id)
727 value = flag_is_set(short_id) || flag_is_set(long_id);
752 template <
typename option_type,
typename val
idator_type>
753 void get_positional_option(option_type & value, validator_type &&
validator)
755 ++positional_option_count;
763 if (it == argv.end())
764 throw too_few_arguments(
"Not enough positional arguments provided (Need at least "
766 +
"). See -h/--help for more information.");
768 if constexpr (detail::is_container_option<
771 assert(positional_option_count == positional_option_calls.size());
775 while (it != argv.end())
777 auto res = parse_option_value(value, *it);
779 throw_on_input_error<option_type>(res,
id, *it);
788 ++positional_option_count;
793 auto res = parse_option_value(value, *it);
795 throw_on_input_error<option_type>(res,
id, *it);
806 throw validation_error(
"Validation failed for positional option " +
std::to_string(positional_option_count)
818 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:247
@ required
Definition auxiliary.hpp:249
constexpr size_t size
The size of a type pack.
Definition type_pack/traits.hpp:143
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.