Skip to content

Commit 6cddd87

Browse files
committed
Add transform actions section
1 parent f0a5ed3 commit 6cddd87

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

docs/guide.adoc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,9 @@ Level 2 selected.
884884
----
885885

886886

887-
=== Before Actions
887+
=== Parsing Actions
888+
889+
==== Before Actions
888890
Before actions operate on the entire argument list, after environment variable
889891
and response file expansion, but before any individual arguments are parsed.
890892
The before action should:
@@ -954,7 +956,23 @@ That isn't too complicated, but since this case is so common cli.helpNoArgs()
954956
is available to do the same thing.
955957

956958

957-
=== Parse Actions
959+
==== Transform Actions
960+
Action to take immediately before each source value is parsed. Any
961+
number of transform actions can be added.
962+
963+
The function should:
964+
* Inspect, and/or change the source value via cli.newValue().
965+
* Call cli.badUsage() with an error message if there's a problem.
966+
* Call cli.parseExit() if the program should stop without an error. This could
967+
be due to an early out like "--version" and "--help".
968+
969+
The argument is set, so you can use opt.from() and opt.pos() to get the
970+
option name that the value was matched with on the command line and its
971+
position in argv[]. For bool arguments the val string will always be
972+
either "0" or "1".
973+
974+
975+
==== Parse Actions
958976
Sometimes, you want an argument to completely change the execution flow. For
959977
instance, to provide more detailed errors about badly formatted arguments. Or
960978
to make "--version" print some crazy ASCII artwork and exit the program (for
@@ -964,8 +982,8 @@ Parsing actions are bound to options and get invoked when a value becomes
964982
available for it. Any std::function compatible object that accepts references
965983
to cli, opt, and string as parameters can be used. The function should:
966984

967-
* Parse the source string and use the result to set the option value (or
968-
push back the additional value for vector arguments).
985+
* Parse the source string and use the result to update the option value (or
986+
value vector).
969987
* Call cli.badUsage() with an error message if there's a problem.
970988
* Call cli.parseExit() if the program should stop. This could be due to an
971989
early out like "--version" and "--help".
@@ -1017,7 +1035,7 @@ Error: Invalid '-n' value: x
10171035
----
10181036

10191037

1020-
=== Check Actions
1038+
==== Check Actions
10211039
Check actions run for each value that is successfully parsed and are a good
10221040
place for additional work. For example, opt.range() and opt.clamp() are
10231041
implemented as check actions. Just like parse actions the callback is any
@@ -1074,7 +1092,7 @@ $ a.out --socks 3
10741092
----
10751093

10761094

1077-
=== After Actions
1095+
==== After Actions
10781096
After actions run after all arguments have been parsed. For example,
10791097
opt.prompt() and opt.require() are both implemented as after actions. Any
10801098
number of after actions can be added and will, for every (not just the ones

docs/reference.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ present, multiplies by the corresponding factor.
260260
| opt.toString<T>
261261
| Inherited from <<string-conversions, Convert>>.
262262

263-
| opt.transform
263+
| opt.<<guide.adoc#transform-actions, transform>>
264264
| Add action to transform value string before it is parsed, any number of
265265
transform actions can be added.
266266

libs/dimcli/cli.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ class DIMCLI_LIB_DECL Cli {
364364
// command's action function should:
365365
// - For parsing errors not caught by cli.parse(), such as complex
366366
// interactions between arguments; call cli.badUsage() and return. Also
367-
// consider an after action instead.
367+
// consider after actions instead.
368368
// - If no action was really attempted, as when only printing help text or
369369
// a version string; call cli.parseExit() and return.
370370
// - Do something useful.
@@ -1924,11 +1924,11 @@ class Cli::OptShim : public OptBase {
19241924
// Function signature of actions that are tied to options.
19251925
using ActionFn = void(Cli & cli, A & opt, const std::string & val);
19261926

1927-
// Action to take immediately before each value string is parsed. Any
1927+
// Action to take immediately before each source value is parsed. Any
19281928
// number of transform actions can be added.
19291929
//
19301930
// The function should:
1931-
// - Inspect, and/or change the raw value via cli.newValue().
1931+
// - Inspect, and/or change the val string via cli.newValue().
19321932
// - Call cli.badUsage() with an error message if there's a problem.
19331933
// - Call cli.parseExit() if the program should stop without an error.
19341934
// This could be due to an early out like "--version" and "--help".

0 commit comments

Comments
 (0)