Subcommands in the context of main command
dmgr opened this issue · comments
Would it be possible to create subcommands in the context of main command that have parsed options?
I think about something like that:
mycli --variant code subcommand
Internally I would like to load different Subcommand
depend on --variant
option. That variants of subcommands would have different options and parameters.
So just to sum up. My script will load different Subcommand
class depends on --variant
option thus main command need to parse options first then load apprioprate subcommand class.
Here is a high level pseudo code for that:
Clamp do
option '--variant', 'CODE'
MySubcommands.get(variant).build(self)
end
This would be trivial if I could build subcommands after the main (top-level) command have been instantiated and then I will have access to its options.
OK, currently I use workaround:
variant = nil
OptionParser.new do |opts|
opts.on("--variant=CODE") do |v|
variant = v
end
end.parse
Then I have a variant
and I can load specific subcommand for that variant in Clamp block.:
Clamp do
option '--variant', 'CODE'
MySubcommands.get(variant).build(self)
end
So, this issue is really old. Sorry I wasn't paying attention at the time. I'm not sure if this is still a need you have, after all this time.
Even if it is, it's not the kind of thing Clamp
is going to provide explicit support for.
It might be possible to make it work, somehow, since:
Clamp do ... end
is just syntax sugar for declaring a class, then creating an instance, and callingrun
- which sets properties, and calls
execute
- executing a command with sub-commands creates a new object, for the subcommand
and so: - you could probably override/modify the way the sub-command object is created, based on your
--variant
option
Having said that, as much easier approach might be to have different sub-commands per "variant", rather than having an option, e.g. instead of
mycli --variant v1 subcommand
you'd do
mycli v1 subcommand