main icon indicating copy to clipboard operation
main copied to clipboard

arity=1 is not enforced for options

Open alexism opened this issue 12 years ago • 1 comments

An option with arity 1 does not fail if 2 values are passed. See the example below:

require 'main'

ARGV.replace %w(--foo=bar --foo=baz)

Main {
  option('foo', 'f'){
    required
    arity 1
    argument_required
  }

  def run
    p "Did you pass --foo ? #{params['foo'].given? == true}"
    p params['foo'].value
    p params['foo'].values
  end
}

Expected result: failure 2!=1 Output: failure

   "Did you pass --foo ? true"
   "bar"
   ["bar", "baz"]

However for an option with arity>1, the check is correctly done

require 'main'

ARGV.replace %w(--foo=bar --foo=baz)

Main {
  option('foo', 'f'){
    required
    arity 3   # arity is now 3!
    argument_required
  }

  def run
    p "Did you pass --foo ? #{params['foo'].given? == true}"
    p params['foo'].value
    p params['foo'].values
  end
}

Expected result: fail because 2 values are set instead of 3 Ouput: OK

 option(--foo)) 2/3

alexism avatar Sep 13 '12 17:09 alexism

Is that the expected behavior? I'm also surprised. If yes, what about introducing a range or the use of two parameters for #arity?

https://github.com/ahoward/main/blob/9133795315c0c8fbc05da3cbb83cbadbd02ddaf3/lib/main/parameter.rb#L204

jgburet avatar Dec 04 '16 17:12 jgburet