FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpputils/include/cpputils/argparse.h at master · nlitsme/cpputils · GitHub
nlitsme
/
cpputils
Public
Notifications
You must be signed in to change notification settings
Fork
10
Star
24
Code
Issues
1
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpputils
/
include
/
cpputils
/
argparse.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
281 lines (242 loc) · 7.28 KB
Breadcrumbs
cpputils
/
include
/
cpputils
/
argparse.h
Copy path
File metadata and controls
executable file
·
281 lines (242 loc) · 7.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
(C) 2003 XDA Developers itsme@xs4all.nl
*
* $Header$
*/
#
ifndef
__ARGS_H__
#
include
<
string
>
#
include
<
algorithm
>
#
include
<
stdexcept
>
#
include
<
stdlib.h
>
#
include
<
cpputils/stringlibrary.h
>
/*
// cmd -apple -a 1234 first second -
for (auto& arg : ArgParser(argc, argv))
switch (arg.option())
{
case 'a':
if (arg.match("-apple")) {
}
else {
a_arg = arg.getint();
}
break;
case '-':
if (arg.match("--long"))
a_long = true;
break;
case 0:
usestdin = true;
break;
case -1:
switch(n++)
{
case 0: first = arg.getstr(); break;
case 1: second = arg.getstr(); break;
}
}
*/
//
TODO - support floating point numbers
//
TODO - support options with multiple arguments.
class
ArgParser
{
struct
ArgIterator
{
const
char
**argv;
//
argument list
int
argc;
//
nr of argument strings
int
i;
//
current argument
const
char
*p;
//
position in current argument
const
char
*pend;
//
end of current argument
bool
longmatch;
bool
isoption;
bool
_options_ended =
false
;
//
detects '--'
void
setcurrent
()
{
longmatch =
false
;
isoption =
false
;
if
(i==argc) {
p = pend =
nullptr
;
return
;
}
p = argv[i];
pend = p +
stringlength
(p);
}
ArgIterator
(
const
char
**argv,
int
i,
int
argc)
: argv(argv), argc(argc), i(i)
{
setcurrent
();
}
friend
bool
operator
!=(
const
ArgIterator& lhs,
const
ArgIterator& rhs)
{
return
!(lhs.
i
==rhs.
i
&& lhs.
p
== rhs.
p
);
}
ArgIterator&
operator
*() {
return
*
this
; }
ArgIterator&
operator
++()
{
if
(i==argc)
//
calller should throw when nescesary.
return
*
this
;
i++;
setcurrent
();
return
*
this
;
}
/*
* get single option character
* returns -1 for non option arguments
* returns 0 for single dash '-'
*/
int
option
()
{
if
(i==argc)
throw
std::range_error
(
"
arg out of range
"
);
if
(!_options_ended && *p==
'
-
'
) {
//
a single '-' by itself is not considered an option,
//
so 'getstr()' will return "-"
if
(
optionterminator
())
_options_ended =
true
;
isoption = p[
1
]!=
0
;
return
p[
1
];
}
isoption =
false
;
return
-
1
;
}
/*
* matches long option names
* the name should include the leading option dash(es)
*
* like: match("--verbose")
*
* the caller must make sure the matching order does not
* lead to ambiguities.
*
* When the name matches, the current ptr will point after the match.
*/
bool
match
(
const
char
*name)
{
if
(i==argc)
throw
std::range_error
(
"
arg out of range
"
);
auto
namelen =
stringlength
(name);
if
(namelen >
stringlength
(p))
return
false
;
if
(
std::equal
(name, name+namelen, p)) {
p += namelen;
longmatch =
true
;
return
true
;
}
return
false
;
}
/*
* checks for '--', usually used to end option processing
*/
bool
optionterminator
()
{
if
(i==argc)
throw
std::range_error
(
"
arg out of range
"
);
if
(p+
2
!=pend)
return
false
;
return
p[
0
]==
'
-
'
&& p[
1
]==
'
-
'
;
}
/*
* gets the next argument string
*
* long arguments can have a '=' after the option name
*
* --number=1234
*
*/
const
char
*
getstr
()
{
if
(i==argc)
throw
std::range_error
(
"
arg out of range
"
);
if
(isoption && !longmatch) {
//
skips '-' + optionchar
p +=
2
;
if
(*p==
0
) {
//
-opt <space> argument
operator
++();
}
//
-opt<argument>
}
else
if
(longmatch) {
//
long match need not match the full word.
p =
std::find
(p, pend,
'
=
'
);
if
(p!=pend) {
//
--longopt '=' argument
return
p+
1
;
}
//
--longopt <space> argument
operator
++();
}
//
check again if we are out of options
if
(i==argc)
throw
std::range_error
(
"
arg out of range
"
);
return
p;
}
const
char
*
getfullarg
()
{
return
argv[i];
}
/*
* return count for a repeated option:
*
* -vvv -> returns 3
*
*/
int
count
()
{
if
(i==argc)
throw
std::range_error
(
"
arg out of range
"
);
if
(longmatch)
throw
std::logic_error
(
"
can't count a --long option
"
);
char
opt = p[
1
];
const
char
*q = p+
1
;
while
(q!=pend && *q==opt)
++q;
if
(q!=pend)
throw
std::logic_error
(
"
counted options must all be equal
"
);
return
pend - p -
1
;
}
/*
* gets the integer after the current option.
* either in the same string, or in the next full argument.
*/
int64_t
getint
()
{
getstr
();
//
ignoring result, we use 'p' directly.
auto
res =
parsesigned
(p, pend,
0
);
if
(res.
second
!= pend)
throw
std::logic_error
(
"
characters trailing number
"
);
return
res.
first
;
}
uint64_t
getuint
()
{
getstr
();
//
ignoring result, we use 'p' directly.
auto
res =
parseunsigned
(p, pend,
0
);
if
(res.
second
!= pend)
throw
std::logic_error
(
"
characters trailing number
"
);
return
res.
first
;
}
double
getdouble
()
{
getstr
();
//
ignoring result, we use 'p' directly.
//
note: can't use <charconv> std::from_chars<double>, since that is not implemented in libc++ yet.
char
*numend;
double
res =
strtod
(p, &numend);
if
(numend != pend)
throw
std::logic_error
(
"
characters trailing number
"
);
return
res;
}
};
int
argc;
const
char
**argv;
public:
ArgParser
(
int
argc,
const
char
**argv)
: argc(argc), argv(argv)
{
}
ArgParser
(
int
argc,
char
**argv)
: argc(argc), argv(
const_cast
<
const
char
**>(argv))
{
}
auto
begin
() {
return
ArgIterator
(argv,
1
, argc); }
auto
end
() {
return
ArgIterator
(argv, argc, argc); }
};
#
define
__ARGS_H__
#
endif
Back
|
FazBrowse Home
|
New Git URL