Skip to content

[BUG] Integer class with enum values doesn't work correctly #79

@kwatch

Description

@kwatch

OptionParser object doesn't work correctly when both Integer and enum values (such as [1, 2, 3] or ["1", "2", "3"]) specified.

  • When Integer and [1, 2, 3] specified, OptionParser::InvalidArgument will be raised unexpectedly.
  • When Integer and ["1", "2", "3"] specified, no exceptions will be raised but the option value will be remained as a string (expected to be converted into an integer value).

Example script:

require 'optparse'

parser = OptionParser.new
parser.on("-a <N>", "int #1", Integer)
parser.on("-b <N>", "int #2", Integer, [1,2,3])     # with enum (integer)
parser.on("-c <N>", "int #3", Integer, ["1", "2"])  # with enum (string)

## Without enum, parser parses args successfully.
opts = {}
parser.parse(["-a", "2"], into: opts)   # ok
p opts     #=> {:a=>2}

## [BUG] With enum (integer), parser raises InvalidArgument error.
opts = {}
#parser.parse(["-b", "2"], into: opts)  #=> OptionParser::InvalidArgument

## [BUG] With enum (string), parser doesn't convert value into an integer.
opts = {}
parser.parse(["-c", "2"], into: opts)   # no exception
p opts     #=> {:c=>"2"}  (expected: {:c=>2})

Environment:

  • Version: optparse 0.4.0 & 0.5.0 (github repo)
  • Ruby: 3.3.5

If allowed, I can submit a pull request for this bug. Let me know if you need it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy