slop icon indicating copy to clipboard operation
slop copied to clipboard

Default value for boolean flags not working when invoking method

Open sepastian opened this issue 3 years ago • 1 comments

Create a parser with a single boolean flag, -t/--test, with a default value of true.

When parsing an empty command, both opts['test'] and opts.test? should return true.

Only opts['test'] returns true; opts.test? returns false.

irb(main):001:0> require 'slop'
=> true
irb(main):002:1* opts = Slop.parse do |o|
irb(main):003:1*   o.bool '-t', '--test', 'boolean, default: true', default: true
irb(main):004:0> end
=> 
#<Slop::Result:0x00005654b0919d20
...
irb(main):008:0> opts['test']
=> true
irb(main):009:0> opts.test?
=> false
irb(main):013:1* opts = Slop.parse(%w('--test')) do |o|
irb(main):014:1*   o.bool '-t', '--test', 'boolean, default: true', default: true
irb(main):015:0> end
=> 
#<Slop::Result:0x00005654b0340098
...
irb(main):016:0> opts['test']
=> true
irb(main):017:0> opts.test?
=> false

sepastian avatar Sep 28 '21 13:09 sepastian

The methods with question marks are not designed to return the true or false value of the option, but rather whether or not the option was present/used. I can see why this might be confusing; I'll have a think about how to improve the README to clarify this.

leejarvis avatar Sep 28 '21 16:09 leejarvis