commander-rb / commander

The complete solution for Ruby command-line executables
MIT License
821 stars 74 forks source link

active_command to delegates #33

Closed ondra-m closed 6 years ago

ondra-m commented 8 years ago

I need parse values from args like:

command :test do |c|
   c.option('--files FILES', Array) { |values|
       Commander::Runner.instance.active_command
   }
end

It will be much nicer if I use just active_command.

ggilder commented 8 years ago

Can you provide an example of why you would need to access active_command? I'm not sure I see the use case.

ondra-m commented 8 years ago

Cmd

  test --files file1.txt --files file2.txt

Commander

  command :test do |c|
     c.option('--files FILES', Array)
  end

Result of option.files will be file2.txt but I need also file1.txt. So I add custom Proc with

  proxy_options = Commander::Runner.instance.active_command.proxy_options

  saved = proxy_options.find{|switch, _| switch == :files }
  if saved
    saved[1].concat(values)
  else
    proxy_options << [:files, values]
  end

First I modify command.rb but I do not think that's a desirable feature for more people.

What do you think?

ggilder commented 8 years ago

I see, so the actual problem here is that repeated options result in the last one "winning" rather than the repeated option being treated as an array. It seems to be that this should be fixed in the option parser rather than hacking around it with a custom proc. Since this could be a breaking change I'd suggest making it an optional switch on the commander program so you can opt-in to this parsing style.

ggilder commented 6 years ago

Closing since this has not been updated in a while. Please feel free to reopen if this is still an issue for you.