commander-openflighthpc 1.0.0.pre.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/.rubocop.yml +44 -0
- data/.rubocop_todo.yml +77 -0
- data/.travis.yml +14 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/LICENSE +51 -0
- data/Manifest +38 -0
- data/README.md +492 -0
- data/Rakefile +13 -0
- data/bin/commander +104 -0
- data/commander-openflighthpc.gemspec +32 -0
- data/lib/commander.rb +36 -0
- data/lib/commander/blank.rb +7 -0
- data/lib/commander/command.rb +263 -0
- data/lib/commander/configure.rb +14 -0
- data/lib/commander/core_ext.rb +2 -0
- data/lib/commander/core_ext/array.rb +24 -0
- data/lib/commander/core_ext/object.rb +8 -0
- data/lib/commander/delegates.rb +27 -0
- data/lib/commander/help_formatters.rb +52 -0
- data/lib/commander/help_formatters/base.rb +24 -0
- data/lib/commander/help_formatters/terminal.rb +24 -0
- data/lib/commander/help_formatters/terminal/command_help.erb +35 -0
- data/lib/commander/help_formatters/terminal/help.erb +36 -0
- data/lib/commander/help_formatters/terminal/subcommand_help.erb +23 -0
- data/lib/commander/help_formatters/terminal_compact.rb +11 -0
- data/lib/commander/help_formatters/terminal_compact/command_help.erb +26 -0
- data/lib/commander/help_formatters/terminal_compact/help.erb +29 -0
- data/lib/commander/help_formatters/terminal_compact/subcommand_help.erb +15 -0
- data/lib/commander/import.rb +5 -0
- data/lib/commander/methods.rb +11 -0
- data/lib/commander/patches/decimal-integer.rb +17 -0
- data/lib/commander/patches/help_formatter_binding.rb +15 -0
- data/lib/commander/patches/implicit-short-tags.rb +75 -0
- data/lib/commander/patches/option_defaults.rb +23 -0
- data/lib/commander/patches/validate_inputs.rb +76 -0
- data/lib/commander/platform.rb +7 -0
- data/lib/commander/runner.rb +493 -0
- data/lib/commander/user_interaction.rb +551 -0
- data/lib/commander/version.rb +3 -0
- data/spec/command_spec.rb +157 -0
- data/spec/configure_spec.rb +37 -0
- data/spec/core_ext/array_spec.rb +18 -0
- data/spec/core_ext/object_spec.rb +19 -0
- data/spec/help_formatters/terminal_compact_spec.rb +69 -0
- data/spec/help_formatters/terminal_spec.rb +67 -0
- data/spec/methods_spec.rb +61 -0
- data/spec/patches/validate_inputs_spec.rb +84 -0
- data/spec/runner_spec.rb +672 -0
- data/spec/spec_helper.rb +79 -0
- data/spec/ui_spec.rb +30 -0
- metadata +183 -0
data/spec/runner_spec.rb
ADDED
@@ -0,0 +1,672 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class InheritedRuntimeError < RuntimeError; end
|
4
|
+
|
5
|
+
describe Commander do
|
6
|
+
include Commander::Methods
|
7
|
+
|
8
|
+
before :each do
|
9
|
+
$stderr = StringIO.new
|
10
|
+
mock_terminal
|
11
|
+
create_test_command
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#program' do
|
15
|
+
it 'should set / get program information' do
|
16
|
+
program :name, 'test'
|
17
|
+
expect(program(:name)).to eq('test')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should allow arbitrary blocks of global help documentation' do
|
21
|
+
program :help, 'Copyright', 'TJ Holowaychuk'
|
22
|
+
expect(program(:help)['Copyright']).to eq('TJ Holowaychuk')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should raise an error when required info has not been set' do
|
26
|
+
new_command_runner '--help'
|
27
|
+
program :version, ''
|
28
|
+
expect { run! }.to raise_error(Commander::Runner::CommandError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should allow aliases of help formatters' do
|
32
|
+
program :help_formatter, :compact
|
33
|
+
expect(program(:help_formatter)).to eq(Commander::HelpFormatter::TerminalCompact)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#command' do
|
38
|
+
it 'should return a command instance when only the name is passed' do
|
39
|
+
expect(command(:test)).to be_instance_of(Commander::Command)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should return nil when the command does not exist' do
|
43
|
+
expect(command(:im_not_real)).to be_nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#separate_switches_from_description' do
|
48
|
+
it 'should seperate switches and description returning both' do
|
49
|
+
switches, description = *Commander::Runner.separate_switches_from_description('-h', '--help', 'display help')
|
50
|
+
expect(switches).to eq(['-h', '--help'])
|
51
|
+
expect(description).to eq('display help')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#switch_to_sym' do
|
56
|
+
it 'should return a symbol based on the switch name' do
|
57
|
+
expect(Commander::Runner.switch_to_sym('--trace')).to eq(:trace)
|
58
|
+
expect(Commander::Runner.switch_to_sym('--foo-bar')).to eq(:foo_bar)
|
59
|
+
expect(Commander::Runner.switch_to_sym('--[no-]feature"')).to eq(:feature)
|
60
|
+
expect(Commander::Runner.switch_to_sym('--[no-]feature ARG')).to eq(:feature)
|
61
|
+
expect(Commander::Runner.switch_to_sym('--file [ARG]')).to eq(:file)
|
62
|
+
expect(Commander::Runner.switch_to_sym('--colors colors')).to eq(:colors)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#alias_command' do
|
67
|
+
it 'should alias a command' do
|
68
|
+
alias_command :foo, :test
|
69
|
+
expect(command(:foo)).to eq(command(:test))
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should pass arguments passed to the alias when called' do
|
73
|
+
gem_name = ''
|
74
|
+
new_command_runner 'install', 'gem', 'commander' do
|
75
|
+
command :install do |c|
|
76
|
+
c.option '--gem-name NAME', 'Install a gem'
|
77
|
+
c.when_called { |_, options| gem_name = options.gem_name }
|
78
|
+
end
|
79
|
+
alias_command :'install gem', :install, '--gem-name'
|
80
|
+
end.run!
|
81
|
+
expect(gem_name).to eq('commander')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#global_option' do
|
86
|
+
it 'should be invoked when used in the args list' do
|
87
|
+
file = ''
|
88
|
+
new_command_runner 'test', '--config', 'foo' do
|
89
|
+
global_option('--config FILE') { |f| file = f }
|
90
|
+
end.run!
|
91
|
+
expect(file).to eq('foo')
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should be inherited by commands' do
|
95
|
+
quiet = nil
|
96
|
+
new_command_runner 'foo', '--quiet' do
|
97
|
+
global_option('--quiet', 'Suppress output')
|
98
|
+
command :foo do |c|
|
99
|
+
c.when_called { |_, options| quiet = options.quiet }
|
100
|
+
end
|
101
|
+
end.run!
|
102
|
+
expect(quiet).to be true
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should be inherited by commands even when a block is present' do
|
106
|
+
quiet = nil
|
107
|
+
new_command_runner 'foo', '--quiet' do
|
108
|
+
global_option('--quiet', 'Suppress output') {}
|
109
|
+
command :foo do |c|
|
110
|
+
c.when_called { |_, options| quiet = options.quiet }
|
111
|
+
end
|
112
|
+
end.run!
|
113
|
+
expect(quiet).to be true
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should be inherited by commands when the positive form of a [no-] option' do
|
117
|
+
quiet = nil
|
118
|
+
new_command_runner 'foo', '--quiet' do
|
119
|
+
global_option('--[no-]quiet', 'Suppress output') {}
|
120
|
+
command :foo do |c|
|
121
|
+
c.when_called { |_, options| quiet = options.quiet }
|
122
|
+
end
|
123
|
+
end.run!
|
124
|
+
expect(quiet).to be true
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should be inherited by commands when the negative form of a [no-] option' do
|
128
|
+
quiet = nil
|
129
|
+
new_command_runner 'foo', '--no-quiet' do
|
130
|
+
global_option('--[no-]quiet', 'Suppress output') {}
|
131
|
+
command :foo do |c|
|
132
|
+
c.when_called { |_, options| quiet = options.quiet }
|
133
|
+
end
|
134
|
+
end.run!
|
135
|
+
expect(quiet).to be false
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe '#parse_global_options' do
|
140
|
+
it 'should parse global options before command' do
|
141
|
+
global_option = nil
|
142
|
+
new_command_runner('--testing-global', 'foo') do
|
143
|
+
global_option('--testing-global') { global_option = 'MAGIC' }
|
144
|
+
|
145
|
+
command :foo do |c|
|
146
|
+
c.when_called {}
|
147
|
+
end
|
148
|
+
end.run!
|
149
|
+
expect(global_option).to eq('MAGIC')
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should parse global options after command' do
|
153
|
+
global_option = nil
|
154
|
+
new_command_runner('foo', '--testing-global') do
|
155
|
+
global_option('--testing-global') { global_option = 'MAGIC' }
|
156
|
+
|
157
|
+
command :foo do |c|
|
158
|
+
c.when_called {}
|
159
|
+
end
|
160
|
+
end.run!
|
161
|
+
expect(global_option).to eq('MAGIC')
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'should parse global options placed before command options' do
|
165
|
+
global_option = nil
|
166
|
+
new_command_runner('foo', '--testing-global', '--testing-command') do
|
167
|
+
global_option('--testing-global') { global_option = 'MAGIC' }
|
168
|
+
|
169
|
+
command :foo do |c|
|
170
|
+
c.option('--testing-command') {}
|
171
|
+
c.when_called {}
|
172
|
+
end
|
173
|
+
end.run!
|
174
|
+
|
175
|
+
expect(global_option).to eq('MAGIC')
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'should parse global options placed after command options' do
|
179
|
+
global_option = nil
|
180
|
+
new_command_runner('foo', '--testing-command', '--testing-global') do
|
181
|
+
global_option('--testing-global') { global_option = 'MAGIC' }
|
182
|
+
|
183
|
+
command :foo do |c|
|
184
|
+
c.option('--testing-command') {}
|
185
|
+
c.when_called {}
|
186
|
+
end
|
187
|
+
end.run!
|
188
|
+
|
189
|
+
expect(global_option).to eq('MAGIC')
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'should parse global options surrounded by command options' do
|
193
|
+
global_option = nil
|
194
|
+
new_command_runner('foo', '--testing-command', '--testing-global', '--other-command') do
|
195
|
+
global_option('--testing-global') { global_option = 'MAGIC' }
|
196
|
+
|
197
|
+
command :foo do |c|
|
198
|
+
c.option('--testing-command') {}
|
199
|
+
c.option('--other-command') {}
|
200
|
+
c.when_called {}
|
201
|
+
end
|
202
|
+
end.run!
|
203
|
+
|
204
|
+
expect(global_option).to eq('MAGIC')
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'should not parse command options' do
|
208
|
+
global_option = nil
|
209
|
+
command_option = nil
|
210
|
+
new_command_runner('foo', '--testing-command', '--testing-global') do
|
211
|
+
global_option('--testing-global') { global_option = 'MAGIC' }
|
212
|
+
|
213
|
+
command :foo do |c|
|
214
|
+
c.option('--testing-command') { command_option = 'NO!' }
|
215
|
+
c.when_called {}
|
216
|
+
end
|
217
|
+
end.parse_global_options
|
218
|
+
|
219
|
+
expect(command_option).to be_nil
|
220
|
+
expect(global_option).to eq('MAGIC')
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'should not affect command arguments with values' do
|
224
|
+
global_option = nil
|
225
|
+
command_option = nil
|
226
|
+
new_command_runner('foo', '--testing-command', 'bar', '--testing-global') do
|
227
|
+
global_option('--testing-global') { global_option = 'MAGIC' }
|
228
|
+
|
229
|
+
command :foo do |c|
|
230
|
+
c.option('--testing-command VALUE') { |v| command_option = v }
|
231
|
+
c.when_called {}
|
232
|
+
end
|
233
|
+
end.run!
|
234
|
+
|
235
|
+
expect(command_option).to eq('bar')
|
236
|
+
expect(global_option).to eq('MAGIC')
|
237
|
+
end
|
238
|
+
|
239
|
+
it 'should not affect global arguments with values' do
|
240
|
+
global_option = nil
|
241
|
+
new_command_runner('foo', '--testing-command', '--testing-global', 'bar') do
|
242
|
+
global_option('--testing-global VALUE') { |v| global_option = v }
|
243
|
+
|
244
|
+
command :foo do |c|
|
245
|
+
c.option('--testing-command') {}
|
246
|
+
c.when_called {}
|
247
|
+
end
|
248
|
+
end.run!
|
249
|
+
|
250
|
+
expect(global_option).to eq('bar')
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'should allow global arguments with values before command arguments (github issue #8)' do
|
254
|
+
global_option = nil
|
255
|
+
command_option = nil
|
256
|
+
new_command_runner('foo', '--config', 'path', 'bar') do
|
257
|
+
global_option('--config VALUE') { |v| global_option = v }
|
258
|
+
|
259
|
+
command :foo do |c|
|
260
|
+
c.option('bar') { command_option = 'bar' }
|
261
|
+
c.when_called {}
|
262
|
+
end
|
263
|
+
end.run!
|
264
|
+
|
265
|
+
expect(global_option).to eq('path')
|
266
|
+
expect(command_option).to eq('bar')
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
describe '#remove_global_options' do
|
271
|
+
it 'should remove only specified switches' do
|
272
|
+
options, args = [], []
|
273
|
+
options << { switches: ['-t', '--trace'] }
|
274
|
+
options << { switches: ['--help'] }
|
275
|
+
options << { switches: ['--paths PATHS'] }
|
276
|
+
args << '-t'
|
277
|
+
args << '--help'
|
278
|
+
args << '--command'
|
279
|
+
args << '--command-with-arg' << 'rawr'
|
280
|
+
args << '--paths' << '"lib/**/*.js","spec/**/*.js"'
|
281
|
+
command_runner.remove_global_options options, args
|
282
|
+
expect(args).to eq(['--command', '--command-with-arg', 'rawr'])
|
283
|
+
end
|
284
|
+
|
285
|
+
it 'should not swallow an argument unless it expects an argument' do
|
286
|
+
options, args = [], []
|
287
|
+
options << { switches: ['-n', '--no-arg'] }
|
288
|
+
options << { switches: ['-y', '--yes ARG'] }
|
289
|
+
options << { switches: ['-a', '--alternative=ARG'] }
|
290
|
+
args << '-n' << 'alpha'
|
291
|
+
args << '--yes' << 'deleted'
|
292
|
+
args << '-a' << 'deleted'
|
293
|
+
args << 'beta'
|
294
|
+
command_runner.remove_global_options options, args
|
295
|
+
expect(args).to eq(%w(alpha beta))
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'should remove a switch that is the positive form of the [no-] option' do
|
299
|
+
options, args = [], []
|
300
|
+
options << { switches: ['-g', '--[no-]good'] }
|
301
|
+
options << { switches: ['-y', '--yes ARG'] }
|
302
|
+
options << { switches: ['-a', '--alternative=ARG'] }
|
303
|
+
args << '--good' << 'alpha'
|
304
|
+
args << '--yes' << 'deleted'
|
305
|
+
args << '-a' << 'deleted'
|
306
|
+
args << 'beta'
|
307
|
+
command_runner.remove_global_options options, args
|
308
|
+
expect(args).to eq(%w(alpha beta))
|
309
|
+
end
|
310
|
+
|
311
|
+
it 'should remove a switch that is the negative form of the [no-] option' do
|
312
|
+
options, args = [], []
|
313
|
+
options << { switches: ['-g', '--[no-]good'] }
|
314
|
+
options << { switches: ['-y', '--yes ARG'] }
|
315
|
+
options << { switches: ['-a', '--alternative=ARG'] }
|
316
|
+
args << '--no-good' << 'alpha'
|
317
|
+
args << '--yes' << 'deleted'
|
318
|
+
args << '-a' << 'deleted'
|
319
|
+
args << 'beta'
|
320
|
+
command_runner.remove_global_options options, args
|
321
|
+
expect(args).to eq(%w(alpha beta))
|
322
|
+
end
|
323
|
+
|
324
|
+
it 'should not remove options that start with a global option name' do
|
325
|
+
options, args = [], []
|
326
|
+
options << { switches: ['-v', '--version'] }
|
327
|
+
args << '--versionCode' << 'something'
|
328
|
+
command_runner.remove_global_options options, args
|
329
|
+
expect(args).to eq(%w(--versionCode something))
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
describe '--trace' do
|
334
|
+
it 'should display pretty errors by default' do
|
335
|
+
expect do
|
336
|
+
new_command_runner 'foo' do
|
337
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
338
|
+
end.run!
|
339
|
+
end.to raise_error(SystemExit, /error: cookies!. Use --trace/)
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'should display callstack when using this switch' do
|
343
|
+
expect do
|
344
|
+
new_command_runner 'foo', '--trace' do
|
345
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
346
|
+
end.run!
|
347
|
+
end.to raise_error(RuntimeError)
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
describe '#always_trace!' do
|
352
|
+
it 'should enable tracing globally, regardless of whether --trace was passed or not' do
|
353
|
+
expect do
|
354
|
+
new_command_runner 'foo' do
|
355
|
+
always_trace!
|
356
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
357
|
+
end.run!
|
358
|
+
end.to raise_error(RuntimeError)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
describe '#never_trace!' do
|
363
|
+
it 'should disable tracing globally, regardless of whether --trace was passed or not' do
|
364
|
+
expect do
|
365
|
+
new_command_runner 'help', '--trace' do
|
366
|
+
never_trace!
|
367
|
+
end.run!
|
368
|
+
end.to raise_error(SystemExit, /invalid option: --trace/)
|
369
|
+
end
|
370
|
+
|
371
|
+
it 'should not prompt to use --trace switch on errors' do
|
372
|
+
msg = nil
|
373
|
+
begin
|
374
|
+
new_command_runner 'foo' do
|
375
|
+
never_trace!
|
376
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
377
|
+
end.run!
|
378
|
+
rescue SystemExit => e
|
379
|
+
msg = e.message
|
380
|
+
end
|
381
|
+
expect(msg).to match(/error: cookies!/)
|
382
|
+
expect(msg).not_to match(/--trace/)
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
describe '#silent_trace!' do
|
387
|
+
it 'should allow the --trace option' do
|
388
|
+
expect do
|
389
|
+
new_command_runner 'help', '--trace' do
|
390
|
+
silent_trace!
|
391
|
+
end.run!
|
392
|
+
end.not_to raise_error
|
393
|
+
end
|
394
|
+
|
395
|
+
it 'should not prompt to use --trace switch on errors' do
|
396
|
+
msg = nil
|
397
|
+
begin
|
398
|
+
new_command_runner 'foo' do
|
399
|
+
silent_trace!
|
400
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
401
|
+
end.run!
|
402
|
+
rescue SystemExit => e
|
403
|
+
msg = e.message
|
404
|
+
end
|
405
|
+
expect(msg).to match(/error: cookies!/)
|
406
|
+
expect(msg).not_to match(/--trace/)
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
context 'conflict between #always_trace! and #never_trace!' do
|
411
|
+
it 'respects the last used command' do
|
412
|
+
expect do
|
413
|
+
new_command_runner 'foo' do
|
414
|
+
never_trace!
|
415
|
+
always_trace!
|
416
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
417
|
+
end.run!
|
418
|
+
end.to raise_error(RuntimeError)
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
describe '--version' do
|
423
|
+
it 'should output program version' do
|
424
|
+
expect(run('--version')).to eq("test 1.2.3\n")
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
describe '--help' do
|
429
|
+
it 'should not output an invalid command message' do
|
430
|
+
expect(run('--help')).not_to eq("invalid command. Use --help for more information\n")
|
431
|
+
end
|
432
|
+
|
433
|
+
it 'can be used before or after the command and options' do
|
434
|
+
expect(run('test', '--help')).to eq("Implement help for test here\n")
|
435
|
+
end
|
436
|
+
|
437
|
+
describe 'help_paging program information' do
|
438
|
+
it 'enables paging when enabled' do
|
439
|
+
run('--help') { program :help_paging, true }
|
440
|
+
expect(Commander::UI).to have_received(:enable_paging)
|
441
|
+
end
|
442
|
+
|
443
|
+
it 'is enabled by default' do
|
444
|
+
run('--help')
|
445
|
+
expect(Commander::UI).to have_received(:enable_paging)
|
446
|
+
end
|
447
|
+
|
448
|
+
it 'does not enable paging when disabled' do
|
449
|
+
run('--help') { program :help_paging, false }
|
450
|
+
expect(Commander::UI).not_to have_received(:enable_paging)
|
451
|
+
end
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
describe 'with invalid options' do
|
456
|
+
it 'should output an invalid option message' do
|
457
|
+
expect do
|
458
|
+
run('test', '--invalid-option')
|
459
|
+
end.to raise_error(SystemExit, /invalid option: --invalid-option/)
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
describe 'with invalid command passed' do
|
464
|
+
it 'should output an invalid command message' do
|
465
|
+
expect do
|
466
|
+
run('foo')
|
467
|
+
end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
describe 'with invalid command passed to help' do
|
472
|
+
it 'should output an invalid command message' do
|
473
|
+
expect do
|
474
|
+
run('help', 'does_not_exist')
|
475
|
+
end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
describe 'with invalid command passed to --help' do
|
480
|
+
it 'should output an invalid command message' do
|
481
|
+
expect do
|
482
|
+
run('--help', 'does_not_exist')
|
483
|
+
end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
describe 'with invalid option passed to --help' do
|
488
|
+
it 'should output an invalid option message' do
|
489
|
+
expect do
|
490
|
+
run('--help', 'test', '--invalid-option')
|
491
|
+
end.to raise_error(SystemExit, /invalid option: --invalid-option/)
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
495
|
+
describe '#valid_command_names_from' do
|
496
|
+
it 'should return array of valid command names' do
|
497
|
+
new_command_runner do
|
498
|
+
command('foo bar') {}
|
499
|
+
command('foo bar foo') {}
|
500
|
+
expect(command_runner.valid_command_names_from('foo', 'bar', 'foo').sort).to eq(['foo bar', 'foo bar foo'])
|
501
|
+
end
|
502
|
+
end
|
503
|
+
|
504
|
+
it 'should return empty array when no possible command names exist' do
|
505
|
+
new_command_runner do
|
506
|
+
expect(command_runner.valid_command_names_from('fake', 'command', 'name')).to eq([])
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
it 'should match exact commands only' do
|
511
|
+
new_command_runner do
|
512
|
+
command('foo') {}
|
513
|
+
expect(command_runner.valid_command_names_from('foobar')).to eq([])
|
514
|
+
end
|
515
|
+
end
|
516
|
+
end
|
517
|
+
|
518
|
+
describe '#command_name_from_args' do
|
519
|
+
it 'should locate command within arbitrary arguments passed' do
|
520
|
+
new_command_runner '--help', '--arbitrary', 'test'
|
521
|
+
expect(command_runner.command_name_from_args).to eq('test')
|
522
|
+
end
|
523
|
+
|
524
|
+
it 'should support multi-word commands' do
|
525
|
+
new_command_runner '--help', '--arbitrary', 'some', 'long', 'command', 'foo'
|
526
|
+
command('some long command') {}
|
527
|
+
expect(command_runner.command_name_from_args).to eq('some long command')
|
528
|
+
end
|
529
|
+
|
530
|
+
it 'should match the longest possible command' do
|
531
|
+
new_command_runner '--help', '--arbitrary', 'foo', 'bar', 'foo'
|
532
|
+
command('foo bar') {}
|
533
|
+
command('foo bar foo') {}
|
534
|
+
expect(command_runner.command_name_from_args).to eq('foo bar foo')
|
535
|
+
end
|
536
|
+
|
537
|
+
it 'should use the left-most command name when multiple are present' do
|
538
|
+
new_command_runner 'help', 'test'
|
539
|
+
expect(command_runner.command_name_from_args).to eq('help')
|
540
|
+
end
|
541
|
+
end
|
542
|
+
|
543
|
+
describe '#active_command' do
|
544
|
+
it 'should resolve the active command' do
|
545
|
+
new_command_runner '--help', 'test'
|
546
|
+
expect(command_runner.active_command).to be_instance_of(Commander::Command)
|
547
|
+
end
|
548
|
+
|
549
|
+
it 'should resolve active command when invalid options are passed' do
|
550
|
+
new_command_runner '--help', 'test', '--arbitrary'
|
551
|
+
expect(command_runner.active_command).to be_instance_of(Commander::Command)
|
552
|
+
end
|
553
|
+
|
554
|
+
it 'should return nil when the command is not found' do
|
555
|
+
new_command_runner 'foo'
|
556
|
+
expect(command_runner.active_command).to be_nil
|
557
|
+
end
|
558
|
+
end
|
559
|
+
|
560
|
+
describe '#default_command' do
|
561
|
+
it 'should allow you to default any command when one is not explicitly passed' do
|
562
|
+
new_command_runner '--trace' do
|
563
|
+
default_command :test
|
564
|
+
expect(command(:test)).to receive(:run).once
|
565
|
+
expect(command_runner.active_command).to eq(command(:test))
|
566
|
+
end.run!
|
567
|
+
end
|
568
|
+
|
569
|
+
it 'should not prevent other commands from being called' do
|
570
|
+
new_command_runner 'foo', 'bar', '--trace' do
|
571
|
+
default_command :test
|
572
|
+
command(:'foo bar') {}
|
573
|
+
expect(command(:'foo bar')).to receive(:run).once
|
574
|
+
expect(command_runner.active_command).to eq(command(:'foo bar'))
|
575
|
+
end.run!
|
576
|
+
end
|
577
|
+
|
578
|
+
it 'should not prevent longer commands to use the same words as the default' do
|
579
|
+
new_command_runner 'foo', 'bar', 'something'
|
580
|
+
default_command :'foo bar'
|
581
|
+
command(:'foo bar') {}
|
582
|
+
command(:'foo bar something') {}
|
583
|
+
expect(command_runner.active_command).to eq(command(:'foo bar something'))
|
584
|
+
end
|
585
|
+
|
586
|
+
it 'should allow defaulting of command aliases' do
|
587
|
+
new_command_runner '--trace' do
|
588
|
+
default_command :foobar
|
589
|
+
alias_command :foobar, :test
|
590
|
+
expect(command(:test)).to receive(:run).once
|
591
|
+
end.run!
|
592
|
+
end
|
593
|
+
end
|
594
|
+
|
595
|
+
describe 'should function correctly' do
|
596
|
+
it 'when options are passed before the command name' do
|
597
|
+
new_command_runner '--verbose', 'test', 'foo', 'bar' do
|
598
|
+
@command.when_called do |args, options|
|
599
|
+
expect(args).to eq(%w(foo bar))
|
600
|
+
expect(options.verbose).to be true
|
601
|
+
end
|
602
|
+
end.run!
|
603
|
+
end
|
604
|
+
|
605
|
+
it 'when options are passed after the command name' do
|
606
|
+
new_command_runner 'test', '--verbose', 'foo', 'bar' do
|
607
|
+
@command.when_called do |args, options|
|
608
|
+
expect(args).to eq(%w(foo bar))
|
609
|
+
expect(options.verbose).to be true
|
610
|
+
end
|
611
|
+
end.run!
|
612
|
+
end
|
613
|
+
|
614
|
+
it 'when an argument passed is the same name as the command' do
|
615
|
+
new_command_runner 'test', '--verbose', 'foo', 'test', 'bar' do
|
616
|
+
@command.when_called do |args, options|
|
617
|
+
expect(args).to eq(%w(foo test bar))
|
618
|
+
expect(options.verbose).to be true
|
619
|
+
end
|
620
|
+
end.run!
|
621
|
+
end
|
622
|
+
|
623
|
+
it 'when using multi-word commands' do
|
624
|
+
new_command_runner '--verbose', 'my', 'command', 'something', 'foo', 'bar' do
|
625
|
+
command('my command') { |c| c.option('--verbose') }
|
626
|
+
expect(command_runner.command_name_from_args).to eq('my command')
|
627
|
+
expect(command_runner.args_without_command_name).to eq(['--verbose', 'something', 'foo', 'bar'])
|
628
|
+
end.run!
|
629
|
+
end
|
630
|
+
|
631
|
+
it 'when using multi-word commands with parts of the command name as arguments' do
|
632
|
+
new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
|
633
|
+
command('my command') { |c| c.option('--verbose') }
|
634
|
+
expect(command_runner.command_name_from_args).to eq('my command')
|
635
|
+
expect(command_runner.args_without_command_name).to eq(['--verbose', 'something', 'my', 'command'])
|
636
|
+
end.run!
|
637
|
+
end
|
638
|
+
|
639
|
+
it 'when using multi-word commands with other commands using the same words' do
|
640
|
+
new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
|
641
|
+
command('my command') {}
|
642
|
+
command('my command something') { |c| c.option('--verbose') }
|
643
|
+
expect(command_runner.command_name_from_args).to eq('my command something')
|
644
|
+
expect(command_runner.args_without_command_name).to eq(['--verbose', 'my', 'command'])
|
645
|
+
end.run!
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
describe 'options with optional arguments' do
|
650
|
+
it 'should return the argument when it is specified' do
|
651
|
+
new_command_runner 'foo', '--optional', 'arg1' do
|
652
|
+
command('foo') do |c|
|
653
|
+
c.option('--optional [argument]')
|
654
|
+
c.when_called do |_, options|
|
655
|
+
expect(options.optional).to eq('arg1')
|
656
|
+
end
|
657
|
+
end
|
658
|
+
end.run!
|
659
|
+
end
|
660
|
+
|
661
|
+
it 'should return true when no argument is specified for the option' do
|
662
|
+
new_command_runner 'foo', '--optional' do
|
663
|
+
command('foo') do |c|
|
664
|
+
c.option('--optional [argument]')
|
665
|
+
c.when_called do |_, options|
|
666
|
+
expect(options.optional).to be true
|
667
|
+
end
|
668
|
+
end
|
669
|
+
end.run!
|
670
|
+
end
|
671
|
+
end
|
672
|
+
end
|