You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This adds support for aliases for subcommands via a new parameter to
CommandConfigurations constructors. The aliases are passed as an array
of strings, where the default is just an empty array that signifies there
are no aliases. The aliases are supported regardless of if a different
commandName is chosen or not. This also updates how subcommands show up
in the help text. Any aliases are now displayed to the right of the original
command.
In addition to the functionality itself, this change:
1. Updates some of the EndToEnd parsing tests to make sure they function
while using aliases.
2. Sprinkles mentions where I saw fit in the documentation.
3. Updates the Math example to have aliases for `math stats average`
(`math stats avg`), and `math multiply` (`math mul`).
`math`'s help text now looks like the below:
```
~ math --help
OVERVIEW: A utility for performing maths.
USAGE: math <subcommand>
OPTIONS:
--version Show the version.
-h, --help Show help information.
SUBCOMMANDS:
add (default) Print the sum of the values.
multiply, mul Print the product of the values.
stats Calculate descriptive statistics.
See 'math help <subcommand>' for detailed help.
~ math stats --help
OVERVIEW: Calculate descriptive statistics.
USAGE: math stats <subcommand>
OPTIONS:
--version Show the version.
-h, --help Show help information.
SUBCOMMANDS:
average, avg Print the average of the values.
stdev Print the standard deviation of the values.
quantiles Print the quantiles of the values (TBD).
See 'math help stats <subcommand>' for detailed help.
```
and use of the aliases:
```
~ math mul 10 10
100
~ math stats avg 10 20
15.0
```
This change does NOT add any updates to the shell completion logic for
this feature.
Fixes#248
Copy file name to clipboardExpand all lines: Sources/ArgumentParser/Documentation.docc/Articles/CommandsAndSubcommands.md
+17-4Lines changed: 17 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ OPTIONS:
28
28
-h, --help Show help information.
29
29
30
30
SUBCOMMANDS:
31
-
average Print the average of the values.
31
+
average, avg Print the average of the values.
32
32
stdev Print the standard deviation of the values.
33
33
quantiles Print the quantiles of the values (TBD).
34
34
@@ -84,8 +84,9 @@ extension Math {
84
84
}
85
85
86
86
structMultiply: ParsableCommand {
87
-
staticlet configuration
88
-
=CommandConfiguration(abstract: "Print the product of the values.")
87
+
staticlet configuration =CommandConfiguration(
88
+
abstract: "Print the product of the values.",
89
+
aliases: ["mul"])
89
90
90
91
@OptionGroupvar options: Math.Options
91
92
@@ -97,6 +98,17 @@ extension Math {
97
98
}
98
99
```
99
100
101
+
One thing to note is the aliases parameter for `CommandConfiguration`. This is useful for subcommands
102
+
to define alternative names that can be used to invoke them. In this case we've defined a shorthand
103
+
for multiply named mul, so you could invoke the `Multiply` command for our program by either of the below:
104
+
105
+
```
106
+
% math multiply 10 15 7
107
+
1050
108
+
% math mul 10 15 7
109
+
1050
110
+
```
111
+
100
112
Next, we'll define `Statistics`, the third subcommand of `Math`. The `Statistics` command specifies a custom command name (`stats`) in its configuration, overriding the default derived from the type name (`statistics`). It also declares two additional subcommands, meaning that it acts as a forked branch in the command tree, and not a leaf.
101
113
102
114
```swift
@@ -116,7 +128,8 @@ Let's finish our subcommands with the `Average` and `StandardDeviation` types. E
0 commit comments