Comments (8)
Definitely a bug:
Line 98 in 4394168
That means: if a boolean value is false
it will return the default_value
, which in this case is true
, hence it is impossible to get the right (false
) value!
Something like the following should fix the problem:
@value.nil? ? default_value : @value
from slop.
Is this project abandonware?
I switched to Optimist... single-file lib drop-in that doesn't need to be a gem dependency.
from slop.
Thanks for the report.
Is this project abandonware?
No it is still maintained.
I switched to Optimist... single-file lib drop-in that doesn't need to be a gem dependency.
Great, I'm glad you found a solution.
from slop.
Great, I'm glad you found a solution.
I needed to release pagy and I fricked out, but I like your project! 👍
from slop.
@ddnexus What's the purpose you're forcibly changing the value in the Options? e.g. doing this: opts[:false] = true
— this is unexpected behaviour IMO. Maybe Slop should raise an error when you try to reassign a value, but it's meant to be readonly. If you change the value explicitly then we no longer have tracking of the original value. The following all works as expected:
% ruby demo.rb
{:false=>false, :true=>true}
% ruby demo.rb --false
{:false=>true, :true=>true}
% ruby demo.rb --false --no-true
{:false=>true, :true=>false}
from slop.
Well, an accessor method that doesn't return the actual instance variable value, but changes it on the fly because it "expects" it to be any arbitrary non-false value, is conceptually plain wrong IMO.
You could object that it's not an accessor, but a method that returns the content of an instance variable is hardly something else... even if it perform tricks on your back 😉.
Returning a default value on read instead of assigning it to the instance variable on creation is also not exactly following the principle of less surprise.... at least my own surprise 🫢.
But that's kinda personal.
Maybe Slop should raise an error when you try to reassign a value, but it's meant to be readonly
Why would you want to impose your own concept of "options" to your users? If I want to freeze the options I can just do it. If I want to use slop as a simple initializer/validator of some options, why would you want to stop me to edit the original options based on other conditional logic later in the script? Wouldn't that just reduce the flexibility of your tool for no advantage?
If you change the value explicitly then we no longer have tracking of the original value.
I definitely miss the point of "tracking" the original value.
My ideal options parser should just give me a simple hash of options after defaults and validations and leave the rest to me.
from slop.
My ideal options parser should just give me a simple hash of options after defaults and validations and leave the rest to me
Sure. This is what to_h
is for. opts
is a Slop::Result
and shouldn't be treated like a Hash, despite the existence of a #[]
method.
from slop.
So you should definitely raise an error if the user tries to change the Slop::Result
, and put the opts.to_hash
as a suggestion in the error message itself, in order to educate the user that indeed may think that opts IS a Hash or Hash-like thing.
Principle of less surprise! 😄
PS:
opts
is aSlop::Result
That sounds weird as well to me (kinda "truth is not truth" 😄).
One would think opts
is a opts
hash... or maybe a Slop::Opts
object which is likely a hash-like thing, since it even responds to []
and []=
.
And why on Earth an object "meant to be read-only" has a []=
AND even a defined attr_writer
is beyond comprehension to me. 🤷
Maybe you can make it clearer starting from the README. Something like calling the variable like the object it represents or some funny variation like slopts
or whatever but opts
might be helpful.
from slop.
Related Issues (20)
- Thor comparison HOT 1
- ArgumentError not raised when Array arg is missing HOT 1
- Maybe support a choice option by default HOT 2
- Setting an option doesn't set it "everywhere" HOT 2
- Support for command in v4 HOT 1
- `help: false` throws off separator position HOT 4
- Attempt to modify frozen string litterals HOT 2
- Practical example HOT 5
- CI: Switch away from TravisCI HOT 6
- Conditional 'required' option HOT 2
- Custom message with Slop::MissingRequiredOption
- Ruby v3.0.0 problem with Slop v4.8.2 HOT 2
- Some bugs. HOT 1
- Enable frozen strings in Slop using the `# frozen_string_literal: true` pragma HOT 1
- No way to pass in a blank array? HOT 7
- [question] is there a way to check that one or more of some set of args exist? HOT 2
- Default value for boolean flags not working when invoking method HOT 1
- Issue / breaking change with boolean flags with 4.10.0 HOT 2
- Confusing that there's a 4.0 branch... HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slop.