davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

Feature request: provide a way to give better help for required args #334

Open rsanheim opened 1 week ago

rsanheim commented 1 week ago

Given a command like this:

  desc "Deploy the app to the specified environment"
  arg :env, [:required]
  command :ship do |c|
    c.flag :ref, default_value: Dx::Main.git_current_rev, require: true
    c.action do |_global, options, args|
      current.execute(c.name)
    end
  end

it gives this error by default:

error: Not enough arguments for command

It would be nice to somehow provide a way for particular commands to override this sort of help message for required arg(s). I looked at subclassing MissingRequiredArgumentsException, but that gets raised too early in GLI's run method to actually raise our own exception.

I was able to do this via patching GLI, which works fine - but I'm wondering if there would be a nice way to specify this in the DSL...maybe something like?

  arg :env, [:required], error_message: "`ship` requires a single env to deploy to; for ex: `dx ship pre-prod`"

Here how I patched it for context, which honestly is not bad at all and I definitely run with:

module CustomMissingArgsMessage
  def initialize(message, command)
    if command.name == :ship
      super("error: 'ship' requires a single env to deploy to; for ex 'dx ship pre-prod'", command)
    else
      super
    end
  end
end

GLI::MissingRequiredArgumentsException.prepend(CustomMissingArgsMessage)
davetron5000 commented 6 days ago

Good call - you could use on_error, but I realized GLI knows more info to make a better error message. Check out #335 and see what you think?