Restricting the `--version` option to the root command

I'm making a CLI with SwiftArgumentParser and so far I'm really enjoying it, it's a pleasure to use. However I have one question relating to the automatically generated --version option.

It has been brought to my attention by a friend that it's a bit counter intuitive having the --version option allowed in places like swift-bundler templates --version (where you might expect that it would print the version of the templates repository) or swift-bundler create MyApp --version (where you might expect that it would allow you to provide an initial version to put in the app's configuration). In both cases, the command just prints the root command's version and exits.

Is there a way to disable this 'propagation' of the automatic version command and limit it to only the root command? Because realistically that's the only place someone would ever want to/expect to be able to use the automatically generated version option.

1 Like

I don't think this is possible to achieve with ArgumentParser in its current form, so I solved this issue by creating a fork that has the behaviour I want. I think a good long term solution would be to add some sort of configuration option to restrict an option to only be valid when the command is the last command in the stack (currently options of all parent commands are valid on subcommands). I will give this idea some more thought and I may end up making a PR for swift-argument-parser if I come up with a nice solution.

1 Like