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