Skip to content
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

Default value for boolean flags not working when invoking method #272

Closed
sepastian opened this issue Sep 28, 2021 · 1 comment
Closed

Default value for boolean flags not working when invoking method #272

sepastian opened this issue Sep 28, 2021 · 1 comment

Comments

@sepastian
Copy link

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
@leejarvis
Copy link
Owner

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.

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