How do I get only long options work in OptionParser in Ruby?

Viewed 2106

I have such a simple code in Ruby (test.rb):

#! /usr/bin/env ruby

require 'optparse'

OptionParser.new do |option|
  option.on("--sort", "Sort data") do 
    puts "--sort passed"
  end
end.parse!

then I run it: ./test.rb -s and got:

--sort passed

Have I missed something?

I want the only --sort (long) option works, not the short one.

How do I get it?

5 Answers

You can reopen OptionParser::OptionMap to disable completions with:

class OptionParser::OptionMap
  def complete(key, icase = false, pat = nil)
    # disable completions
    nil
  end
end

This will disable the predefined behavior of searching for stuff to complete.

Related