Chain of subcommands #1789
-
I would like to provide an CLI with a succession of subcommands instead of a single one, and it looks like clap does not handle this case (it's more than adding a subcommand to another subcommand). The following is a bit long and verbose to clarify what I would like to do and why. In summary, I am designing a tool with a collection of data analysis features. It needs to specify which data to load and then which analysis to perform. Up to this point, I'm fine with what clap provides: I use global arguments to specify the data to load and one subcommand for each type of analysis. However, it gets a bit more complicated now that I want to be able to transform the data before performing the analysis. To support this, I would like to provide several layers of subcommands, organized as follows:
It would lead to relatively natural commands. For example in the simplest case: And when including some transform: Or more complex chains: It can be abstracted out by defining input/output types for subcommands:
In this setup, the available commands depend on the current type (none when the program is launched) and evolves along the chain of commands. Coming back to clap: I am currently abusing the common parameters to encode the transformations, but it gets ugly to implement and to use. I am considering to do a first analysis of parameters to split subcommands and then use separate clap "programs" to parse them separately, but I will end up having to implement a wrapper for the help command (which is OK if I can delegate to clap for individual subcommands) and do a lot of parsing myself (which I would rather avoid). Can clap perform partial parsing to make it smoother? I mean calling clap to parse the first layer (load and generate subcommands), retrieve the first subcommand and its arguments and being able to call clap again to parse the next subcommand and so on until all command line parameters are consumed. It would still mean having to deal with the help command myself but it would be a nice solution for me. If a subcommand can take a list of random words as arguments, it would have to avoid consuming the next subcommand, for example by using the "--" trick or something similar.
Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I don't think this is easy to implement and such a rare case that I am afraid the maintainers would be hard pressed to work on this. If someone can make a PR, we will be happy to help. |
Beta Was this translation helpful? Give feedback.
-
I'll conduct a proper analysis a bit later, but at the first glance it looks a lot like #1407 |
Beta Was this translation helpful? Give feedback.
I'll conduct a proper analysis a bit later, but at the first glance it looks a lot like #1407