Skip to content

Commit

Permalink
switches can be defaulted to other than false
Browse files Browse the repository at this point in the history
Closes #86
  • Loading branch information
davetron5000 committed Nov 21, 2012
1 parent 1b1dd50 commit 942b764
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/gli/commands/help_modules/options_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def description_with_default(option)
if option.kind_of? Flag
String(option.description) + " (default: #{option.safe_default_value || 'none'})"
else
String(option.description)
String(option.description) + (option.default_value ? " (default: enabled)" : "")
end
end

Expand Down
10 changes: 7 additions & 3 deletions lib/gli/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ def arg_name(name,options=[])
@next_arg_options = options
end

# set the default value of the next flag
# set the default value of the next flag or switch
#
# +val+:: A String reprensenting the default value to be used for the following flag if the user doesn't specify one
# and, when using a config file, the config also doesn't specify one
# +val+:: The default value to be used for the following flag if the user doesn't specify one
# and, when using a config file, the config also doesn't specify one. For a switch, this is
# the value to be used if the switch isn't specified on the command-line. Note that if you
# set a switch to have a default of true, using the switch on the command-line has no effect.
# To disable a switch where the default is true, use the <tt>--no-</tt> form.
def default_value(val); @next_default_value = val; end

# Create a flag, which is a switch that takes an argument
Expand Down Expand Up @@ -84,6 +87,7 @@ def flag(*names)
# and aliases for this switch. The last element can be a hash of options:
# +:desc+:: the description, instead of using #desc
# +:long_desc+:: the long_description, instead of using #long_desc
# +:default_value+:: if the switch is omitted, use this as the default value. By default, switches default to off, or +false+
# +:negatable+:: if true, this switch will get a negatable form (e.g. <tt>--[no-]switch</tt>, false it will not. Default is true
def switch(*names)
options = extract_options(names)
Expand Down
5 changes: 4 additions & 1 deletion lib/gli/gli_option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def parse_options(args) # :nodoc:
@flags.each do |name,flag|
global_options[name] = flag.default_value unless global_options[name]
end
@switches.each do |name,switch|
global_options[name] = switch.default_value if global_options[name].nil?
end

command_name ||= @default_command || :help
command = find_command(command_name)
Expand All @@ -43,7 +46,7 @@ def parse_options(args) # :nodoc:
command_options[name] = flag.default_value unless command_options[name]
end
command.switches.each do |name,switch|
command_options[name] = switch.default_value unless command_options[name]
command_options[name] = switch.default_value if command_options[name].nil?
end

[global_options,command,command_options,args]
Expand Down
7 changes: 5 additions & 2 deletions lib/gli/switch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ class Switch < CommandLineOption #:nodoc:
# :desc - the short description
# :long_desc - the long description
# :negatable - true or false if this switch is negatable; defaults to true
# :default_value - ignored, switches default to false
# :default_value - default value if the switch is omitted
def initialize(names,options = {})
super(names,options)
@default_value = false
@default_value = false if options[:default_value].nil?
@negatable = options[:negatable].nil? ? true : options[:negatable]
if @default_value != false && @negatable == false
raise "A switch with default #{@default_value} that isn't negetable is useless"
end
end

def arguments_for_option_parser
Expand Down
37 changes: 37 additions & 0 deletions test/tc_gli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,43 @@ def test_two_flags_with_a_default
@app.run(['foo', '-i','5','-s','a'])
end

def test_switch_with_default_of_true
@app.reset
@app.on_error do |ex|
raise ex
end
@switch_value = nil

@app.command [:foo] do |c|
c.default_value true
c.switch :switch
c.action do |g,o,a|
@switch_value = o[:switch]
end
end
@app.run(['foo'])

assert @switch_value == true,"Got: '#{@switch_value}', but expected true"

@app.run(['foo','--no-switch'])

assert @switch_value == false,"Got: '#{@switch_value}', but expected false"
end

def test_switch_with_default_true_and_not_negetable_causes_exception
@app.reset
@app.on_error do |ex|
raise ex
end
@switch_value = nil

assert_raises(RuntimeError) do
@app.command [:foo] do |c|
c.switch :switch, :default_value => true, :negatable => false
end
end
end

def test_two_flags_using_equals_with_a_default
@app.reset
@app.on_error do |ex|
Expand Down
7 changes: 4 additions & 3 deletions test/tc_subcommands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ def a_very_deeply_nested_command_structure
# - args => array of expected args
def assert_command_ran_with(expected_command,options)
lambda {
global_options = options[:global_options] || { :help => false }
@run_results.each do |command,results|
if command == expected_command
assert_equal(indiffernt_hash(options[:global_options]),results[0])
assert_equal(indiffernt_hash(options[:command_options]),results[1])
assert_equal(indifferent_hash(global_options),results[0])
assert_equal(indifferent_hash(options[:command_options]),results[1])
assert_equal(options[:args],results[2])
else
assert_nil results
Expand All @@ -133,7 +134,7 @@ def assert_command_ran_with(expected_command,options)
}
end

def indiffernt_hash(possibly_nil_hash)
def indifferent_hash(possibly_nil_hash)
return {} if possibly_nil_hash.nil?
keys = possibly_nil_hash.keys
keys.map(&:to_s).each do |key|
Expand Down

0 comments on commit 942b764

Please sign in to comment.