davetron5000 / optparse-plus

Start your command line scripts off right in Ruby
http://davetron5000.github.com/optparse-plus
Apache License 2.0
521 stars 54 forks source link

When a user calls my app with no arguments, I'd like to show the usage banner #62

Closed variousauthors closed 11 years ago

variousauthors commented 11 years ago

Hi

After perusing the source for a while I've decided to call this an issue. When a user calls my app with no arguments, I'd like my app to behave as though it had been called with --help. I'm not sure how to accomplish this within the framework you've provided.

Thanks,

z.

davetron5000 commented 11 years ago

You should be able to use the arg method in your binfile, e.g.

main do |my,args|
  # ... whatever
end

arg :first_arg, "The first arg"
arg :second_arg, :optional, "the second arg"

go!

Methadone should bomb out if the first arg is omitted (and, handily, you can code your main block relying on it being non-nil)

Another option is to use the method help_now! to do this: http://davetron5000.github.com/methadone/rdoc/classes/Methadone/ExitNow.html#method-i-help_now-21

Basically, you can check for no arguments:

main do |my,args|
  help_now!("you must supply an argument") if my.nil?
end

This will raise an exception that Methadone interprets as "something went wrong and the user needs to see the command line help".