28 namespace seqan3::detail
41 template <
typename value_type>
42 static std::string get_type_name_as_string(value_type
const & )
44 using type = std::decay_t<value_type>;
58 std::filesystem::path>;
59 std::vector<std::string> names{
"signed 8 bit integer",
60 "unsigned 8 bit integer",
61 "signed 16 bit integer",
62 "unsigned 16 bit integer",
63 "signed 32 bit integer",
64 "unsigned 32 bit integer",
65 "signed 64 bit integer",
66 "unsigned 64 bit integer",
72 "std::filesystem::path"};
74 if constexpr (list_traits::contains<type, types>)
75 return names[list_traits::find<type, types>];
77 return detail::type_name_as_string<value_type>;
84 template <sequence_container container_type>
86 requires (!std::is_same_v<container_type, std::string>)
88 static std::string get_type_name_as_string(container_type
const & )
90 typename container_type::value_type tmp{};
91 return get_type_name_as_string(tmp);
99 template <
typename option_value_type>
100 static std::string option_type_and_list_info(option_value_type
const & value)
102 return (
"(\\fI" + get_type_name_as_string(value) +
"\\fP)");
111 template <
typename container_type>
115 static std::string option_type_and_list_info(container_type
const &
container)
117 return (
"(\\fIList\\fP of \\fI" + get_type_name_as_string(
container) +
"\\fP's)");
127 static std::string prep_id_for_help(
char const short_id, std::string
const & long_id)
131 if (short_id !=
'\0')
132 term =
"\\fB-" + std::string(1, short_id) +
"\\fP";
134 if (short_id !=
'\0' && !long_id.empty())
137 if (!long_id.empty())
138 term.append(
"\\fB--" + long_id +
"\\fP");
149 std::string escape_special_xml_chars(std::string
const & original)
152 escaped.reserve(original.size());
154 for (
auto c : original)
157 escaped.append(
""");
159 escaped.append(
"'");
161 escaped.append(
"&");
163 escaped.append(
"<");
165 escaped.append(
">");
167 escaped.push_back(c);
179 static std::string expand_multiple_flags(std::string
const & flag_cluster)
182 auto it{flag_cluster.begin()};
184 if (flag_cluster[0] ==
'-')
187 for (; it != flag_cluster.end() - 1; ++it)
188 tmp.append(
"-" + std::string(1, *it) +
", ");
190 tmp.erase(tmp.find_last_of(
','));
191 tmp.append(
" and -" + std::string(1, flag_cluster[flag_cluster.size() - 1]));
201 template <
typename derived_type>
202 class format_help_base :
public format_base
208 format_help_base() =
default;
209 format_help_base(format_help_base
const & pf) =
default;
210 format_help_base & operator=(format_help_base
const & pf) =
default;
211 format_help_base(format_help_base &&) =
default;
212 format_help_base & operator=(format_help_base &&) =
default;
213 ~format_help_base() =
default;
219 format_help_base(std::vector<std::string>
const & names,
bool const advanced) :
220 command_names{names}, show_advanced_options{advanced}
228 template <
typename option_type,
typename val
idator_type>
229 void add_option(option_type & value,
231 std::string
const & long_id,
232 std::string
const & desc,
234 validator_type && option_validator)
236 std::string
id = prep_id_for_help(short_id, long_id) +
" " + option_type_and_list_info(value);
237 std::string info{desc};
238 info += ((spec &
option_spec::REQUIRED) ? std::string{
" "} : detail::to_string(
" Default: ", value,
". "));
239 info += option_validator.get_help_page_message();
240 store_help_page_element([
this,
id, info] () { derived_t().print_list_item(
id, info); }, spec);
246 void add_flag(
bool & SEQAN3_DOXYGEN_ONLY(value),
248 std::string
const & long_id,
249 std::string
const & desc,
252 std::string
id = prep_id_for_help(short_id, long_id);
253 store_help_page_element([
this,
id, desc] () { derived_t().print_list_item(
id, desc); }, spec);
259 template <
typename option_type,
typename val
idator_type>
260 void add_positional_option(option_type & value,
261 std::string
const & desc,
262 validator_type & option_validator)
264 std::string msg = option_validator.get_help_page_message();
266 positional_option_calls.push_back([
this, &value, desc, msg] ()
268 ++positional_option_count;
269 derived_t().print_list_item(detail::to_string(
"\\fBARGUMENT-", positional_option_count,
"\\fP ",
270 option_type_and_list_info(value)),
274 ? detail::to_string(
" Default: ", value,
". ")
275 : std::string{
" "}) +
283 void parse(argument_parser_meta_data & parser_meta)
287 derived_t().print_header();
289 if (!meta.synopsis.empty())
291 derived_t().print_section(
"Synopsis");
292 derived_t().print_synopsis();
295 if (!meta.description.empty())
297 derived_t().print_section(
"Description");
298 for (
auto desc : meta.description)
302 if (!command_names.empty())
304 derived_t().print_section(
"Subcommands");
305 derived_t().print_line(
"This program must be invoked with one of the following subcommands:",
false);
306 for (std::string
const & name : command_names)
307 derived_t().print_line(
"- \\fB" + name +
"\\fP",
false);
308 derived_t().print_line(
"See the respective help page for further details (e.g. by calling " +
309 meta.app_name +
" " + command_names[0] +
" -h).",
true);
310 derived_t().print_line(
"The following options below belong to the top-level parser and need to be "
311 "specified \\fBbefore\\fP the subcommand key word. Every argument after the "
312 "subcommand key word is passed on to the corresponding sub-parser.",
true);
316 if (!positional_option_calls.empty())
317 derived_t().print_section(
"Positional Arguments");
320 for (
auto f : positional_option_calls)
324 if (!parser_set_up_calls.empty())
325 derived_t().print_section(
"Options");
328 for (
auto f : parser_set_up_calls)
331 if (!meta.examples.empty())
333 derived_t().print_section(
"Examples");
334 for (
auto example : meta.examples)
338 derived_t().print_footer();
340 std::exit(EXIT_SUCCESS);
346 void add_section(std::string
const & title,
option_spec const spec)
348 store_help_page_element([
this, title] () { derived_t().print_section(title); }, spec);
354 void add_subsection(std::string
const & title,
option_spec const spec)
356 store_help_page_element([
this, title] () { derived_t().print_subsection(title); }, spec);
362 void add_line(std::string
const & text,
bool is_paragraph,
option_spec const spec)
364 store_help_page_element([
this, text, is_paragraph] () { derived_t().print_line(text, is_paragraph); }, spec);
370 void add_list_item(std::string
const & key, std::string
const & desc,
option_spec const spec)
372 store_help_page_element([
this, key, desc] () { derived_t().print_list_item(key, desc); }, spec);
389 argument_parser_meta_data meta;
396 derived_type & derived_t()
398 return static_cast<derived_type &
>(*this);
402 void print_synopsis()
404 for (
unsigned i = 0; i < meta.synopsis.size(); ++i)
406 std::string text =
"\\fB";
407 text.append(meta.synopsis[i]);
408 text.insert(text.find_first_of(
" \t"),
"\\fP");
410 derived_t().print_line(text,
false);
417 void print_line(std::string
const & text)
419 derived_t().print_line(text,
true);
423 std::vector<std::function<void()>> parser_set_up_calls;
425 std::vector<std::function<void()>> positional_option_calls;
427 unsigned positional_option_count{0};
429 std::vector<std::string> command_names{};
431 bool show_advanced_options{
true};
444 void store_help_page_element(std::function<
void()> printer,
option_spec const spec)
447 parser_set_up_calls.push_back(
std::move(printer));
Provides auxiliary information.
Provides parser related exceptions.
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
option_spec
Used to further specify argument_parser options/flags.
Definition: auxiliary.hpp:233
@ REQUIRED
Definition: auxiliary.hpp:235
@ ADVANCED
Definition: auxiliary.hpp:240
@ HIDDEN
Definition: auxiliary.hpp:244
meta::list< types... > type_list
Type that contains multiple types, an alias for meta::list.
Definition: type_list.hpp:31
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
The (most general) container concept as defined by the standard library.
A more refined container concept than seqan3::container.
Provides traits for seqan3::type_list.
Provides traits to inspect some information of a type, for example its name.
Provides some standard validators for (positional) options.