commander-rb / commander

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

Display --help on missing option #53

Closed mastermindg closed 7 years ago

mastermindg commented 7 years ago

I have a command with a required option. How can I display the help page to the user if this option is not set?:

    command :test do |c|
      c.syntax = 'Test command'
      c.description = 'Test command'
      c.option '--prefix STRING', String, 'Adds a prefix to test'
      c.action do |args, options|
        raise ArgumentError.new("Prefix is required!") unless options.prefix
        testMeth
      end
    end
lebogan commented 7 years ago

mastermindg,

Not sure what your use case is. If you really need help to display, you can try this. It displays an option-specific help screen:

        unless options.prefix
          command(:help).run(:test)
          raise ArgumentError.new('Prefix is required!')
        end

Please note, this is not a public (published) option. I dug this out of the commander source code. Commander out-of-the-box is pretty complete and rethinking my uses has helped me a lot. You might want to seriously review your use case as this solution may not be very portable.

I am not a developer but a retired old-school network specialist. Take this with several grains of salt and a pint of good brew.

Lewis

ggilder commented 7 years ago

It seems like @lebogan 's workaround would do the trick. Closing the issue