Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
sharg::custom::parsing< t > Struct Template Reference

A type that can be specialised to provide customisation point implementations for the sharg::parser such that third party types may be adapted. More...

#include <sharg/enumeration_names.hpp>

Detailed Description

template<typename t>
struct sharg::custom::parsing< t >

A type that can be specialised to provide customisation point implementations for the sharg::parser such that third party types may be adapted.

Template Parameters
tThe type you wish to specialise for.

Named Enumerations

In order to use a third party type within the sharg::parser::add_option or sharg::parser::add_positional_option call, you can specialise this struct in the following way:

// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
#include <system_error>
#include <sharg/all.hpp>
namespace sharg::custom
{
// Specialise the sharg::custom::parsing data structure to enable parsing of std::errc.
template <>
struct parsing<std::errc>
{
// Specialise a mapping from an identifying string to the respective value of your type Foo.
{"no_error", std::errc{}},
{"timed_out", std::errc::timed_out},
{"invalid_argument", std::errc::invalid_argument},
{"io_error", std::errc::io_error}};
};
} // namespace sharg::custom
int main(int argc, char const * argv[])
{
std::errc value{};
sharg::parser parser{"my_program", argc, argv};
// Because of the parsing struct and
// the static member function enumeration_names
// you can now add an option that takes a value of type std::errc:
auto validator = sharg::value_list_validator{(sharg::enumeration_names<std::errc> | std::views::values)};
parser.add_option(value,
.long_id = "errc",
.description = "Give me a std::errc value.",
.validator = validator});
try
{
parser.parse();
}
catch (sharg::parser_error const & ext) // the user did something wrong
{
std::cerr << "[PARSER ERROR] " << ext.what() << "\n"; // customize your error message
return -1;
}
return 0;
}
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
auto const enumeration_names
Return a conversion map from std::string_view to option_type.
Definition enumeration_names.hpp:214
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
A type that can be specialised to provide customisation point implementations for the sharg::parser s...
Definition enumeration_names.hpp:48
T what(T... args)

Please note that by default the t const, t & and t const & specialisations of this class inherit the specialisation for t so you usually only need to provide a specialisation for t.

Note
Only use this if you cannot provide respective functions in your namespace. See the tutorial Parsing command line arguments with Sharg for an example of customising a type within your own namespace.
Remarks
For a complete overview, take a look at Parser

This entity is experimental and subject to change in the future. Experimental since version 1.0.


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