New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow subcommands to have alias names #248
Comments
@natecook1000 I would like to work on this issue. I am some questions in my mind:
|
Just my two cents, I would love the ability to be able to have a parent command be able to define a In regards to point 1, I would absolutely think that this should be opt-in somehow so that things "just work" out of the box - it certainly feels more Swifty to have the option requiring more complexity (manual command names) be the one that's opted into, to not overcomplicate getting a project started. And we probably also don't want to break whatever existing code there is that relies on the current API, either, if it can be avoided. I don't have any concrete ideas about what the implementation of this would look like, but it's certainly something I'd be excited to see! |
Another aspect to this which would be nice to have is "forwardable aliasing" — an alias that forwards to another subcommand with some default options already set. For example, one could define a command alias named "reinstall", which would forward to the "install" command, with the "--reinstall" flag set to |
@Zoha131 Thanks for your interest in working on this!
Yes — I think the aliases could just be an array of strings, with the default as an empty array.
Right — a You might want to think about how to present the aliases to users in the help, as well. @MPLew-is / @ajh17: I'm disinclined to add this kind of aliasing, as it somewhat inverts the command tree metaphor. I think we have enough opportunities with composition that this kind of forwarding can be handled by existing language features. For example, you could create a generic wrapper command that includes another command as an struct ForwardingCommand<Base: ParsableCommand>: ParsableCommand {
static var configuration: CommandConfiguration {
Base.configuration // might need a little extra something here
}
@OptionGroup
var command: Base
mutating func run() throws {
try command.run()
}
} |
Enjoying the library. Wanted to say thanks via this issue. @natecook1000 I'd submit a PR from this branch, but am still sorting how to update completion scripting.
|
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 apple#248
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 apple#248
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 apple#248
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
Since ArgumentParser is case-sensitive, it would be nice to allow commands to have alias names. For example, if I have a subcommand named "fetchResults", if the user types "fetchresults", I would like either of these arguments to run my subcommand.
This would also be useful to have both a verbose subcommand name and a short form like "fr"
The text was updated successfully, but these errors were encountered: