katzer/mruby-tiny-opt-parser

Similar flags are overriding each other

Closed this issue · 4 comments

Hi,

I noticed this bug while working with your library where if you have flags that start similarly then they'll override each other.

assert 'OptParser#opts with similar flags' do
  parser = OptParser.new
  parser.on(:"export-targets", :string, "linux,windows")
  parser.on(:"export-directory", :string, "./exports")
  parser.parse(['--export-directory', './releases'])

  assert_equal({
    "export-targets": "linux,windows",
    "export-directory": "./releases",
  }, parser.opts)
end

I've figured out why it's happening, it seems this line is what's causing it:

@args.each_index { |i| pos = i if !pos && opt[0] == @args[i][0] } unless pos

Basically because they start with the same letter they're getting the same "short flag". I'm not sure how you'd like to go about fixing this. My thought was to generate the short flags when they're defined with .on and keep a hash of them you can check. If you're happy with this approach I'm happy to do the work.

katzer commented

I am aware of that. If you like you can provide a fix for it

Excellent, I'll put together a pull request soon.

kvtb commented

It would be nice to see this fixed, although mruby has several similar libraries, some of them only work with mruby-2.1.2, others are too heavy (requiring mruby-onig-regex), so this is the only usable library right now, only this bug remains.