-
Notifications
You must be signed in to change notification settings - Fork 102
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
Cannot see switches default value #86
Comments
One more thing I found out - I also use argv and if I call the command with no arguments, the switch get initialized properly... But if I call the command with an argument (utils synchronize_datasets file.xml) I got what I described above. |
Switches are just true or false, and the default value is currently assumed to be false. In GLI 1.x there is no way to specify a switch as false on the command line, so if the switch were defaulted to true, the user could never turn it "off". This is somewhat rectified in GLI 2 (which is not yet available via RubyGems) For your case, you can get around this like so: c.desc "Don't actually execute anything"
c.switch [:n,:'dry-run'] Basically reversing the meaning of the switch. Now, you would do something like:
Also, you should not use |
I'm using GLI 2. What are you referring to wrt this issue being "rectified"? default_value still seems to work only for flags. Switches work great for turning things on, but if you want to default to on and have a switch to turn something off.. I can't seem to figure it out. |
I guess what I'm saying is that switches only ever have a default value of false. You cannot set their default value to true via the DSL. If you want a switch that defaults to on, you can handle it directly like so: desc 'execute for real (defaults true)'
switch 'execute', :negatable => true
pre do |global_options,options,args|
global_options[:execute] = true if global_options[:execute].nil?
end
command :foo do |c|
c.action do |global_options,options,args|
if global_options[:execute] # will be true if the user omitted it on the command line
end
end I agree that's a bit of a kludge, but it's pretty unusual (at least in my experience) to have a switch that, if omitted, defaults to true. Usually a switch is used to switch something on that is off by default, and the negatable form is to allow overriding a switch that was set "on" in a config file or environment variable. |
I have a switch which removes borders from the table it prints. By default borders are on. I can try using the pre hook. Would be nice to have a default_value for wierdos like me. Thanks. -----Original Message----- I guess what I'm saying is that switches only ever have a default value of false. You cannot set their default value to true via the DSL. If you want a switch that defaults to on, you can handle it directly like so: desc 'execute for real (defaults true)'
switch 'execute', :negatable => true
pre do |global_options,options,args|
global_options[:execute] = true if global_options[:execute].nil?
end
command :foo do |c|
c.action do |global_options,options,args|
if global_options[:execute] # will be true if the user omitted it on the command line
end
end I agree that's a bit of a kludge, but it's pretty unusual (at least in my experience) to have a switch that, if omitted, defaults to true. Usually a switch is used to switch something on that is off by default, and the negatable form is to allow overriding a switch that was set "on" in a config file or environment variable. Reply to this email directly or view it on GitHub: |
I've noticed that the pre hook trick works for global options, but for sub-command switches, the switch value is always false. I've had to look into ARGV myself to see if the sub-command switch is present. |
Can we re-open this to get default_value working for switches? |
I'm fine re-opening it, but I do feel like having a switch default to true would be a very confusing user experience. That said, it's probably not too much effort to implement… |
Should be in 2.5.0 |
Also, be mindful that you don't make a switch that defaults to true, but is not negatable - GLI will fail with an error in that case |
Hi, I have a problem - maybe it is just my misunderstanding of how to use gli - that inside of c.action I cannot see any value for commands switch even if I set a default one. I use version 1.6.0.
I generated the stub using "gli init project_name commands" and I added a switch like:
desc ''
arg_name 'Describe arguments to synchronize_datasets here'
command :synchronize_datasets do |c|
c.desc 'Model directory'
c.default_value '.'
c.flag [:m, :model_dir]
c.desc ''
c.default_value true
c.switch [:e, :execute]
c.action do |global_options,options,args|
pp options
end
end
and I get only {:m=>".", :model_dir=>"."} . Is it not possible to set a default value to a switch?
Thank you - didnt know where else to ask.
T.
The text was updated successfully, but these errors were encountered: