gli 2.4.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -10,3 +10,4 @@ gli.wiki
10
10
  Gemfile.lock
11
11
  results.html
12
12
  .rbx
13
+ .DS_Store
@@ -115,10 +115,10 @@ Feature: The todo app has a nice user interface
115
115
 
116
116
  GLOBAL OPTIONS
117
117
  --flag=arg - (default: none)
118
- --help - Show this message
119
- --[no-]otherswitch -
120
118
  --[no-]switch -
119
+ --[no-]otherswitch -
121
120
  --version -
121
+ --help - Show this message
122
122
 
123
123
  COMMANDS
124
124
  help - Shows a list of commands or help for one command
data/lib/gli/app.rb CHANGED
@@ -224,10 +224,10 @@ module GLI
224
224
  raise exception
225
225
  end
226
226
 
227
- # Control how help commands are sorted. By default, the commands are sorted alphabetically.
227
+ # Control how commands and options are sorted in help output. By default, they are sorted alphabetically.
228
228
  #
229
- # sort_type:: How you want help commands sorted:
230
- # +:manually+:: help commands are ordered in the order declared.
229
+ # sort_type:: How you want help commands/options sorted:
230
+ # +:manually+:: help commands/options are ordered in the order declared.
231
231
  # +:alpha+:: sort alphabetically (default)
232
232
  def sort_help(sort_type)
233
233
  @help_sort_type = sort_type
@@ -16,7 +16,7 @@ module GLI
16
16
  command_wrapper = @wrapper_class.new(Terminal.instance.size[0],4 + @command.name.to_s.size + 3)
17
17
  wrapper = @wrapper_class.new(Terminal.instance.size[0],4)
18
18
  flags_and_switches = Hash[@command.topmost_ancestor.flags.merge(@command.topmost_ancestor.switches).select { |_,option| option.associated_command == @command }]
19
- options_description = OptionsFormatter.new(flags_and_switches,@wrapper_class).format
19
+ options_description = OptionsFormatter.new(flags_and_switches,@sorter,@wrapper_class).format
20
20
  commands_description = format_subcommands(@command)
21
21
 
22
22
  synopses = []
@@ -27,7 +27,7 @@ module GLI
27
27
  command_formatter.output(stringio)
28
28
  commands = stringio.string
29
29
 
30
- global_option_descriptions = OptionsFormatter.new(global_flags_and_switches,@wrapper_class).format
30
+ global_option_descriptions = OptionsFormatter.new(global_flags_and_switches,@sorter,@wrapper_class).format
31
31
 
32
32
  GLOBAL_HELP.result(binding)
33
33
  end
@@ -2,15 +2,15 @@ module GLI
2
2
  module Commands
3
3
  module HelpModules
4
4
  class OptionsFormatter
5
- def initialize(flags_and_switches,wrapper_class)
5
+ def initialize(flags_and_switches,sorter,wrapper_class)
6
6
  @flags_and_switches = flags_and_switches
7
+ @sorter = sorter
7
8
  @wrapper_class = wrapper_class
8
9
  end
9
10
 
10
11
  def format
11
- list_formatter = ListFormatter.new(@flags_and_switches.values.sort { |a,b|
12
- a.name.to_s <=> b.name.to_s
13
- }.map { |option|
12
+ flags_and_switches = @sorter.call(@flags_and_switches.values)
13
+ list_formatter = ListFormatter.new(flags_and_switches.map { |option|
14
14
  if option.respond_to? :argument_name
15
15
  [option_names_for_help_string(option,option.argument_name),description_with_default(option)]
16
16
  else
@@ -28,7 +28,7 @@ module GLI
28
28
  if option.kind_of? Flag
29
29
  String(option.description) + " (default: #{option.safe_default_value || 'none'})"
30
30
  else
31
- String(option.description)
31
+ String(option.description) + (option.default_value ? " (default: enabled)" : "")
32
32
  end
33
33
  end
34
34
 
data/lib/gli/dsl.rb CHANGED
@@ -36,10 +36,13 @@ module GLI
36
36
  @next_arg_options = options
37
37
  end
38
38
 
39
- # set the default value of the next flag
39
+ # set the default value of the next flag or switch
40
40
  #
41
- # +val+:: A String reprensenting the default value to be used for the following flag if the user doesn't specify one
42
- # and, when using a config file, the config also doesn't specify one
41
+ # +val+:: The default value to be used for the following flag if the user doesn't specify one
42
+ # and, when using a config file, the config also doesn't specify one. For a switch, this is
43
+ # the value to be used if the switch isn't specified on the command-line. Note that if you
44
+ # set a switch to have a default of true, using the switch on the command-line has no effect.
45
+ # To disable a switch where the default is true, use the <tt>--no-</tt> form.
43
46
  def default_value(val); @next_default_value = val; end
44
47
 
45
48
  # Create a flag, which is a switch that takes an argument
@@ -84,6 +87,7 @@ module GLI
84
87
  # and aliases for this switch. The last element can be a hash of options:
85
88
  # +:desc+:: the description, instead of using #desc
86
89
  # +:long_desc+:: the long_description, instead of using #long_desc
90
+ # +:default_value+:: if the switch is omitted, use this as the default value. By default, switches default to off, or +false+
87
91
  # +:negatable+:: if true, this switch will get a negatable form (e.g. <tt>--[no-]switch</tt>, false it will not. Default is true
88
92
  def switch(*names)
89
93
  options = extract_options(names)
@@ -26,6 +26,9 @@ module GLI
26
26
  @flags.each do |name,flag|
27
27
  global_options[name] = flag.default_value unless global_options[name]
28
28
  end
29
+ @switches.each do |name,switch|
30
+ global_options[name] = switch.default_value if global_options[name].nil?
31
+ end
29
32
 
30
33
  command_name ||= @default_command || :help
31
34
  command = find_command(command_name)
@@ -43,7 +46,7 @@ module GLI
43
46
  command_options[name] = flag.default_value unless command_options[name]
44
47
  end
45
48
  command.switches.each do |name,switch|
46
- command_options[name] = switch.default_value unless command_options[name]
49
+ command_options[name] = switch.default_value if command_options[name].nil?
47
50
  end
48
51
 
49
52
  [global_options,command,command_options,args]
data/lib/gli/switch.rb CHANGED
@@ -14,11 +14,14 @@ module GLI
14
14
  # :desc - the short description
15
15
  # :long_desc - the long description
16
16
  # :negatable - true or false if this switch is negatable; defaults to true
17
- # :default_value - ignored, switches default to false
17
+ # :default_value - default value if the switch is omitted
18
18
  def initialize(names,options = {})
19
19
  super(names,options)
20
- @default_value = false
20
+ @default_value = false if options[:default_value].nil?
21
21
  @negatable = options[:negatable].nil? ? true : options[:negatable]
22
+ if @default_value != false && @negatable == false
23
+ raise "A switch with default #{@default_value} that isn't negetable is useless"
24
+ end
22
25
  end
23
26
 
24
27
  def arguments_for_option_parser
data/lib/gli/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module GLI
2
2
  unless const_defined? :VERSION
3
- VERSION = '2.4.1' #:nodoc:
3
+ VERSION = '2.5.0' #:nodoc:
4
4
  end
5
5
  end
data/test/tc_gli.rb CHANGED
@@ -425,6 +425,43 @@ class TC_testGLI < Clean::Test::TestCase
425
425
  @app.run(['foo', '-i','5','-s','a'])
426
426
  end
427
427
 
428
+ def test_switch_with_default_of_true
429
+ @app.reset
430
+ @app.on_error do |ex|
431
+ raise ex
432
+ end
433
+ @switch_value = nil
434
+
435
+ @app.command [:foo] do |c|
436
+ c.default_value true
437
+ c.switch :switch
438
+ c.action do |g,o,a|
439
+ @switch_value = o[:switch]
440
+ end
441
+ end
442
+ @app.run(['foo'])
443
+
444
+ assert @switch_value == true,"Got: '#{@switch_value}', but expected true"
445
+
446
+ @app.run(['foo','--no-switch'])
447
+
448
+ assert @switch_value == false,"Got: '#{@switch_value}', but expected false"
449
+ end
450
+
451
+ def test_switch_with_default_true_and_not_negetable_causes_exception
452
+ @app.reset
453
+ @app.on_error do |ex|
454
+ raise ex
455
+ end
456
+ @switch_value = nil
457
+
458
+ assert_raises(RuntimeError) do
459
+ @app.command [:foo] do |c|
460
+ c.switch :switch, :default_value => true, :negatable => false
461
+ end
462
+ end
463
+ end
464
+
428
465
  def test_two_flags_using_equals_with_a_default
429
466
  @app.reset
430
467
  @app.on_error do |ex|
@@ -121,10 +121,11 @@ class TC_testSubCommand < Clean::Test::TestCase
121
121
  # - args => array of expected args
122
122
  def assert_command_ran_with(expected_command,options)
123
123
  lambda {
124
+ global_options = options[:global_options] || { :help => false }
124
125
  @run_results.each do |command,results|
125
126
  if command == expected_command
126
- assert_equal(indiffernt_hash(options[:global_options]),results[0])
127
- assert_equal(indiffernt_hash(options[:command_options]),results[1])
127
+ assert_equal(indifferent_hash(global_options),results[0])
128
+ assert_equal(indifferent_hash(options[:command_options]),results[1])
128
129
  assert_equal(options[:args],results[2])
129
130
  else
130
131
  assert_nil results
@@ -133,7 +134,7 @@ class TC_testSubCommand < Clean::Test::TestCase
133
134
  }
134
135
  end
135
136
 
136
- def indiffernt_hash(possibly_nil_hash)
137
+ def indifferent_hash(possibly_nil_hash)
137
138
  return {} if possibly_nil_hash.nil?
138
139
  keys = possibly_nil_hash.keys
139
140
  keys.map(&:to_s).each do |key|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gli
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 2.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-20 00:00:00.000000000 Z
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -369,7 +369,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
369
369
  version: '0'
370
370
  segments:
371
371
  - 0
372
- hash: -3175102747663964560
372
+ hash: 3564389066848623229
373
373
  required_rubygems_version: !ruby/object:Gem::Requirement
374
374
  none: false
375
375
  requirements:
@@ -378,7 +378,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
378
378
  version: '0'
379
379
  segments:
380
380
  - 0
381
- hash: -3175102747663964560
381
+ hash: 3564389066848623229
382
382
  requirements: []
383
383
  rubyforge_project: gli
384
384
  rubygems_version: 1.8.24