From 942b764a4e6fcc295aab733fd39d784f338d34d9 Mon Sep 17 00:00:00 2001 From: Dave Copeland Date: Wed, 21 Nov 2012 16:34:31 -0500 Subject: [PATCH] switches can be defaulted to other than false Closes #86 --- .../help_modules/options_formatter.rb | 2 +- lib/gli/dsl.rb | 10 +++-- lib/gli/gli_option_parser.rb | 5 ++- lib/gli/switch.rb | 7 +++- test/tc_gli.rb | 37 +++++++++++++++++++ test/tc_subcommands.rb | 7 ++-- 6 files changed, 58 insertions(+), 10 deletions(-) diff --git a/lib/gli/commands/help_modules/options_formatter.rb b/lib/gli/commands/help_modules/options_formatter.rb index 9167b525..8ccf2b46 100644 --- a/lib/gli/commands/help_modules/options_formatter.rb +++ b/lib/gli/commands/help_modules/options_formatter.rb @@ -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 diff --git a/lib/gli/dsl.rb b/lib/gli/dsl.rb index a5e838ba..38846b78 100644 --- a/lib/gli/dsl.rb +++ b/lib/gli/dsl.rb @@ -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 --no- form. def default_value(val); @next_default_value = val; end # Create a flag, which is a switch that takes an argument @@ -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. --[no-]switch, false it will not. Default is true def switch(*names) options = extract_options(names) diff --git a/lib/gli/gli_option_parser.rb b/lib/gli/gli_option_parser.rb index 077e12c1..89d4506a 100644 --- a/lib/gli/gli_option_parser.rb +++ b/lib/gli/gli_option_parser.rb @@ -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) @@ -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] diff --git a/lib/gli/switch.rb b/lib/gli/switch.rb index 9721d715..bb14aecb 100644 --- a/lib/gli/switch.rb +++ b/lib/gli/switch.rb @@ -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 diff --git a/test/tc_gli.rb b/test/tc_gli.rb index 1cd83216..d113336e 100644 --- a/test/tc_gli.rb +++ b/test/tc_gli.rb @@ -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| diff --git a/test/tc_subcommands.rb b/test/tc_subcommands.rb index 6be19374..eea91a6f 100644 --- a/test/tc_subcommands.rb +++ b/test/tc_subcommands.rb @@ -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 @@ -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|