gli 2.6.2 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -211,6 +211,8 @@ module GLI
211
211
  commands[:help].execute({},{},command.nil? ? [] : [command.name.to_s])
212
212
  end
213
213
  end
214
+ elsif ENV['GLI_DEBUG'] == 'true'
215
+ stderr.puts "Custom error handler exited false, skipping normal error handling"
214
216
  end
215
217
 
216
218
  raise ex if ENV['GLI_DEBUG'] == 'true'
@@ -254,7 +256,8 @@ module GLI
254
256
  # Returns true if we should proceed with GLI's basic error handling.
255
257
  # This calls the error block if the user provided one
256
258
  def regular_error_handling?(ex) #:nodoc:
257
- if @error_block
259
+ if @error_block
260
+ return true if (ex.respond_to?(:exit_code) && ex.exit_code == 0)
258
261
  @error_block.call(ex)
259
262
  else
260
263
  true
@@ -157,20 +157,25 @@ module GLI
157
157
  :skips_post => @skips_post,
158
158
  :skips_around => @skips_around,
159
159
  }
160
+ @commands_declaration_order ||= []
160
161
  if names.first.kind_of? Hash
161
162
  command = GLI::Commands::CompoundCommand.new(self,
162
163
  names.first,
163
164
  command_options)
164
165
  command.parent = self
165
166
  commands[command.name] = command
167
+ @commands_declaration_order << command
166
168
  else
167
- command = Command.new(command_options.merge(:names => [names].flatten))
168
- command.parent = self
169
- commands[command.name] = command
169
+ new_command = Command.new(command_options.merge(:names => [names].flatten))
170
+ command = commands[new_command.name]
171
+ if command.nil?
172
+ command = new_command
173
+ command.parent = self
174
+ commands[command.name] = command
175
+ @commands_declaration_order << command
176
+ end
170
177
  yield command
171
178
  end
172
- @commands_declaration_order ||= []
173
- @commands_declaration_order << command
174
179
  clear_nexts
175
180
  end
176
181
  alias :c :command
@@ -1,5 +1,5 @@
1
1
  module GLI
2
2
  unless const_defined? :VERSION
3
- VERSION = '2.6.2'
3
+ VERSION = '2.7.0'
4
4
  end
5
5
  end
@@ -206,6 +206,48 @@ class TC_testCommand < Clean::Test::TestCase
206
206
  assert_contained(@fake_stdout,/SYNOPSIS/)
207
207
  end
208
208
 
209
+ def test_error_handler_prints_that_its_skipping_when_gli_debug_is_set
210
+ ENV["GLI_DEBUG"] = 'true'
211
+ @app.on_error do
212
+ false
213
+ end
214
+ @app.command :blah do |c|
215
+ c.action do |*|
216
+ raise 'wtf'
217
+ end
218
+ end
219
+
220
+ assert_raises(RuntimeError) {
221
+ @app.run(['blah'])
222
+ }
223
+ assert_contained(@fake_stderr,/Custom error handler exited false, skipping normal error handling/)
224
+ end
225
+
226
+ def test_error_handler_should_be_called_on_help_now
227
+ @app.command :blah do |c|
228
+ c.action do |*|
229
+ help_now!
230
+ end
231
+ end
232
+ @app.run(["blah"])
233
+ assert @error_called
234
+ end
235
+
236
+ def test_error_handler_shouldnt_be_called_on_help_from_command_line
237
+ @app.command :blah do |c|
238
+ c.action do |*|
239
+ end
240
+ end
241
+ [
242
+ ["--help", "blah"],
243
+ ["blah", "--help"],
244
+ ].each do |args|
245
+ args_copy = args.clone
246
+ @app.run(args)
247
+ assert !@error_called, "for args #{args_copy.inspect}"
248
+ end
249
+ end
250
+
209
251
  def test_command_skips_pre
210
252
  @app.skips_pre
211
253
  @app.skips_post
@@ -45,6 +45,67 @@ class TC_testSubCommand < Clean::Test::TestCase
45
45
  :args => ['bar'])
46
46
  end
47
47
 
48
+ test_that "we can reopen commands to add new subcommands" do
49
+ Given {
50
+ @app.command :remote do |p|
51
+ p.command :add do |c|
52
+ c.action do |global_options,command_options,args|
53
+ @ran_command = :add
54
+ end
55
+ end
56
+ end
57
+ @app.command :remote do |p|
58
+ p.command :new do |c|
59
+ c.action do |global_options,command_options,args|
60
+ @ran_command = :new
61
+ end
62
+ end
63
+ end
64
+ }
65
+ When run_app('remote','new')
66
+ Then { assert_equal(@ran_command, :new) }
67
+ When run_app('remote', 'add')
68
+ Then { assert_equal(@ran_command, :add) }
69
+ end
70
+
71
+ test_that "reopening commands doesn't re-add them to the output" do
72
+ Given {
73
+ @app.command :remote do |p|
74
+ p.command(:add) { }
75
+ end
76
+ @app.command :remote do |p|
77
+ p.command(:new) { }
78
+ end
79
+ }
80
+ command_names = @app.instance_variable_get("@commands_declaration_order").collect { |c| c.name }
81
+ assert_equal 1, command_names.grep(:remote).size
82
+ end
83
+
84
+
85
+ test_that "we can reopen commands doesn't cause conflicts" do
86
+ Given {
87
+ @app.command :remote do |p|
88
+ p.command :add do |c|
89
+ c.action do |global_options,command_options,args|
90
+ @ran_command = :remote_add
91
+ end
92
+ end
93
+ end
94
+ @app.command :local do |p|
95
+ p.command :add do |c|
96
+ c.action do |global_options,command_options,args|
97
+ @ran_command = :local_add
98
+ end
99
+ end
100
+ end
101
+ }
102
+ When run_app('remote','add')
103
+ Then { assert_equal(@ran_command, :remote_add) }
104
+ When run_app('local', 'add')
105
+ Then { assert_equal(@ran_command, :local_add) }
106
+ end
107
+
108
+
48
109
  test_that "we can nest subcommands very deep" do
49
110
  Given {
50
111
  @run_results = { :add => nil, :rename => nil, :base => nil }
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.6.2
4
+ version: 2.7.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: 2013-07-03 00:00:00.000000000 Z
12
+ date: 2013-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake