Sharg 1.2.3-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
sharg::detail::format_base Class Reference

The format that contains all helper functions needed in all formats. More...

#include <sharg/detail/format_base.hpp>

+ Inheritance diagram for sharg::detail::format_base:

Static Protected Member Functions

template<typename value_type , bool verbose = true>
static std::string get_type_name_as_string ()
 Returns the input type as a string (reflection).
 
template<detail::is_container_option container_type, bool verbose = true>
static std::string get_type_name_as_string ()
 Returns the value_type of the input container as a string (reflection).
 
template<typename option_value_type >
static std::string option_type_and_list_info (option_value_type const &)
 Formats the type of a value for the help page printing.
 
template<detail::is_container_option container_type>
static std::string option_type_and_list_info (container_type const &)
 Formats the container and its value_type for the help page printing.
 
static std::string prep_id_for_help (detail::id_pair const &id, bool const is_synopsis=false)
 Formats the option/flag identifier pair for the help page printing.
 
static std::string escape_special_xml_chars (std::string const &original)
 Escapes certain characters for correct output.
 
static std::string expand_multiple_flags (std::string const &flag_cluster)
 Expands multiple one character flag identifiers for pretty help output.
 
template<typename option_type , typename default_type >
static std::string get_default_message (option_type const &option, default_type const &value)
 Returns the default message for the help page.
 
static void print_as_lowercase (std::string const &str)
 Prints a string to std::cout converted to lowercase.
 
static void print_as_uppercase (std::string const &str)
 Prints a string to std::cout converted to uppercase.
 

Detailed Description

The format that contains all helper functions needed in all formats.

Remarks
For a complete overview, take a look at Parser

Member Function Documentation

◆ escape_special_xml_chars()

static std::string sharg::detail::format_base::escape_special_xml_chars ( std::string const & original)
inlinestaticprotected

Escapes certain characters for correct output.

Parameters
[in]originalThe string containing characters to be escaped.
Returns
The original string as their corresponding html/xml representation.

Special characters considered are ", \, &, < and >.

◆ expand_multiple_flags()

static std::string sharg::detail::format_base::expand_multiple_flags ( std::string const & flag_cluster)
inlinestaticprotected

Expands multiple one character flag identifiers for pretty help output.

Parameters
[in]flag_clusterThe string of one character flags.
Returns
A string that lists all flags as a comma separated list.

e.g. "-agdg" becomes "-a, -g, -d and -g".

◆ get_default_message()

template<typename option_type , typename default_type >
static std::string sharg::detail::format_base::get_default_message ( option_type const & option,
default_type const & value )
inlinestaticprotected

Returns the default message for the help page.

Template Parameters
option_typeThe type of the option.
default_typeThe type of the default value.
Parameters
[in]optionThe option to get the default message for.
[in]valueThe default value to get the default message for.
Returns
The default message for the help page (" Default: <default-value>. ").

value is either config.default_message, or the same as option. If the option_type is a std::string or std::filesystem::path, the value is quoted. If the option_type is a container of std::string or std::filesystem::path, each individual value is quoted; if a config.default_message is provided, it will not be quoted.

◆ get_type_name_as_string() [1/2]

template<typename value_type , bool verbose = true>
static std::string sharg::detail::format_base::get_type_name_as_string ( )
inlinestaticprotected

Returns the input type as a string (reflection).

Template Parameters
value_typeThe type whose name is converted to std::string.
verboseWhether to use long names ("signed 8 bit integer") or short names ("int8").
Returns
The type of the value as a string.

◆ get_type_name_as_string() [2/2]

template<detail::is_container_option container_type, bool verbose = true>
static std::string sharg::detail::format_base::get_type_name_as_string ( )
inlinestaticprotected

Returns the value_type of the input container as a string (reflection).

Template Parameters
container_typeThe container type for which to query its value_type.
verboseWhether to use long names ("signed 8 bit integer") or short names ("int8").
Returns
The type of the container's value_type as a string.

◆ option_type_and_list_info() [1/2]

template<detail::is_container_option container_type>
static std::string sharg::detail::format_base::option_type_and_list_info ( container_type const & )
inlinestaticprotected

Formats the container and its value_type for the help page printing.

Template Parameters
container_typeA type that must satisfy the sharg::detail::is_container_option.
Returns
The type of the container value type as a string, encapsulated in "List of".

◆ option_type_and_list_info() [2/2]

template<typename option_value_type >
static std::string sharg::detail::format_base::option_type_and_list_info ( option_value_type const & )
inlinestaticprotected

Formats the type of a value for the help page printing.

Template Parameters
option_value_typeThe type of the option value to get the info for.
Returns
The type of the value as string.

◆ prep_id_for_help()

static std::string sharg::detail::format_base::prep_id_for_help ( detail::id_pair const & id,
bool const is_synopsis = false )
inlinestaticprotected

Formats the option/flag identifier pair for the help page printing.

Parameters
[in]idA sharg::detail::id_pair encapsulating both short and long id.
[in]is_synopsisWhether to use synopsis formatting.
Returns
The name of the short and long id, prepended with (double)dash.

For descriptions (is_synopsis == false): "-i, --integer", "-i", or "--integer". For synopsis (is_synopses == true): "-i|--integer", "-i", or "--integer".

◆ print_as_lowercase()

static void sharg::detail::format_base::print_as_lowercase ( std::string const & str)
inlinestaticprotected

Prints a string to std::cout converted to lowercase.

Parameters
[in]strThe string to print in lowercase.

This could also be generalized:

template <std::ranges::input_range range_t>
static void print_transform(range_t && range, int (*fun)(int))
{
[fun](unsigned char c)
{
return fun(c);
});
}
// Usage:
std::string s{"Hello World"};
print_transform(s, std::toupper);
T is_same_v
T transform(T... args)

Using std::function<int(int)> fun wouldn't work the same way because std::toupper also has a templatized overload. For the function pointer, the overload can be infered, from std::function not.

template <std::ranges::input_range range_t>
static void print_transform(range_t && range, std::function<int(int)> fun)
{
// ...
}
// Usage:
std::string s{"Hello World"};
print_transform(s, static_cast<int(*)(int)>(std::toupper));

Because int (*fun)(int) seems a bit sketchy, and other solutions, like using strong types/enums to decide between upper- and lowercase are more complex, we just have two separate functions.

◆ print_as_uppercase()

static void sharg::detail::format_base::print_as_uppercase ( std::string const & str)
inlinestaticprotected

Prints a string to std::cout converted to uppercase.

Parameters
[in]strThe string to print in uppercase.
See also
print_as_lowercase

The documentation for this class was generated from the following file:
Hide me