Return a conversion map from std::string_view to option_type.
- Template Parameters
-
your_type | Type of the value to retrieve the conversion map for. |
- Parameters
-
value | The value is not accessed, only its type is used. |
- Returns
- A std::unordered_map<std::string_view, your_type> that maps a string identifier to a value of your_type.
This is a function object. Invoke it with the parameter(s) specified above.
It acts as a wrapper and looks for two possible implementations (in this order):
- A static member
enumeration_names
in sharg::custom::parsing<your_type>
that is of type std::unordered_map<std::string_view, your_type>>
.
- A free function
enumeration_names(your_type const a)
in the namespace of your type (or as friend
) which returns a std::unordered_map<std::string_view, your_type>>
.
Example
If you are working on a type in your namespace, you should implement a free function like this:
namespace foo
{
enum class bar
{
one,
two,
three
};
auto enumeration_names(bar)
{
}
}
int main(int argc, char const * argv[])
{
foo::bar value{};
parser.add_option(
value,
sharg::config{.
short_id =
'f', .long_id =
"foo", .description =
"Give me a value for foo.", .validator = vali});
try
{
parser.parse();
}
{
return -1;
}
}
Meta-header for the Parser module .
Parser exception that is thrown whenever there is an error while parsing the command line arguments.
Definition exceptions.hpp:40
The Sharg command line parser.
Definition parser.hpp:154
A validator that checks whether a value is inside a list of valid values.
Definition validators.hpp:175
Option struct that is passed to the sharg::parser::add_option() function.
Definition config.hpp:43
char short_id
The short identifier for the option (e.g. 'a', making the option callable via -a).
Definition config.hpp:53
Only if you cannot access the namespace of your type to customize you may specialize the sharg::custom::parsing struct like this:
namespace sharg::custom
{
template <>
struct parsing<
std::errc>
{
{"timed_out", std::errc::timed_out},
{"invalid_argument", std::errc::invalid_argument},
{"io_error", std::errc::io_error}};
};
}
int main(int argc, char const * argv[])
{
parser.add_option(value,
.long_id = "errc",
.description = "Give me a std::errc value.",
.validator = validator});
try
{
parser.parse();
}
{
return -1;
}
return 0;
}
auto const enumeration_names
Return a conversion map from std::string_view to option_type.
Definition enumeration_names.hpp:214
Customisation point
This is a customisation point (see Customisation). To specify the behaviour for your type, simply provide one of the two functions specified above.
This entity is experimental and subject to change in the future. Experimental since version 1.0.