Issue #7537 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Assigned to Closed

I don't think this is a bug, I think this is expected behavior. When parsing `-p -1`, where `-p` accepts an optional argument, OptionParser correctly treats the `-1` as a new option, and raises an error as the parser does not accept that option.  You have to specify the optional argument a different way to tell OptionParser that the `-1` is the value for the optional argument.  One way is `-p-1`, another is `--pvalue=-1`.



----------------------------------------
Bug #7537: OptionParser treats negative digits as options
https://bugs.ruby-lang.org/issues/7537#change-80446

* Author: prijutme4ty (Ilya Vorontsov)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
* ruby -v: 1.9.3(p0, p327 both)
* Backport: 
----------------------------------------
Is it intentional that negative digits are treated as options?
If I use negative digit as an argument of an option, it is treated as a number

OptionParser.new {|opts|
  opts.on('-p','--pvalue VAL', Integer, 'P-value') {|v|  puts "P-value: #{v}" }
}.parse!

`ruby my_test.rb -p -1` works normally

But if I use it as optional argument of an option:

OptionParser.new {|opts|
  opts.on('-p','--pvalue [VAL]', Integer, 'P-value') {|v|  puts "P-value: #{v}" }
}.parse!

`ruby my_test.rb -p -1` fails with "Invalid option -1"

Also I can't use it as non-optional argument

OptionParser.new {|opts|
  opts.on('-n', 'no Pvalue argument, other arguments only') {}
}.parse!
puts ARGV

`ruby my_test.rb -1` also fails with "Invalid option -1"

---Files--------------------------------
259.patch (10.2 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>