OptionParse with no arguments show banner

8.7k views Asked by At

I'm using OptionParser with Ruby.

I other languages such as C, Python, etc., there are similar command-line parameters parsers and they often provide a way to show help message when no parameters are provided or parameters are wrong.

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: calc.rb [options]"

  opts.on("-l", "--length L", Integer, "Length") { |l| options[:length] = l }
  opts.on("-w", "--width W", Integer, "Width") { |w| options[:width] = w }

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!

Questions:

  1. Is there a way to set that by default show help message if no parameters were passed (ruby calc.rb)?
  2. What about if a required parameter is not given or is invalid? Suppose length is a REQUIRED parameter and user do not pass it or pass something wrong like -l FOO?
3

There are 3 answers

0
Малъ Скрылевъ On BEST ANSWER

Just add the -h key to the ARGV, when it is empty, so you may to do something like this:

require 'optparse'

ARGV << '-h' if ARGV.empty?

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: calc.rb [options]"

  opts.on("-l", "--length L", Integer, "Length") { |l| options[:length] = l }
  opts.on("-w", "--width W", Integer, "Width") { |w| options[:width] = w }

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!
0
Sam Watkins On

My script requires exactly 2 args, so I used this code to check ARGV.length after parsing:

if ARGV.length != 2 then
  puts optparse.help
  exit 1
end
0
Jasminen On
  1. You can either check ARGV before parsing (as the answer above):
    ARGV << '-h' if ARGV.empty?

    Or check your options hash after parsing:

    if @options.empty?
      puts optparse.help
      puts 'At least 1 argument should be supplied!'
    end
    
  2. This is what i do to ensure mandatory args with OptionParse (couldn't find any builtin feature for this):

    begin
      optparse.parse!
      mandatory = [:length, :width]                                         # Enforce the presence of
      missing = mandatory.select{ |param| @options[param].nil? }            # mandatory switches: :length, :width
      if not missing.empty?                                                 #
            puts "Missing options: #{missing.join(', ')}"                   #
            puts optparse.help                                              #
            exit 2                                                          #
      end                                                                   #
    rescue OptionParser::InvalidOption, OptionParser::MissingArgument => error     #
      puts error                                                                   # Friendly output when parsing fails
      puts optparse                                                                #
      exit 2                                                                       #
    end