-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandLine.h
More file actions
488 lines (422 loc) · 17.7 KB
/
CommandLine.h
File metadata and controls
488 lines (422 loc) · 17.7 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//
// Author Wild Coast Solutions
// David Hamilton
//
// This file is distributed under the MIT License. See the accompanying file
// LICENSE.txt or http://www.opensource.org/licenses/mit-license.php for terms
// and conditions.
//
// This file contains an implementation of a simple command line argument
// handling library for console applications.
//
// Project url: https://github.com/WildCoastSolutions/CommandLine
#ifndef WILD_COMMANDLINE_COMMANDLINE_H
#define WILD_COMMANDLINE_COMMANDLINE_H
#include <string>
#include <initializer_list>
#include <list>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <iomanip>
namespace Wild
{
namespace CommandLine
{
// Whether an arg or flag is optional or required
enum class Ordinality
{
Required,
Optional
};
// But "ordinality" is not a common concept, so simplify for reading
typedef Ordinality Is;
// Represents a single argument
// e.g. -v, --version
class Arg
{
public:
Arg(){};
// Specify the name, letter, description and whether required for a command line arg
// Using this constructor without possible values means that any value is accepted
Arg(
const std::string &name,
const std::string &letter,
const std::string &description,
Ordinality ordinality = Is::Optional) :
name(name),
letter(letter),
description(description),
defaultValue(""),
defaultValueSet(false),
ordinality(ordinality),
isFlag(false)
{
CheckValidity();
}
// Specify the name, letter, description and whether required for a command line arg
// Using this constructor without possible values means that any value is accepted
Arg(
const std::string &name,
const std::string &letter,
const std::string &description,
const std::string &defaultValue) :
name(name),
letter(letter),
description(description),
defaultValue(defaultValue),
defaultValueSet(true),
ordinality(Is::Optional),
isFlag(false)
{
CheckValidity();
}
// Set the name, letter and description, possible values and default value.
// If a default value is given then by definition the arg is optional
Arg(
const std::string &name,
const std::string &letter,
const std::string &description,
std::initializer_list<std::string> possibleValues,
const std::string &defaultValue) :
name(name),
letter(letter),
description(description),
possibleValues(possibleValues),
defaultValue(defaultValue),
defaultValueSet(true),
ordinality(Is::Optional),
isFlag(false)
{
if (!IsValidValue(defaultValue)) throw std::invalid_argument("default value " + defaultValue + " is not present in allowed values");
CheckValidity();
}
// Set the name, letter and description, possible values, default value
// and whether required for a command line argument
Arg(
const std::string &name,
const std::string &letter,
const std::string &description,
std::initializer_list<std::string> possibleValues,
Ordinality ordinality = Is::Optional) :
name(name),
letter(letter),
description(description),
possibleValues(possibleValues),
defaultValue(""),
defaultValueSet(false),
ordinality(ordinality),
isFlag(false)
{
CheckValidity();
}
// Determines if a value passed to a command line argument is in the list of possible values
// If the list of possible values is empty the answer is yes
bool IsValidValue(const std::string &value)
{
// If no set was specified that means we allow any value for this argument
if (possibleValues.size() == 0) return true;
return (possibleValues.find(value) != possibleValues.end());
}
bool IsFlag() { return isFlag; }
bool IsRequired() { return ordinality == Is::Required; }
// Full name of the command e.g. "version", used as "--version"
std::string name;
// Letter for the command e.g. "v", used as "-v"
std::string letter;
// Descriptions, used when printing the usage
std::string description;
// Possible values
std::set<std::string> possibleValues;
std::string defaultValue;
bool defaultValueSet;
// Required or Optional
Ordinality ordinality;
bool isFlag = false;
bool isPositional = false;
protected:
virtual void CheckValidity()
{
if (name.size() < 2) throw std::invalid_argument("argument name must be two or more letters");
if (!(letter.size() == 1 || letter.size() == 0))
throw std::invalid_argument("argument letter must be one letter or blank");
}
};
class PositionalArg : public Arg
{
public:
PositionalArg(const std::string &name, const std::string &description, Ordinality ordinality = Is::Required) :
Arg(name, "", description, ordinality)
{
isPositional = true;
}
};
class Flag : public Arg
{
public:
Flag(
const std::string &name,
const std::string &letter,
const std::string &description) :
Arg(name, letter, description, Is::Optional)
{
isFlag = true;
CheckValidity();
}
};
typedef Arg Option;
// Represents all arguments the command line supports
// All supported args are initialised in the constructor, then Parse is called with argc and argv
// If Parse is successful Args can be queried to get command line argument values
//
// e.g.
//
// Args args({
// Arg("verbose", "v", "Display version information"),
// Arg("another-flag", "a", "Another flag"),
// Arg("colour", "c", "Colour", { "red", "green", "blue" }),
// });
//
// e.g. with command line "-v --colour red -a"
// if(!args.Parse(argc, argv)){ fail & print usage };
//
// if(args.IsSet("verbose")){ set verbosity }
// string colour = args.Get("colour");
//
//
//
class Args
{
public:
Args(const std::initializer_list<Arg> &args)
{
for (auto arg : args)
{
if(m_args.count(arg.name) > 0)
throw std::invalid_argument("cannot have two arguments with the same name");
//if (arg.isPositional &&)
m_args[arg.name] = arg;
m_insertionOrder.push_back(arg.name);
if (arg.letter.size() > 0)
{
if (m_argLookup.count(arg.letter) > 0)
throw std::invalid_argument("cannot have two arguments with the same letter");
m_argLookup[arg.letter] = arg.name;
}
else
{
m_orderedArgNames.push_back(arg.name);
}
ResetValues();
}
}
bool Parse(int argc, char* argv[])
{
std::list<std::string> args(argv + 1, argv + argc);
return Parse(args);
}
bool Parse(const std::list<std::string> &commandLine)
{
try
{
// Clear values in case Parse is called more than once
ResetValues();
// An empty command line is ok as long as no args were required
if (commandLine.size() == 0)
{
for (auto i : m_args)
{
if (i.second.IsRequired())
throw std::invalid_argument(i.second.name + " is required but was not set");
}
return true;
}
auto i = commandLine.begin();
auto j = commandLine.begin();
j++;
Arg arg;
std::string nameOrValue;
std::string name;
std::string value;
while (i != commandLine.end())
{
nameOrValue = StripDashes(*i);
if (!LookupArg(nameOrValue, arg))
{
// If arg doesn't have a name, it could be an ordered arg, where it's order is used to match with the name
if(m_currentOrderedArgIndex >= m_orderedArgNames.size())
throw std::invalid_argument("couldn't find " + *i + " in specified list of arguments");
std::string name = m_orderedArgNames[m_currentOrderedArgIndex];
m_argValues[name] = nameOrValue;
m_currentOrderedArgIndex++;
}
else
{
name = nameOrValue;
if (arg.isFlag)
{
m_argValues[name] = "";
}
else
{
if (j == commandLine.end()) throw std::invalid_argument("argument " + *i + " given without a value");
value = *j;
if (!arg.IsValidValue(value)) throw std::invalid_argument("value " + value + " for argument " + *i + " isn't one of the options");
m_argValues[name] = value;
i++;
if (i == commandLine.end()) break;
j++;
}
}
i++;
if (i == commandLine.end()) break;
j++;
}
// Check all required flags and args have been set
for (auto i : m_args)
{
if (i.second.IsRequired())
if (!IsSet(i.second.name))
{
throw std::invalid_argument(i.second.name + " is required but was not set");
}
}
}
catch (std::invalid_argument &e)
{
std::cout << "Parsing command line failed, details: " << e.what() << std::endl;
return false;
}
return true;
}
bool IsSet(const std::string &name)
{
return m_argValues.count(name) > 0;
}
std::string Get(const std::string &name)
{
return m_argValues[name];
}
int GetAsInt(const std::string &name)
{
int i;
std::istringstream(m_argValues[name]) >> i;
return i;
}
bool GetAsBool(const std::string &name)
{
bool b;
std::istringstream(m_argValues[name]) >> std::boolalpha >> b;
return b;
}
float GetAsFloat(const std::string &name)
{
float f;
std::istringstream(m_argValues[name]) >> f;
return f;
}
// Uses what we know about the args to construct a readable usage string
std::string Usage(const std::string &appName, bool removeNewLines = false, int numSpacesBeforeDescription = 20)
{
std::stringstream s;
std::stringstream line;
line << "usage: " << appName << " ";
for (auto i : m_insertionOrder)
{
Arg a = m_args[i];
s << std::setw(numSpacesBeforeDescription) << std::left << std::setfill(' ') << a.letter + (a.letter.size() > 0 ? ", " : "") + a.name;
s << std::setw(numSpacesBeforeDescription) << std::left << std::setfill(' ') << a.description;
s << std::endl;
if (a.possibleValues.size() > 0)
{
s << std::setw(numSpacesBeforeDescription) << std::left << std::setfill(' ') << "\t";
s << "options: ";
std::string values;
for (auto v : a.possibleValues)
values += "|" + v;
s << values.substr(1) << std::endl;
}
if (a.defaultValueSet)
{
s << std::setw(numSpacesBeforeDescription) << std::left << std::setfill(' ') << "\t";
s << "default: " << a.defaultValue << std::endl;
}
if (a.IsRequired())
{
s << std::setw(numSpacesBeforeDescription) << std::left << std::setfill(' ') << "\t";
s << "required" << std::endl;
}
// Create first line of usage
if (a.IsFlag())
{
if (a.IsRequired())
line << "-" << a.letter << " ";
else
line << "[-" << a.letter << "] ";
}
else
{
if (a.IsRequired())
if(a.letter.size() == 0)
line << "<" << a.name << "> ";
else
line << "-" << a.letter << " <" << a.name << "> ";
else
line << "[-" << a.letter << " " << a.name << "] ";
}
if (!removeNewLines) s << std::endl;
}
return line.str() + "\n\n" + s.str();
}
private:
bool LookupArg(std::string &name, Arg &arg)
{
if (m_argLookup.count(name) > 0)
name = m_argLookup[name];
if (m_args.count(name) > 0)
{
arg = m_args[name];
return true;
}
else
return false;
}
std::string StripDashes(const std::string &s)
{
if (s.size() == 0) throw std::invalid_argument("argument needs to be at least one character");
std::string first = s.substr(0, 1);
if (first != "-") return s; // could be ordered arg
std::string second = s.substr(1, 1);
std::string name;
if (second == "-")
name = s.substr(2);
else
name = s.substr(1);
return name;
}
void ResetValues()
{
m_argValues.clear();
m_currentOrderedArgIndex = 0;
for (auto i : m_args)
{
if (i.second.defaultValueSet)
m_argValues[i.second.name] = i.second.defaultValue;
}
}
std::map<std::string, Arg> m_args;
std::vector<std::string> m_insertionOrder;
std::vector<std::string> m_orderedArgNames;
std::size_t m_currentOrderedArgIndex = 0;
std::map<std::string, std::string> m_argLookup;
std::map<std::string, std::string> m_argValues;
// Afer an optional positional arg, all remaining ones must be optional as well
bool m_seenOptionalPositionalArg = false;
};
}
}
#endif // #ifndef WILD_COMMANDLINE_COMMANDLINE_H