-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgv.hpp
More file actions
164 lines (144 loc) · 5.15 KB
/
Argv.hpp
File metadata and controls
164 lines (144 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Easy command-line option parsing.
Copyright (c) 2019, Michael Cook <michael@waxrat.com>
*/
#pragma once
#include <type_traits>
#include <limits>
#include <string>
#include <cstdlib>
class Argv {
int& argc_;
char** argv_;
char const* help_text_ = nullptr;
char const* name_ = nullptr;
int argi_ = 0;
bool handling_option_ = false;
// Non-null if we're working our way through a bundle of short options
char* bundle_ = nullptr;
char* peek_arg() const;
void shift_arg();
bool get_opt(char short_opt);
char* get_opt_with_arg(char short_opt);
bool get_opt(char const* long_opt);
char* get_opt_with_arg(char const* long_opt);
void bad_arg(char short_opt, char const*);
void bad_arg(char const* long_opt, char const*);
template <typename T,
typename MINT,
typename MAXT,
typename std::enable_if<std::is_integral<T>::value &&
std::is_signed<T>::value>::type* = nullptr>
static bool string_to(char const* s, T& value, MINT minimum, MAXT maximum)
{
errno = 0;
char* endp;
auto v = ::strtoll(s, &endp, 10);
if (errno != 0 || endp == s || *endp != '\0')
return false;
T r = v;
if (r != v)
return false;
if (r < minimum || r > maximum)
return false;
value = r;
return true;
}
static unsigned long long nonnegative_strtoull(char const*, char **, int);
template <typename T,
typename MINT,
typename MAXT,
typename std::enable_if<std::is_integral<T>::value &&
std::is_unsigned<T>::value>::type* = nullptr>
static bool string_to(char const* s, T& value, MINT minimum, MAXT maximum)
{
errno = 0;
char* endp;
auto v = nonnegative_strtoull(s, &endp, 10);
if (errno != 0 || endp == s || *endp != '\0')
return false;
T r = v;
if (r != v)
return false;
if (r < minimum || r > maximum)
return false;
value = r;
return true;
}
template <typename T,
typename MINT,
typename MAXT,
typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
static bool string_to(char const* s, T& value, MINT minimum, MAXT maximum)
{
errno = 0;
char* endp;
auto v = ::strtold(s, &endp);
if (errno != 0 || endp == s || *endp != '\0')
return false;
if (v < minimum || v > maximum)
return false;
value = v;
return true;
}
public:
Argv(int& argc, char** argv, char const* help_text);
// true if there are any more option arguments to process.
// May modify `argv`.
// Handles any `--help`, `-h` or `--` arguments.
explicit operator bool();
// The program name (the base name of argv[0])
char const* name() const {
return name_;
}
// Write the given text to stderr along with "Try --help"
// and then throw std::runtime_error.
void try_help(std::string const&) const __attribute__((noreturn));
void try_help(char const*) const __attribute__((noreturn));
void show_help() const __attribute__((noreturn));
// Consume a `bool` or `char*` option.
//
// If successful, remove the option from the argument list, assign the
// given `value` argument, and return true. Otherwise, merely return
// false
//
// `short_opt` may be '\0' to indicate there is no short form.
// `long_opt` may be nullptr to indicate no long form.
bool option(char short_opt, char const* long_opt, bool& value);
bool option(char short_opt, char const* long_opt, char const*& value);
bool option(char short_opt, char const* long_opt, char*& value);
// Like bool `option` but only return true if the option was found,
// false otherwise
bool option(char short_opt, char const* long_opt) {
bool ok;
return option(short_opt, long_opt, ok);
}
// Like `option` but increment `value`
bool counter(char short_opt, char const* long_opt, int& value);
// Consume an arithmetic option (integer or floating point).
//
// The converted arithmetic value must be between `minimum` and `maximum`
// (inclusive), or else this function throws std::runtime_error.
template <typename T,
typename MINT = T,
typename MAXT = T,
typename std::enable_if<std::is_arithmetic<T>::value>* = nullptr>
bool option(char short_opt, char const* long_opt, T& value,
MINT minimum = std::numeric_limits<T>::min(),
MAXT maximum = std::numeric_limits<T>::max())
{
auto arg = get_opt_with_arg(short_opt);
if (arg) {
if (!string_to(arg, value, minimum, maximum))
bad_arg(short_opt, arg);
return true;
}
arg = get_opt_with_arg(long_opt);
if (arg) {
if (!string_to(arg, value, minimum, maximum))
bad_arg(long_opt, arg);
return true;
}
return false;
}
};