2
3
4
5
6
21
22
24 Switch(std::string name,
bool &value): name(std::move(name)), value(value) {
32
33
34
39 ValueArg(std::string name, T &value,
bool required =
false): name(std::move(name)), value(value),
50
51
53 concept arg = std::same_as<T, Switch>
or sat::concepts::same_template<T, ValueArg>;
57 static_assert(
sat::
traits::always_false_v<T>,
"Unsupported value type");
62 float operator()(
const std::string &s)
const {
69 double operator()(
const std::string &s)
const {
74 template<std::integral T>
76 T operator()(
const std::string &s)
const {
81 template<sat::concepts::enum_type T>
83 T operator()(
const std::string &s)
const {
84 return static_cast<T>(std::stoi(s));
91
92
93
94
95
96
97 inline std::string
parse(
int argc,
char *argv[]) {
99 std::cerr <<
"Specify the input file" << std::endl;
107
108
109
110
111
112
113
114
115
116 template<detail::arg Option, detail::arg ... Options>
117 std::string
parse(
int argc,
char *argv[],
const Option &option,
const Options &... rest) {
118 using namespace std::string_literals;
120 std::cerr <<
"Specify the input file" << std::endl;
124 const std::span options(argv + 2, argv + argc);
125 auto res = std::ranges::find(options, option.name);
126 if (res != options.end()) {
127 if constexpr (std::same_as<Option, Switch>) {
128 option.value =
not option.value;
130 if (++res == options.end()) {
131 throw std::runtime_error(
"Could not find argument for option "s + option.name);
134 option.value = detail::TypeParse<
typename Option::type>()(*res);
135 std::cout <<
"c -- using value " << option.value <<
" for option " << option.name << std::endl;
138 if constexpr (sat::concepts::same_template<Option, ValueArg>) {
139 if (option.required) {
140 throw std::runtime_error(
"Required argument "s + option.name +
" not specified");
143 std::cout <<
"c -- using default value " << option.value <<
" for option " << option.name << std::endl;
147 return parse(argc, argv, rest...);
std::string parse(int argc, char *argv[])
Definition cli.hpp:97
std::string parse(int argc, char *argv[], const Option &option, const Options &... rest)
Definition cli.hpp:117
namespace containing various type traits. Prefer using the concepts.
Definition concepts.hpp:21
Definition basic_structures.cpp:10
Argument specification for switch arguments.
Definition cli.hpp:23
Argument specification for value arguments.
Definition cli.hpp:36