commander-rb / commander

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

Cannot pass '--trace' to alias_command #69

Closed O-kasso closed 5 years ago

O-kasso commented 6 years ago

When trying to add a "debug" alias to a previously defined command, I can't pass the --trace flag to it, even though I could manually append --trace when running the command from CLI manually.

e.g.:

command :run do |c|
  c.syntax = "foobar run"
  c.example 'foobar run --thing path/to/thing --other-thing 12345'
  c.option '--thing STRING', String
  c.option '--other-thing STRING', String

  c.action do |_args, options|
    # do stuff here
  end
end
alias_command :debug, :run, '--thing', 'foo', '--other-thing', '67890', '--trace'

when running debug:

$ foobar debug
invalid option: --trace

amusingly, if I apply --trace to debug:

$ foobar debug

Traceback (most recent call last):
        5: from /Users/me/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/commander-4.4.4/lib/commander/import.rb:5:in `block in <top (required)>'
        4: from /Users/me/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/commander-4.4.4/lib/commander/delegates.rb:15:in `run!'
        3: from /Users/me/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/commander-4.4.4/lib/commander/runner.rb:68:in `run!'
        2: from /Users/me/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/commander-4.4.4/lib/commander/runner.rb:444:in `run_active_command'
        1: from /Users/me/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/commander-4.4.4/lib/commander/command.rb:153:in `run'
/Users/me/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/commander-4.4.4/lib/commander/command.rb:164:in `parse_options_and_call_procs': invalid option: --trace (OptionParser::InvalidOption)

It'd be great if I could hardcode always providing traceback for a specific alias, but not show the traceback by default. Is there an easy way to do this that I'm missing?

ggilder commented 6 years ago

Sorry for the delay responding to this! I'm guessing that the problem is that --trace is a global option rather than a command-specific one. By the time your alias is processed, the global options have probably already been handled. I don't know a great way around that, but one approach might be to set up another command called debug that calls always_trace! and then runs the destination command.