Skip to content

Commit 7c05e68

Browse files
committed
Add transform action example
1 parent c8f2c8a commit 7c05e68

2 files changed

Lines changed: 56 additions & 9 deletions

File tree

docs/guide.adoc

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,17 +1202,64 @@ Action to take immediately before each source value is parsed. Any
12021202
number of transform actions can be added.
12031203

12041204
The function should:
1205+
12051206
* Inspect, then optionally change the source value via cli.newValue().
12061207
* Call cli.badUsage() with an error message if there's a problem.
12071208
* Call cli.parseExit() if the program should stop without an error. This could
12081209
be due to an early out like "--version" and "--help".
12091210

1210-
The argument is set, so you can use opt.from() and opt.pos() to get the
1211+
The argument is bound, so you can use opt.from() and opt.pos() to get the
12111212
option name that the value was matched with on the command line and its
12121213
position in argv[].
12131214

1214-
allow
1215-
deny
1215+
Enable "allow" and "deny" to be used to set an access bool:
1216+
[source, C++]
1217+
----
1218+
int main(int argc, char * argv[]) {
1219+
Dim::Cli cli;
1220+
auto & write = cli.opt<bool>("write.")
1221+
.desc("Grants write access to stuff.")
1222+
.transform([](auto & cli, auto & opt, auto & val) {
1223+
// Replace allow/deny with strings convertible to bool.
1224+
if (val == "allow")
1225+
return cli.newValue("1");
1226+
if (val == "deny")
1227+
return cli.newValue("0");
1228+
});
1229+
if (!cli.parse(argc, argv))
1230+
return cli.printError(cerr);
1231+
cout << "Running " << (*write ? "with" : "without")
1232+
<< " write access." << endl;
1233+
return EX_OK;
1234+
}
1235+
----
1236+
1237+
See it in action:
1238+
[source, shell session]
1239+
----
1240+
$ a.out --help
1241+
Usage: a.out [OPTIONS]
1242+
1243+
Options:
1244+
--write Grants write access to stuff.
1245+
1246+
--help Show this message and exit.
1247+
1248+
$ a.out
1249+
Running without write access.
1250+
$ a.out --write
1251+
Running with write access.
1252+
$ # 'true' parsed to true
1253+
$ a.out --write=true
1254+
Running with write access.
1255+
$ # 'deny' transformed to '0' parsed to false
1256+
$ a.out --write=deny
1257+
Running without write access.
1258+
$ # 'disallow' not parsable to bool
1259+
$ a.out --write=disallow
1260+
Error: Invalid '--write' value: disallow
1261+
----
1262+
12161263

12171264
==== Parse Actions
12181265
Sometimes, you want an argument to completely change the execution flow. For
@@ -1289,9 +1336,9 @@ of priority (largest first) with the order added as the tie breaker.
12891336

12901337
The function should:
12911338

1292-
* Check the options new value. Beware that options are process in the order
1293-
they appear on the command line, so comparing with another option is
1294-
usually better done in an <<#after-actions, after action>>.
1339+
* Check, perhaps modify, the options new value. Beware that options are
1340+
processed in the order they appear on the command line, so comparing with
1341+
another option is usually better done in an <<#after-actions, after action>>.
12951342
* Call cli.badUsage() with an error message if there's a problem.
12961343
* Call cli.parseExit() if the program should stop without an error.
12971344

libs/dimcli/cli.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,12 +1946,12 @@ vector<pair<string, double>> Cli::siUnitMapping(
19461946
}
19471947
if (!symbol.empty()) {
19481948
if (flags & fUnitRequire) {
1949-
// Unit symbol required, modify list of suffixes to include it.
1949+
// Unit symbol required, append it to each suffix in list.
19501950
for (auto && kv : units)
19511951
kv.first += symbol;
19521952
} else {
1953-
// Unit symbol optional, add copy of each suffix with symbol
1954-
// appended.
1953+
// Unit symbol optional, add additional copy of each suffix with
1954+
// symbol appended.
19551955
units.reserve(2 * units.size());
19561956
for (auto i = units.size(); i-- > 0;) {
19571957
auto & kv = units[i];

0 commit comments

Comments
 (0)