Skip to content
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

Subcommands in the context of main command #103

Closed
dmgr opened this issue Jan 23, 2020 · 2 comments
Closed

Subcommands in the context of main command #103

dmgr opened this issue Jan 23, 2020 · 2 comments

Comments

@dmgr
Copy link

dmgr commented Jan 23, 2020

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.

@dmgr
Copy link
Author

dmgr commented Jan 24, 2020

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

@mdub
Copy link
Owner

mdub commented Sep 17, 2022

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 calling run
  • 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

@mdub mdub closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants