Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kwatch opened this issue Oct 11, 2024 · 0 comments
Closed

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

kwatch opened this issue Oct 11, 2024 · 0 comments

Comments

@kwatch
Copy link

kwatch commented Oct 11, 2024

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.

nobu added a commit that referenced this issue Mar 9, 2025
Command line arguments are strings, other objects do not match.
nobu added a commit that referenced this issue Mar 9, 2025
Command line arguments are strings, convert enum list elements to
strings to match.
@nobu nobu closed this as completed Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants