views:

46

answers:

1

There are only three ways to invoke install.sh

./install.sh
./install.sh --force
./install.sh -f

I can write it easily. However I am trying to make use of OptionParse. This is what I have so far.

 def self.parse
    option = {}
    optparse = OptionParser.new do |opts|
      opts.banner = "Usage: ./install.sh [--force]"
      opts.on('-f', '--force', '') do |dir|
        option[:force] = true
      end
    end

    begin
      optparse.parse!
    rescue OptionParser::InvalidOption => e
      puts e
    end
  end

If user types ./install.sh --foo then program fails and user sees following message.

invalid option: --foo

Ideally I would like banner to be presented whenever there is an error message. How do I do that?

Second question:

If the user invokes likes this ./install.sh foo (notice foo is being passed as a param) then OptionParser does not show any error. How do provide message to user that this install takes only one argument -f or --force and nothing else.

+1  A: 

It seems like you're already doing that. When you rescue the exception and print it you should get an error message like:

invalid option $INVALID_OPTION

You can print the usage with

puts optparse
pgmura