argagg: argagg::convert Namespace Reference

argagg

Loading...
Searching...
No Matches
Classes | Functions
argagg::convert Namespace Reference

The set of template instantiations that convert C-strings to other types for the option_result::as(), option_results::as(), parser_results::as(), and parser_results::all_as() methods are placed in this namespace. More...

## Classes
struct | converter
| For simple types the main extension point for adding argument conversions is argagg::convert::arg<T>(). However, for complex types such as templated types partial specialization of a helper struct is required. This struct provides that extension point. The default, generic implementation of argagg::convert::arg<T>() calls converter<T>::convert(). More...
struct | converter< csv< T > >
| Partially specializes argagg::convert::converter for the argagg::csv type. More...
struct | converter< cv::Point3_< T > >
| Partially specializes argagg::convert::converter for the cv::Point3_ type. Parses as a comma separated list of components. More...
struct | converter< cv::Point_< T > >
| Partially specializes argagg::convert::converter for the cv::Point_ type. Parses as a comma separated list of components. More...
struct | converter< cv::Rect_< T > >
| Partially specializes argagg::convert::converter for the cv::Rect_ type. Parses as a comma separated list of x, y, width, height. More...
struct | converter< cv::Size_< T > >
| Partially specializes argagg::convert::converter for the cv::Size_ type. Parses as a 'x' separated pair of width and height. More...

## Functions
template<typename T>
T | arg (const char *arg)
| Explicit instantiations of this function are used to convert arguments to types.
template<typename T>
bool | parse_next_component (const char *&s, T &out_arg, const char delim=',')
| A utility function for parsing an argument as a delimited list. To use, initialize a const char* pointer to the start of argument string. Then call parse_next_component(), providing that pointer, a mutable reference to where the parsed argument will go, and optionally the delimiting character. The argument string will be read up to the next delimiting character and then converted using argagg::convert::arg<decltype(out_arg)>(). The pointer is then incremented accordingly. If the delimiting character is no longer found then false is returned meaning that parsing the list can be considered finished.
template<typename T>
T | long_ (const char *arg)
| Templated function for conversion to T using the std::strtol() function. This is used for anything long length or shorter (long, int, short, char).
template<typename T>
T | long_long_ (const char *arg)
| Templated function for conversion to T using the std::strtoll() function. This is used for anything long long length or shorter (long long).
template<>
bool | arg (const char *arg)
template<>
float | arg (const char *arg)
template<>
double | arg (const char *arg)
template<>
const char * | arg (const char *arg)
template<>
std::string | arg (const char *arg)

## Detailed Description

The set of template instantiations that convert C-strings to other types for the option_result::as(), option_results::as(), parser_results::as(), and parser_results::all_as() methods are placed in this namespace.

## Function Documentation

## ◆ arg() [1/6]

template<typename T>

T argagg::convert::arg | ( | const char * | arg | ) |

Explicit instantiations of this function are used to convert arguments to types.

Definition at line 1488 of file argagg.hpp.

## ◆ arg() [2/6]

template<>

bool argagg::convert::arg | ( | const char * | arg | ) |
| inline

Definition at line 1495 of file argagg.hpp.

## ◆ arg() [3/6]

template<>

float argagg::convert::arg | ( | const char * | arg | ) |
| inline

Definition at line 1502 of file argagg.hpp.

## ◆ arg() [4/6]

template<>

double argagg::convert::arg | ( | const char * | arg | ) |
| inline

Definition at line 1520 of file argagg.hpp.

## ◆ arg() [5/6]

template<>

const char * argagg::convert::arg | ( | const char * | arg | ) |
| inline

Definition at line 1538 of file argagg.hpp.

## ◆ arg() [6/6]

template<>

std::string argagg::convert::arg | ( | const char * | arg | ) |
| inline

Definition at line 1545 of file argagg.hpp.

## ◆ long_()

template<typename T>

T argagg::convert::long_ | ( | const char * | arg | ) |
| inline

Templated function for conversion to T using the std::strtol() function. This is used for anything long length or shorter (long, int, short, char).

Definition at line 1413 of file argagg.hpp.

## ◆ long_long_()

template<typename T>

T argagg::convert::long_long_ | ( | const char * | arg | ) |
| inline

Templated function for conversion to T using the std::strtoll() function. This is used for anything long long length or shorter (long long).

Definition at line 1437 of file argagg.hpp.

## ◆ parse_next_component()

template<typename T>

bool argagg::convert::parse_next_component | ( | const char *& | s,
|  | T & | out_arg,
|  | const char | delim = ',' )

A utility function for parsing an argument as a delimited list. To use, initialize a const char* pointer to the start of argument string. Then call parse_next_component(), providing that pointer, a mutable reference to where the parsed argument will go, and optionally the delimiting character. The argument string will be read up to the next delimiting character and then converted using argagg::convert::arg<decltype(out_arg)>(). The pointer is then incremented accordingly. If the delimiting character is no longer found then false is returned meaning that parsing the list can be considered finished.

#include <argagg/argagg.hpp>
struct position3 {
double x;
double y;
double z;
};
namespace argagg {
namespace convert {
template <>
position3 arg(const char* s)
{
position3 result {0.0, 0.0, 0.0};
if (!parse_next_component(s, result.x)) {
// could potentially throw an error if you require that at least two
// components exist in the list
return result;
}
if (!parse_next_component(s, result.y)) {
return result;
}
if (!parse_next_component(s, result.z)) {
return result;
}
return result;
}
} // namespace convert
} // namespace argagg
int main(int argc, char** argv)
{
argagg::parser argparser {{
{ "origin", {"-o", "--origin"},
"origin as position3 specified as a comma separated list of "
"components (e.g. '1,2,3')", 1},
}};
argagg::parser_results args = argparser.parse(argc, argv);
auto my_position = args["origin"].as<position3>();
// ...
return 0;
}
argagg.hpp
argagg::convert
The set of template instantiations that convert C-strings to other types for the option_result::as(),...
Definition argagg.hpp:164
argagg::convert::parse_next_component
bool parse_next_component(const char *&s, T &out_arg, const char delim=',')
A utility function for parsing an argument as a delimited list. To use, initialize a const char* poin...
Definition argagg.hpp:1552
argagg::convert::arg
T arg(const char *arg)
Explicit instantiations of this function are used to convert arguments to types.
Definition argagg.hpp:1488
argagg
There are only two hard things in Computer Science: cache invalidation and naming things (Phil Karlto...
Definition argagg.hpp:96
argagg::parser_results::as
T as(std::size_t i=0) const
Gets a positional argument converted to the given type.
Definition argagg.hpp:895
argagg::parser::parse
parser_results parse(int argc, const char **argv) const
Parses the provided command line arguments and returns the results as parser_results.
Definition argagg.hpp:1167

Definition at line 1552 of file argagg.hpp.