commander-rb / commander

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

--help with additional --options gives 'command not found' #97

Open robertgates55 opened 3 years ago

robertgates55 commented 3 years ago

I often find mysql wanting to put --help on the end of a command to see the help text for that command. With commander, if I do that though, I first have to remove any --option options passed or I get 'command not found'

Example:

#!/usr/bin/env ruby

require 'rubygems'
require 'commander'

class MyApplication
  include Commander::Methods
  # include whatever modules you need

  def run
    program :name, 'test'
    program :version, '0.0.1'
    program :description, 'test'

    command :bob do |c|
      c.syntax = 'test bob [options]'
      c.summary = ''
      c.description = ''
      c.example 'description', 'command example'
      c.option '--some-switch', 'Some switch that does something'
      c.action do |args, options|
        # Do something or c.when_called Test::Commands::Bob
      end
    end

    run!
  end
end

MyApplication.new.run if $0 == __FILE__

Results in:

➜  ruby yourfile.rb --some-switch --help
invalid command. Use --help for more information

Is there any way to make this possible?

ggilder commented 3 years ago

Hmm, the invocation in your example is missing a command, which I think is what commander is complaining about. If you run the following does it work?

ruby yourfile.rb bob --some-switch --help
duco-lw commented 3 years ago

Hmm, the invocation in your example is missing a command, which I think is what commander is complaining about. If you run the following does it work?

ruby yourfile.rb bob --some-switch --help

It outputs nothing, exit code 0.

Altering the test file a little:

#!/usr/bin/env ruby

require 'rubygems'
require 'commander'

class MyApplication
  include Commander::Methods
  # include whatever modules you need

  def run
    program :name, 'test'
    program :version, '0.0.1'
    program :description, 'test'

    global_option('--aaa AAA', Integer, 'Test AAA')

    command :bob do |c|
      c.syntax = 'test bob [options]'
      c.summary = ''
      c.description = ''
      c.example 'description', 'command example'
      c.option '--value VALUE', 'Some switch that does something'
      c.action do |args, options|
        # Do something or c.when_called Test::Commands::Bob
        puts "Got value: #{options.value}"
        puts "Got aaa: #{options.aaa}"
      end
    end

    run!
  end
end

MyApplication.new.run if $0 == __FILE__

A

Command: ruby yourfile.rb bob --help or ruby yourfile.rb --help bob Output:

<the help text omitted for brevity>

Good :+1:

B

Command: ruby yourfile.rb bob --aaa=3 --value=4 Output:

Got value: 4
Got aaa: 3

This is as expected. :+1:

C

Command: ruby yourfile.rb bob --aaa=3 --value=4 --help or ruby yourfile.rb bob --aaa=3 --help --value=4 Output:

Got value: 4
Got aaa: 3

Expectation is the help output you get with: ruby yourfile.rb bob --help

D

Command: ruby yourfile.rb bob --help --aaa=3 --value=4 or ruby yourfile.rb --help bob --aaa=3 --value=4

Got value: 4
Got aaa: 

Curious, would maybe expect aaa to be populated. But really, expectation is the help output you get with: ruby yourfile.rb bob --help

Summary

A and B are normal behaviour. For C and D it is desired that the presence of --help means that the help text is shown rather than the command being processed.