-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.txt
79 lines (68 loc) · 3.21 KB
/
README.txt
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
FEATURES
--------
(Just to codify existing behavior as "expected" (and testable), rather than leaving them
"accidental"...)
- [x] Classic named (option) and unnamed (positional) arguments
- [x] intermixed
- [x] Prefix char either - or / freely mixed,
- [ ] but that can be disabled
- [x] Both short and long options: -x --long
- [x] Long options only as --long (so //whaaat is always positional)
- [x] Aggregated short options: -xyz
- [x] with the last one possibly taking values: -xyz param-for-z
- [x] multiple values, too: -xyZ Zval1 Zval2
- [x] greedy, too: -xyZ Zval1 Zval2 ... Zvalx-up to -this
- [x] A bare -- turns off named args. for the rest of the cmdline by default, but it
- [x] can be configured to be a regular positional arg. (*for now it always is!*)
- [x] Options are predicates by default, with simple bool checks: args["x"], args["long"]
- [x] Long options can take values without config.: --name=val
- [x] Any option can take values if configured so: -a file --except *pattern
- [x] long ones also without = in this case
- [x] query (as std::string): args("a") -> "file", args("except") -> "*pattern"
- [x] Outputs also available in args.named() -> std::map, args.positional() -> std::vector
- [x] Use the non-const accessors to modify these containers as you wish
(they are *yours*, right? ;) especially after parsing...)
- [x] Options (short or long) can also have multiple parameters --multi a b c
- [x] query like: args("multi", 2) -> "c",
- [x] or get them all with args.named("multi") -> std::vector{"a", "b", "c"}
- [x] Options can be set to "greedy" to take each value up to the next opt.,
- [x] or only a fixed n. of values
- [x] Repeated options override earlier ones by default
- [x] Repeated options can also be set to
- [x] be ignored,
- [x] append (for multi-val opts.),
- [x] fail
- [x] Parsing on construction: Args args(argc, argv)
- [x] Deferred parsing: Args args; args.parse(argc, argv)
- [x] Reparsing with different config: reparse(flags = Defaults, rules = {})
- [x] The instance can be reused for completely new parses, too:
parse(new_argc, new_argv, flags = Defaults, rules = {})
- [x] The last used argc/argv are available as args.argc, args.argv
(in case they're needed outside of main(), e.g. via myApp.args)
- [x] exename(): argv[0], but stripping the path and
- [x] the extension (".exe" by default, but -> exename(false, ".mysuffix"),
- [x] unless its "true value" :) is requested with exename(true)
- [x] Quick bool check if there have been any args: if (args), if (!args)
EXAMPLES
--------
- A simple one:
#include "Args.hpp"
#include <iostream>
using std::cout;
int main(int argc, char** argv)
{
Args args(argc, argv);
if (args)
cout << "Some args are present.\n";
if (!args || args["h"])
cout << "Usage: " << args.exename() << " "
<< "[-h] [-x] [--long] [whatever...]\n";
if (args["x"])
cout << " 'x' was set\n";
if (args["long"])
cout << " 'long' was set"
<< (args("long").empty() ? "" : " to " + args("long"))
<< '\n';
for (auto a: args.positional())
cout << " positional arg.: " << a << '\n';
}