Search code examples
rubyoptparse

Are long option names case insensitive using OptionParser?


In the following script, the short options work as expected, the long ones don't:

#!/usr/bin/env ruby
require 'optparse'

optparse = OptionParser.new do|opts|
   opts.on( '-h', '--help', 'Display standard help') do puts opts    end
   opts.on( '-H', '--Help', 'Display other help'   ) do puts 'Help!' end
end
optparse.parse!

Here are the results when running it:

$ ./test -h
Usage: t [options]
-h, --help                       Display standard help
-H, --Help                       Display other help
$ ./test -H
Help!
$ ./test --help
Help!
$ ./test --Help
Help!

Is there a way to have --help generate the same output as -h?


Solution

  • Yes, it seems that long options are case-insensitive. This is by convention, I imagine. Never seen a tool with case-sensitive long names.

    See the source: https://github.com/ruby/ruby/blob/b4974e71dcb32d430d7d686c5de247218991ec6c/lib/optparse.rb#L1408

    You can copy and modify source of OptionParser, but you probably should not do this. :)