commander 5.0.0 → 6.0.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/spec/runner_spec.rb DELETED
@@ -1,761 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
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 when provided before the command name' do
106
- option = nil
107
- new_command_runner '--global-option', 'option-value', 'command_name' do
108
- global_option('--global-option=GLOBAL', 'A global option')
109
- command :command_name do |c|
110
- c.when_called { |_, options| option = options.global_option }
111
- end
112
- end.run!
113
- expect(option).to eq('option-value')
114
- end
115
-
116
- it 'should be inherited by commands even when a block is present' do
117
- quiet = nil
118
- new_command_runner 'foo', '--quiet' do
119
- global_option('--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 positive form of a [no-] option' do
128
- quiet = nil
129
- new_command_runner 'foo', '--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 true
136
- end
137
-
138
- it 'should be inherited by commands when the negative form of a [no-] option' do
139
- quiet = nil
140
- new_command_runner 'foo', '--no-quiet' do
141
- global_option('--[no-]quiet', 'Suppress output') {}
142
- command :foo do |c|
143
- c.when_called { |_, options| quiet = options.quiet }
144
- end
145
- end.run!
146
- expect(quiet).to be false
147
- end
148
-
149
- it 'should allow command arguments before the global option' do
150
- config = nil
151
- args = nil
152
- new_command_runner 'foo', '--config', 'config-value', 'arg1', 'arg2' do
153
- global_option('-c', '--config CONFIG', String)
154
- command :foo do |c|
155
- c.when_called do |arguments, options|
156
- options.default(config: 'default')
157
- args = arguments
158
- config = options.config
159
- end
160
- end
161
- end.run!
162
- expect(config).to eq('config-value')
163
- expect(args).to eq(%w(arg1 arg2))
164
- end
165
-
166
- it 'should allow command arguments after the global option' do
167
- config = nil
168
- args = nil
169
- new_command_runner 'foo', 'arg1', 'arg2', '--config', 'config-value' do
170
- global_option('-c', '--config CONFIG', String)
171
- command :foo do |c|
172
- c.when_called do |arguments, options|
173
- options.default(config: 'default')
174
- args = arguments
175
- config = options.config
176
- end
177
- end
178
- end.run!
179
- expect(config).to eq('config-value')
180
- expect(args).to eq(%w(arg1 arg2))
181
- end
182
-
183
- it 'allows global options in the form option=value' do
184
- config = nil
185
- args = nil
186
- new_command_runner 'test', 'arg1', '--config=config-value', 'arg2' do
187
- global_option('-c', '--config CONFIG', String)
188
- command :test do |c|
189
- c.when_called do |arguments, options|
190
- options.default(config: 'default')
191
- args = arguments
192
- config = options.config
193
- end
194
- end
195
- end.run!
196
- expect(config).to eq('config-value')
197
- expect(args).to eq(%w[arg1 arg2])
198
- end
199
- end
200
-
201
- describe '#parse_global_options' do
202
- it 'should parse global options before command' do
203
- global_option = nil
204
- new_command_runner('--testing-global', 'foo') do
205
- global_option('--testing-global') { global_option = 'MAGIC' }
206
-
207
- command :foo do |c|
208
- c.when_called {}
209
- end
210
- end.run!
211
- expect(global_option).to eq('MAGIC')
212
- end
213
-
214
- it 'should parse global options after command' do
215
- global_option = nil
216
- new_command_runner('foo', '--testing-global') do
217
- global_option('--testing-global') { global_option = 'MAGIC' }
218
-
219
- command :foo do |c|
220
- c.when_called {}
221
- end
222
- end.run!
223
- expect(global_option).to eq('MAGIC')
224
- end
225
-
226
- it 'should parse global options placed before command options' do
227
- global_option = nil
228
- new_command_runner('foo', '--testing-global', '--testing-command') do
229
- global_option('--testing-global') { global_option = 'MAGIC' }
230
-
231
- command :foo do |c|
232
- c.option('--testing-command') {}
233
- c.when_called {}
234
- end
235
- end.run!
236
-
237
- expect(global_option).to eq('MAGIC')
238
- end
239
-
240
- it 'should parse global options placed after command options' do
241
- global_option = nil
242
- new_command_runner('foo', '--testing-command', '--testing-global') do
243
- global_option('--testing-global') { global_option = 'MAGIC' }
244
-
245
- command :foo do |c|
246
- c.option('--testing-command') {}
247
- c.when_called {}
248
- end
249
- end.run!
250
-
251
- expect(global_option).to eq('MAGIC')
252
- end
253
-
254
- it 'should parse global options surrounded by command options' do
255
- global_option = nil
256
- new_command_runner('foo', '--testing-command', '--testing-global', '--other-command') do
257
- global_option('--testing-global') { global_option = 'MAGIC' }
258
-
259
- command :foo do |c|
260
- c.option('--testing-command') {}
261
- c.option('--other-command') {}
262
- c.when_called {}
263
- end
264
- end.run!
265
-
266
- expect(global_option).to eq('MAGIC')
267
- end
268
-
269
- it 'should not parse command options' do
270
- global_option = nil
271
- command_option = nil
272
- new_command_runner('foo', '--testing-command', '--testing-global') do
273
- global_option('--testing-global') { global_option = 'MAGIC' }
274
-
275
- command :foo do |c|
276
- c.option('--testing-command') { command_option = 'NO!' }
277
- c.when_called {}
278
- end
279
- end.parse_global_options
280
-
281
- expect(command_option).to be_nil
282
- expect(global_option).to eq('MAGIC')
283
- end
284
-
285
- it 'should not affect command arguments with values' do
286
- global_option = nil
287
- command_option = nil
288
- new_command_runner('foo', '--testing-command', 'bar', '--testing-global') do
289
- global_option('--testing-global') { global_option = 'MAGIC' }
290
-
291
- command :foo do |c|
292
- c.option('--testing-command VALUE') { |v| command_option = v }
293
- c.when_called {}
294
- end
295
- end.run!
296
-
297
- expect(command_option).to eq('bar')
298
- expect(global_option).to eq('MAGIC')
299
- end
300
-
301
- it 'should not affect global arguments with values' do
302
- global_option = nil
303
- new_command_runner('foo', '--testing-command', '--testing-global', 'bar') do
304
- global_option('--testing-global VALUE') { |v| global_option = v }
305
-
306
- command :foo do |c|
307
- c.option('--testing-command') {}
308
- c.when_called {}
309
- end
310
- end.run!
311
-
312
- expect(global_option).to eq('bar')
313
- end
314
-
315
- it 'should allow global arguments with values before command arguments (github issue #8)' do
316
- global_option = nil
317
- command_option = nil
318
- new_command_runner('foo', '--config', 'path', 'bar') do
319
- global_option('--config VALUE') { |v| global_option = v }
320
-
321
- command :foo do |c|
322
- c.option('bar') { command_option = 'bar' }
323
- c.when_called {}
324
- end
325
- end.run!
326
-
327
- expect(global_option).to eq('path')
328
- expect(command_option).to eq('bar')
329
- end
330
- end
331
-
332
- describe '#remove_global_options' do
333
- it 'should remove only specified switches' do
334
- options, args = [], []
335
- options << { switches: ['-t', '--trace'] }
336
- options << { switches: ['--help'] }
337
- options << { switches: ['--paths PATHS'] }
338
- args << '-t'
339
- args << '--help'
340
- args << '--command'
341
- args << '--command-with-arg' << 'rawr'
342
- args << '--paths' << '"lib/**/*.js","spec/**/*.js"'
343
- command_runner.remove_global_options options, args
344
- expect(args).to eq(['--command', '--command-with-arg', 'rawr'])
345
- end
346
-
347
- it 'should not swallow an argument unless it expects an argument' do
348
- options, args = [], []
349
- options << { switches: ['-n', '--no-arg'] }
350
- options << { switches: ['-y', '--yes ARG'] }
351
- options << { switches: ['-a', '--alternative=ARG'] }
352
- args << '-n' << 'alpha'
353
- args << '--yes' << 'deleted'
354
- args << '-a' << 'deleted'
355
- args << 'beta'
356
- command_runner.remove_global_options options, args
357
- expect(args).to eq(%w(alpha beta))
358
- end
359
-
360
- it 'should remove a switch that is the positive form of the [no-] option' do
361
- options, args = [], []
362
- options << { switches: ['-g', '--[no-]good'] }
363
- options << { switches: ['-y', '--yes ARG'] }
364
- options << { switches: ['-a', '--alternative=ARG'] }
365
- args << '--good' << 'alpha'
366
- args << '--yes' << 'deleted'
367
- args << '-a' << 'deleted'
368
- args << 'beta'
369
- command_runner.remove_global_options options, args
370
- expect(args).to eq(%w(alpha beta))
371
- end
372
-
373
- it 'should remove a switch that is the negative form of the [no-] option' do
374
- options, args = [], []
375
- options << { switches: ['-g', '--[no-]good'] }
376
- options << { switches: ['-y', '--yes ARG'] }
377
- options << { switches: ['-a', '--alternative=ARG'] }
378
- args << '--no-good' << 'alpha'
379
- args << '--yes' << 'deleted'
380
- args << '-a' << 'deleted'
381
- args << 'beta'
382
- command_runner.remove_global_options options, args
383
- expect(args).to eq(%w(alpha beta))
384
- end
385
-
386
- it 'should not remove options that start with a global option name' do
387
- options, args = [], []
388
- options << { switches: ['-v', '--version'] }
389
- args << '--versionCode' << 'something'
390
- command_runner.remove_global_options options, args
391
- expect(args).to eq(%w(--versionCode something))
392
- end
393
-
394
- it 'should remove specified switches value provided via equals' do
395
- options = [{ switches: ['--global GLOBAL'] }]
396
- args = ['--command', '--global=option-value', 'arg']
397
- command_runner.remove_global_options options, args
398
- expect(args).to eq(['--command', 'arg'])
399
- end
400
-
401
- it 'should not remove extra values after switches' do
402
- options = [{ switches: ['--global GLOBAL'] }]
403
- args = ['--global', '--command', 'arg']
404
- command_runner.remove_global_options options, args
405
- expect(args).to eq(['--command', 'arg'])
406
- end
407
- end
408
-
409
- describe '--trace' do
410
- it 'should display pretty errors by default' do
411
- expect do
412
- new_command_runner 'foo' do
413
- command(:foo) { |c| c.when_called { fail 'cookies!' } }
414
- end.run!
415
- end.to raise_error(TestSystemExit, /error: cookies!. Use --trace/)
416
- end
417
-
418
- it 'should display callstack when using this switch' do
419
- expect do
420
- new_command_runner 'foo', '--trace' do
421
- command(:foo) { |c| c.when_called { fail 'cookies!' } }
422
- end.run!
423
- end.to raise_error(RuntimeError)
424
- end
425
- end
426
-
427
- describe '#always_trace!' do
428
- it 'should enable tracing globally, regardless of whether --trace was passed or not' do
429
- expect do
430
- new_command_runner 'foo' do
431
- always_trace!
432
- command(:foo) { |c| c.when_called { fail 'cookies!' } }
433
- end.run!
434
- end.to raise_error(RuntimeError)
435
- end
436
- end
437
-
438
- describe '#never_trace!' do
439
- it 'should disable tracing globally, regardless of whether --trace was passed or not' do
440
- expect do
441
- new_command_runner 'help', '--trace' do
442
- never_trace!
443
- end.run!
444
- end.to raise_error(TestSystemExit, /invalid option: --trace/)
445
- end
446
-
447
- it 'should not prompt to use --trace switch on errors' do
448
- msg = nil
449
- begin
450
- new_command_runner 'foo' do
451
- never_trace!
452
- command(:foo) { |c| c.when_called { fail 'cookies!' } }
453
- end.run!
454
- rescue TestSystemExit => e
455
- msg = e.message
456
- end
457
- expect(msg).to match(/error: cookies!/)
458
- expect(msg).not_to match(/--trace/)
459
- end
460
- end
461
-
462
- context 'conflict between #always_trace! and #never_trace!' do
463
- it 'respects the last used command' do
464
- expect do
465
- new_command_runner 'foo' do
466
- never_trace!
467
- always_trace!
468
- command(:foo) { |c| c.when_called { fail 'cookies!' } }
469
- end.run!
470
- end.to raise_error(RuntimeError)
471
- end
472
- end
473
-
474
- describe '--version' do
475
- it 'should output program version' do
476
- expect(run('--version')).to eq("test 1.2.3\n")
477
- end
478
- end
479
-
480
- describe '--help' do
481
- it 'should not output an invalid command message' do
482
- expect(run('--help')).not_to eq("invalid command. Use --help for more information\n")
483
- end
484
-
485
- it 'can be used before or after the command and options' do
486
- expect(run('test', '--help')).to eq("Implement help for test here\n")
487
- end
488
-
489
- it 'can be used after the command and command arguments' do
490
- expect(run('test', 'command-arg', '--help')).to eq("Implement help for test here\n")
491
- end
492
-
493
- it 'can be used before a single-word command with command arguments' do
494
- expect(run('help', 'test', 'command-arg')).to eq("Implement help for test here\n")
495
- end
496
-
497
- it 'can be used before a multi-word command with command arguments' do
498
- expect(
499
- run('help', 'module', 'install', 'command-arg') do
500
- command('module install') { |c| c.when_called { say 'whee!' } }
501
- end
502
- ).to eq("Implement help for module install here\n")
503
- end
504
-
505
- describe 'help_paging program information' do
506
- it 'enables paging when enabled' do
507
- run('--help') { program :help_paging, true }
508
- expect(Commander::UI).to have_received(:enable_paging)
509
- end
510
-
511
- it 'is enabled by default' do
512
- run('--help')
513
- expect(Commander::UI).to have_received(:enable_paging)
514
- end
515
-
516
- it 'does not enable paging when disabled' do
517
- run('--help') { program :help_paging, false }
518
- expect(Commander::UI).not_to have_received(:enable_paging)
519
- end
520
- end
521
- end
522
-
523
- describe 'with invalid options' do
524
- it 'should output an invalid option message' do
525
- expect do
526
- run('test', '--invalid-option')
527
- end.to raise_error(TestSystemExit, /invalid option: --invalid-option/)
528
- end
529
- end
530
-
531
- describe 'with invalid command passed' do
532
- it 'should output an invalid command message' do
533
- expect do
534
- run('foo')
535
- end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
536
- end
537
- end
538
-
539
- describe 'with invalid command passed to help' do
540
- it 'should output an invalid command message' do
541
- expect do
542
- run('help', 'does_not_exist')
543
- end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
544
- end
545
- end
546
-
547
- describe 'with invalid command passed to --help' do
548
- it 'should output an invalid command message' do
549
- expect do
550
- run('--help', 'does_not_exist')
551
- end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
552
- end
553
- end
554
-
555
- describe 'with invalid option passed to --help' do
556
- it 'should output an invalid option message' do
557
- expect do
558
- run('--help', 'test', '--invalid-option')
559
- end.to raise_error(TestSystemExit, /invalid option: --invalid-option/)
560
- end
561
- end
562
-
563
- describe '#valid_command_names_from' do
564
- it 'should return array of valid command names' do
565
- new_command_runner do
566
- command('foo bar') {}
567
- command('foo bar foo') {}
568
- expect(command_runner.valid_command_names_from('foo', 'bar', 'foo').sort).to eq(['foo bar', 'foo bar foo'])
569
- end
570
- end
571
-
572
- it 'should return empty array when no possible command names exist' do
573
- new_command_runner do
574
- expect(command_runner.valid_command_names_from('fake', 'command', 'name')).to eq([])
575
- end
576
- end
577
-
578
- it 'should match exact commands only' do
579
- new_command_runner do
580
- command('foo') {}
581
- expect(command_runner.valid_command_names_from('foobar')).to eq([])
582
- end
583
- end
584
- end
585
-
586
- describe '#command_name_from_args' do
587
- it 'should locate command within arbitrary arguments passed' do
588
- new_command_runner '--help', '--arbitrary', 'test'
589
- expect(command_runner.command_name_from_args).to eq('test')
590
- end
591
-
592
- it 'should locate command when provided after a global argument with value' do
593
- new_command_runner '--global-option', 'option-value', 'test' do
594
- global_option('--global-option=GLOBAL', 'A global option')
595
- end
596
- expect(command_runner.command_name_from_args).to eq('test')
597
- end
598
-
599
- it 'should support multi-word commands' do
600
- new_command_runner '--help', '--arbitrary', 'some', 'long', 'command', 'foo'
601
- command('some long command') {}
602
- expect(command_runner.command_name_from_args).to eq('some long command')
603
- end
604
-
605
- it 'should match the longest possible command' do
606
- new_command_runner '--help', '--arbitrary', 'foo', 'bar', 'foo'
607
- command('foo bar') {}
608
- command('foo bar foo') {}
609
- expect(command_runner.command_name_from_args).to eq('foo bar foo')
610
- end
611
-
612
- it 'should use the left-most command name when multiple are present' do
613
- new_command_runner 'help', 'test'
614
- expect(command_runner.command_name_from_args).to eq('help')
615
- end
616
- end
617
-
618
- describe '#active_command' do
619
- it 'should resolve the active command' do
620
- new_command_runner '--help', 'test'
621
- expect(command_runner.active_command).to be_instance_of(Commander::Command)
622
- end
623
-
624
- it 'should resolve active command when invalid options are passed' do
625
- new_command_runner '--help', 'test', '--arbitrary'
626
- expect(command_runner.active_command).to be_instance_of(Commander::Command)
627
- end
628
-
629
- it 'should return nil when the command is not found' do
630
- new_command_runner 'foo'
631
- expect(command_runner.active_command).to be_nil
632
- end
633
- end
634
-
635
- describe '#default_command' do
636
- it 'should allow you to default any command when one is not explicitly passed' do
637
- new_command_runner '--trace' do
638
- default_command :test
639
- expect(command(:test)).to receive(:run).once
640
- expect(command_runner.active_command).to eq(command(:test))
641
- end.run!
642
- end
643
-
644
- it 'should not prevent other commands from being called' do
645
- new_command_runner 'foo', 'bar', '--trace' do
646
- default_command :test
647
- command(:'foo bar') {}
648
- expect(command(:'foo bar')).to receive(:run).once
649
- expect(command_runner.active_command).to eq(command(:'foo bar'))
650
- end.run!
651
- end
652
-
653
- it 'should not prevent longer commands to use the same words as the default' do
654
- new_command_runner 'foo', 'bar', 'something'
655
- default_command :'foo bar'
656
- command(:'foo bar') {}
657
- command(:'foo bar something') {}
658
- expect(command_runner.active_command).to eq(command(:'foo bar something'))
659
- end
660
-
661
- it 'should allow defaulting of command aliases' do
662
- new_command_runner '--trace' do
663
- default_command :foobar
664
- alias_command :foobar, :test
665
- expect(command(:test)).to receive(:run).once
666
- end.run!
667
- end
668
- end
669
-
670
- describe 'should function correctly' do
671
- it 'when options are passed before the command name' do
672
- new_command_runner '--verbose', 'test', 'foo', 'bar' do
673
- @command.when_called do |args, options|
674
- expect(args).to eq(%w(foo bar))
675
- expect(options.verbose).to be true
676
- end
677
- end.run!
678
- end
679
-
680
- it 'when options are passed after the command name' do
681
- new_command_runner 'test', '--verbose', 'foo', 'bar' do
682
- @command.when_called do |args, options|
683
- expect(args).to eq(%w(foo bar))
684
- expect(options.verbose).to be true
685
- end
686
- end.run!
687
- end
688
-
689
- it 'when an argument passed is the same name as the command' do
690
- new_command_runner 'test', '--verbose', 'foo', 'test', 'bar' do
691
- @command.when_called do |args, options|
692
- expect(args).to eq(%w(foo test bar))
693
- expect(options.verbose).to be true
694
- end
695
- end.run!
696
- end
697
-
698
- it 'when using multi-word commands' do
699
- new_command_runner '--verbose', 'my', 'command', 'something', 'foo', 'bar' do
700
- command('my command') { |c| c.option('--verbose') }
701
- expect(command_runner.command_name_from_args).to eq('my command')
702
- expect(command_runner.args_without_command_name).to eq(['--verbose', 'something', 'foo', 'bar'])
703
- end.run!
704
- end
705
-
706
- it 'when using multi-word commands with parts of the command name as arguments' do
707
- new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
708
- command('my command') { |c| c.option('--verbose') }
709
- expect(command_runner.command_name_from_args).to eq('my command')
710
- expect(command_runner.args_without_command_name).to eq(['--verbose', 'something', 'my', 'command'])
711
- end.run!
712
- end
713
-
714
- it 'when using multi-word commands with other commands using the same words' do
715
- new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
716
- command('my command') {}
717
- command('my command something') { |c| c.option('--verbose') }
718
- expect(command_runner.command_name_from_args).to eq('my command something')
719
- expect(command_runner.args_without_command_name).to eq(['--verbose', 'my', 'command'])
720
- end.run!
721
- end
722
- end
723
-
724
- describe 'options with optional arguments' do
725
- it 'should return the argument when it is specified' do
726
- new_command_runner 'foo', '--optional', 'arg1' do
727
- command('foo') do |c|
728
- c.option('--optional [argument]')
729
- c.when_called do |_, options|
730
- expect(options.optional).to eq('arg1')
731
- end
732
- end
733
- end.run!
734
- end
735
-
736
- it 'should return true when no argument is specified for the option' do
737
- new_command_runner 'foo', '--optional' do
738
- command('foo') do |c|
739
- c.option('--optional [argument]')
740
- c.when_called do |_, options|
741
- expect(options.optional).to be true
742
- end
743
- end
744
- end.run!
745
- end
746
- end
747
-
748
- describe 'with double dash' do
749
- it 'should interpret the remainder as arguments' do
750
- new_command_runner 'foo', '--', '-x' do
751
- command('foo') do |c|
752
- c.option '-x', 'Switch'
753
- c.when_called do |args, options|
754
- expect(args).to eq(%w(-x))
755
- expect(options.x).to be_nil
756
- end
757
- end
758
- end.run!
759
- end
760
- end
761
- end