Project

General

Profile

Actions

Feature #11523

closed

optparse short options will match complete options

Added by mjrk (Micha J) about 9 years ago. Updated over 3 years ago.

Status:
Closed
Target version:
-
[ruby-core:<unknown>]

Description

In short, if I define an option like "-F", "--irs [OCTAL]", -i will match this option, although the short version is defined as -F.


In long, this can be quite troublesome:

See the provided example

http://ruby-doc.org/stdlib-2.2.0/libdoc/optparse/rdoc/OptionParser.html

and change or remove the "-i", "--inplace [EXTENSION]" option for something else than i:

Now, the -i will still match, but the other option "-F", "--irs [OCTAL]"!

In a more complete stack this resulted in a hard to find error. Also, to fix this (and raise the required error) you need to check the ARGV directly which renders optparse a bit less useful.


Files

optparse-require-exact.patch (3.06 KB) optparse-require-exact.patch jeremyevans0 (Jeremy Evans), 07/01/2019 10:23 PM
Actions #1

Updated by mjrk (Micha J) about 9 years ago

Modified code example optiontest.rb

require 'optparse'
require 'pp'

class OptparseExample
  def self.parse(args)
    options = {}

    opt_parser = OptionParser.new do |opts|
      opts.on("-F", "--irs [OCTAL]", OptionParser::OctalInteger,
              "Specify record separator (default \\0)") do |rs|
        options["trigger"] = "I got triggered"
      end
    end

    opt_parser.parse!(args)
    options
  end  # parse()

end  # class OptparseExample

options = OptparseExample.parse(ARGV)
pp options
pp ARGV
$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 15.04
Release:	15.04
Codename:	vivid
$ ruby -v
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
$ ruby optiontest.rb -i
{"trigger"=>"I got triggered"}
[]

The -F, --irs option is triggered with -i. Especially confusing as the short version is specified as -F

Actions #2

Updated by shishir127 (Shishir Joshi) about 9 years ago

Try executing this script. The only difference between the previous script and this one is that instead of returning the options variable, opt_parser.parse!(args) is being returned from the method. I did not get the error with this script.

require 'optparse'
require 'pp'

class OptparseExample
  def self.parse(args)
    options = {}

    opt_parser = OptionParser.new do |opts|
      opts.on("-F", "--irs [OCTAL]", OptionParser::OctalInteger,
              "Specify record separator (default \\0)") do |rs|
        options["trigger"] = "I got triggered"
      end
    end

    opt_parser.parse!(args)
  end  # parse()

end  # class OptparseExample

options = OptparseExample.parse(ARGV)
pp options
pp ARGV

Updated by vo.x (Vit Ondruch) almost 9 years ago

I believe that:

  1. This is what OptParser doing by default. If your code were opts.on("--irs [OCTAL]", OptionParser::OctalInteger,, i.e. without the '-F', you would get '--irs' as well as shorthand '-i' available. Your case is bit specific, that the short and long parameters are quite different.
  2. If you override the '-i' option somewhere and give it different meaning, the OptParser will handle it correctly.
  3. If you really want to avoid the default short version of '-i', I can't see anything easier then fire the OptionParser::InvalidOption in the '-i' handler.

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

I'm not sure I would consider this a bug, but I can definitely see it as an undesired feature in some cases. I think it would be worthwhile to add a way to turn off this feature, and require that options specified on the command line match exactly. Attached is a patch that adds a require_exact accessor that does this.

Actions #5

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

  • Tracker changed from Bug to Feature
  • ruby -v deleted (2.2)
  • Backport deleted (2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN)
Actions #6

Updated by jeremyevans (Jeremy Evans) over 3 years ago

  • Status changed from Assigned to Closed

Applied in changeset git|eca8ffaa0b446db0a1cacc82a2e73155f6fd3fce.


[ruby/optparse] Add OptionParser#require_exact accessor

This allows you to disable allowing abbreviations of long options
and using short options for long options.

Implements Ruby Feature #11523

https://github.com/ruby/optparse/commit/dfefb2d2e2

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0