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

Cannot see switches default value #86

Closed
Tereci opened this issue May 24, 2012 · 10 comments
Closed

Cannot see switches default value #86

Tereci opened this issue May 24, 2012 · 10 comments

Comments

@Tereci
Copy link

Tereci commented May 24, 2012

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.

@Tereci
Copy link
Author

Tereci commented May 24, 2012

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.

@davetron5000
Copy link
Owner

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:

do_execute unless options['dry-run']

Also, you should not use ARGV in a GLI app; GLI will modify ARGV when it parses, and the value of args to your c.action block will be whatever was left in ARGV after parsing the command-line options and command-name

@bdiz
Copy link

bdiz commented Jul 8, 2012

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.

@davetron5000
Copy link
Owner

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.

@bdiz
Copy link

bdiz commented Jul 9, 2012

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-----
From: David Copeland [mailto:reply@reply.github.com]
Sent: Monday, July 09, 2012 7:12 AM
To: Delsol, Ben
Subject: Re: [gli] Cannot see switches default value (#86)

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:
#86 (comment)

@bdiz
Copy link

bdiz commented Sep 16, 2012

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.

@pdf
Copy link

pdf commented Nov 15, 2012

Can we re-open this to get default_value working for switches?

@davetron5000
Copy link
Owner

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…

@davetron5000
Copy link
Owner

Should be in 2.5.0

@davetron5000
Copy link
Owner

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

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

4 participants