@@ -1202,17 +1202,64 @@ Action to take immediately before each source value is parsed. Any
12021202number of transform actions can be added.
12031203
12041204The 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
12111212option name that the value was matched with on the command line and its
12121213position 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
12181265Sometimes, 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
12901337The 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
0 commit comments