commander-rb / commander

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

Recommended way to access args after -- #96

Closed drusellers closed 4 years ago

drusellers commented 4 years ago

I'm building an application that will ultimately call another script. I would like to have a way to delineate between the commands proper args and options versus whats after the --

doh command --option --name bob -- --pass 

How can I get everything after the -- using commander, or do I need to inspect the $* on my own.

ggilder commented 4 years ago

Anything after the -- is treated as arguments. If you have a command like the following:

command :foo do |c|
  c.action do |args, options|
    say "Arguments:"
    say args.inspect
    say "Options:"
    say options.inspect
  end
end

Then everything after the -- will be present in args:

$ ruby test.rb foo -- -x
Arguments:
["-x"]
Options:
<Commander::Command::Options >
drusellers commented 4 years ago

My apologies, let me try again.

Given the following command

command :foo do |c|
  c.option '--option'
  c.option '--name', String, 'the name'
  c.action do |args, options|
    # elided
  end
end

Let's say I invoke this like

$ ruby test.rb foo somearg --name bob -- -x
Arguments:
["somearg", "-x"]
Options:
<Commander::Command::Options >

How can I know that the -x came after the --?

ggilder commented 4 years ago

Ah, I see, thanks for the clarification. That is not supported in commander, because it isn't supported by the underlying OptionParser library in Ruby's standard lib. As far as I can tell it is not common for standard option parsing libraries to make this distinction. Please let me know if you've found a counterexample.

In practice, can't you simply split the first and rest of the arguments?

drusellers commented 4 years ago

@ggilder totally understand - just coming from rust w/ clap - https://github.com/clap-rs/clap/issues/971