commander 4.4.7 → 4.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bb88c28e0d43c3616cceb161661a306fbcb74ef6c68ce67bc5aee8e820498cd
4
- data.tar.gz: 9c5295c278179f4325c179b61a07182c45f3ed911b8228294cdf53e83cb6fe74
3
+ metadata.gz: c911ee8aef66db7dbe8045f9f996ff0b810afeacb6f8efc7fc288e322937f59a
4
+ data.tar.gz: b22b126f597fad5adcc4ecc44c60ac57ac70b3ce2ffeb646d8001eabf088da8d
5
5
  SHA512:
6
- metadata.gz: 977685b95002c9e0dcaafd60da783473dc1e2d90f72eec6c1fc209d7ba6ff498f7941f2ac263244775d58d5f41b7bda7e57d7a39041963d8ec85852c20b99ee9
7
- data.tar.gz: 2c2eb1a834b1446b61d0ac7dbeb8f0575f80ab10e57d432a6710c4e37eb24eb870a6312655be5f27f3d4250505320f04fb46c763f91ef688b9e6c96fa573b45e
6
+ metadata.gz: '04589993f113b0ed3aab4d90ae8a963f5d68999535f523def84e9d6d2a355c0a5b2bbdc4a5969c0bcf8d44e6d1a40f42e1e6f5bbafe35126b321ef46c05f235f'
7
+ data.tar.gz: 52c16dd44e1801cb3ee353332dada749c013b452d3976c1a9aed9829d16b545f20b625cadcb37fba4fb09468c2c17a04d1dce14f665b2cdd4ac14e664d2a79fd
data/.travis.yml CHANGED
@@ -4,11 +4,10 @@ before_install:
4
4
  - gem update --system
5
5
  - gem update bundler
6
6
  rvm:
7
- - 1.9.3
8
- - 2.0.0
9
- - 2.1.10
10
- - 2.2.9
11
- - 2.3.6
12
- - 2.4.3
13
- - 2.5.0
14
- - jruby-19mode
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ - 2.6
11
+ - 2.7
12
+ - jruby
13
+ - ruby-head
data/History.rdoc CHANGED
@@ -1,3 +1,18 @@
1
+ === 4.5.2 / 2020-03-12
2
+
3
+ * Fix bug handling global options provided in option=value form (#47). (@orien)
4
+ * Fix ERB warnings under Ruby 2.7. (@esotericpig)
5
+ * Fix bug handling global options placed before command name (#32). (@orien)
6
+
7
+ === 4.5.1 / 2020-03-08
8
+
9
+ * Fix bug causing global options to be ignored when arguments are present (#86). (@orien)
10
+
11
+ === 4.5.0 / 2020-01-21
12
+
13
+ * Drop support for Ruby < 2.3.
14
+ * Fix bug parsing double dash (#75).
15
+
1
16
  === 4.4.7 / 2018-10-22
2
17
 
3
18
  * Update HighLine dependency to 2.0.0. (@rohitpaulk)
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- [<img src="https://api.travis-ci.org/commander-rb/commander.svg" alt="Build Status" />](http://travis-ci.org/commander-rb/commander)
2
- [![Inline docs](http://inch-ci.org/github/commander-rb/commander.svg)](http://inch-ci.org/github/commander-rb/commander)
1
+ [<img src="https://api.travis-ci.org/commander-rb/commander.svg" alt="Build Status" />](https://travis-ci.org/commander-rb/commander)
2
+ [![Inline docs](https://inch-ci.org/github/commander-rb/commander.svg)](https://inch-ci.org/github/commander-rb/commander)
3
3
 
4
4
  # Commander
5
5
 
@@ -243,8 +243,8 @@ end
243
243
  ## Growl Notifications
244
244
 
245
245
  Commander provides methods for displaying Growl notifications. To use these
246
- methods you need to install http://github.com/tj/growl which utilizes
247
- the [growlnotify](http://growl.info/extras.php#growlnotify) executable. Note that
246
+ methods you need to install https://github.com/tj/growl which utilizes
247
+ the [growlnotify](https://growl.info/extras.php#growlnotify) executable. Note that
248
248
  growl is auto-imported by Commander when available, no need to require.
249
249
 
250
250
  ```ruby
@@ -449,7 +449,7 @@ global_option '--config FILE'
449
449
 
450
450
  ## ASCII Tables
451
451
 
452
- For feature rich ASCII tables for your terminal app check out the terminal-table gem at http://github.com/tj/terminal-table
452
+ For feature rich ASCII tables for your terminal app check out the terminal-table gem at https://github.com/tj/terminal-table
453
453
 
454
454
  +----------+-------+----+--------+-----------------------+
455
455
  | Terminal | Table | Is | Wicked | Awesome |
@@ -468,7 +468,7 @@ OR
468
468
  ## Contrib
469
469
 
470
470
  Feel free to fork and request a pull, or submit a ticket
471
- http://github.com/commander-rb/commander/issues
471
+ https://github.com/commander-rb/commander/issues
472
472
 
473
473
  ## License
474
474
 
data/commander.gemspec CHANGED
@@ -12,6 +12,13 @@ Gem::Specification.new do |s|
12
12
  s.homepage = 'https://github.com/commander-rb/commander'
13
13
  s.summary = 'The complete solution for Ruby command-line executables'
14
14
  s.description = 'The complete solution for Ruby command-line executables. Commander bridges the gap between other terminal related libraries you know and love (OptionParser, HighLine), while providing many new features, and an elegant API.'
15
+ s.metadata = {
16
+ 'bug_tracker_uri' => "#{s.homepage}/issues",
17
+ 'changelog_uri' => "#{s.homepage}/blob/master/History.rdoc",
18
+ 'documentation_uri' => "https://www.rubydoc.info/gems/commander/#{s.version}",
19
+ 'homepage_uri' => s.homepage,
20
+ 'source_code_uri' => "#{s.homepage}/tree/v#{s.version}",
21
+ }
15
22
 
16
23
  s.files = `git ls-files`.split("\n")
17
24
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -4,6 +4,7 @@ module Commander
4
4
  class Command
5
5
  attr_accessor :name, :examples, :syntax, :description
6
6
  attr_accessor :summary, :proxy_options, :options
7
+ attr_reader :global_options
7
8
 
8
9
  ##
9
10
  # Options struct.
@@ -38,6 +39,7 @@ module Commander
38
39
  def initialize(name)
39
40
  @name, @examples, @when_called = name.to_s, [], []
40
41
  @options, @proxy_options = [], []
42
+ @global_options = []
41
43
  end
42
44
 
43
45
  ##
@@ -190,7 +192,7 @@ module Commander
190
192
  # collected by the #option_proc.
191
193
 
192
194
  def proxy_option_struct
193
- proxy_options.each_with_object(Options.new) do |(option, value), options|
195
+ (global_options + proxy_options).each_with_object(Options.new) do |(option, value), options|
194
196
  # options that are present will evaluate to true
195
197
  value = true if value.nil?
196
198
  options.__send__ :"#{option}=", value
@@ -12,7 +12,11 @@ module Commander
12
12
  end
13
13
 
14
14
  def template(name)
15
- ERB.new(File.read(File.join(File.dirname(__FILE__), 'terminal', "#{name}.erb")), nil, '-')
15
+ if RUBY_VERSION < '2.6'
16
+ ERB.new(File.read(File.join(File.dirname(__FILE__), 'terminal', "#{name}.erb")), nil, '-')
17
+ else
18
+ ERB.new(File.read(File.join(File.dirname(__FILE__), 'terminal', "#{name}.erb")), trim_mode: '-')
19
+ end
16
20
  end
17
21
  end
18
22
  end
@@ -4,7 +4,11 @@ module Commander
4
4
  module HelpFormatter
5
5
  class TerminalCompact < Terminal
6
6
  def template(name)
7
- ERB.new(File.read(File.join(File.dirname(__FILE__), 'terminal_compact', "#{name}.erb")), nil, '-')
7
+ if RUBY_VERSION < '2.6'
8
+ ERB.new(File.read(File.join(File.dirname(__FILE__), 'terminal_compact', "#{name}.erb")), nil, '-')
9
+ else
10
+ ERB.new(File.read(File.join(File.dirname(__FILE__), 'terminal_compact', "#{name}.erb")), trim_mode: '-')
11
+ end
8
12
  end
9
13
  end
10
14
  end
@@ -246,6 +246,7 @@ module Commander
246
246
  # Returns array of valid command names found within _args_.
247
247
 
248
248
  def valid_command_names_from(*args)
249
+ remove_global_options options, args
249
250
  arg_string = args.delete_if { |value| value =~ /^-/ }.join ' '
250
251
  commands.keys.find_all { |name| name if arg_string =~ /^#{name}\b/ }
251
252
  end
@@ -328,26 +329,31 @@ module Commander
328
329
  # again for the command.
329
330
 
330
331
  def remove_global_options(options, args)
331
- # TODO: refactor with flipflop, please TJ ! have time to refactor me !
332
332
  options.each do |option|
333
- switches = option[:switches].dup
333
+ switches = option[:switches]
334
334
  next if switches.empty?
335
-
336
- if (switch_has_arg = switches.any? { |s| s =~ /[ =]/ })
337
- switches.map! { |s| s[0, s.index('=') || s.index(' ') || s.length] }
338
- end
339
-
335
+ option_takes_argument = switches.any? { |s| s =~ /[ =]/ }
340
336
  switches = expand_optionally_negative_switches(switches)
341
337
 
342
- past_switch, arg_removed = false, false
343
- args.delete_if do |arg|
344
- if switches.any? { |s| s[0, arg.length] == arg }
345
- arg_removed = !switch_has_arg
346
- past_switch = true
347
- elsif past_switch && !arg_removed && arg !~ /^-/
348
- arg_removed = true
338
+ option_argument_needs_removal = false
339
+ args.delete_if do |token|
340
+ break if token == '--'
341
+
342
+ # Use just the portion of the token before the = when
343
+ # comparing switches.
344
+ index_of_equals = token.index('=') if option_takes_argument
345
+ token = token[0, index_of_equals] if index_of_equals
346
+ token_contains_option_argument = !index_of_equals.nil?
347
+
348
+ if switches.any? { |s| s[0, token.length] == token }
349
+ option_argument_needs_removal =
350
+ option_takes_argument && !token_contains_option_argument
351
+ true
352
+ elsif option_argument_needs_removal && token !~ /^-/
353
+ option_argument_needs_removal = false
354
+ true
349
355
  else
350
- arg_removed = true
356
+ option_argument_needs_removal = false
351
357
  false
352
358
  end
353
359
  end
@@ -395,7 +401,7 @@ module Commander
395
401
  def global_option_proc(switches, &block)
396
402
  lambda do |value|
397
403
  unless active_command.nil?
398
- active_command.proxy_options << [Runner.switch_to_sym(switches.last), value]
404
+ active_command.global_options << [Runner.switch_to_sym(switches.last), value]
399
405
  end
400
406
  yield value if block && !value.nil?
401
407
  end
@@ -1,3 +1,3 @@
1
1
  module Commander
2
- VERSION = '4.4.7'.freeze
2
+ VERSION = '4.5.2'.freeze
3
3
  end
data/spec/command_spec.rb CHANGED
@@ -164,6 +164,33 @@ describe Commander::Command do
164
164
  end
165
165
  @command.run '--interval', '15'
166
166
  end
167
+
168
+ describe 'given a global option' do
169
+ before do
170
+ @command.global_options << [:global_option, 'gvalue']
171
+ end
172
+
173
+ describe 'and no command specific arguments' do
174
+ it 'provides the global option to the command action' do
175
+ @command.when_called { |_, options| expect(options.global_option).to eq('gvalue') }
176
+ @command.run
177
+ end
178
+ end
179
+
180
+ describe 'and a command specific option' do
181
+ it 'provides the global option to the command action' do
182
+ @command.when_called { |_, options| expect(options.global_option).to eq('gvalue') }
183
+ @command.run '--verbose'
184
+ end
185
+ end
186
+
187
+ describe 'and a command specific argument' do
188
+ it 'provides the global option to the command action' do
189
+ @command.when_called { |_, options| expect(options.global_option).to eq('gvalue') }
190
+ @command.run 'argument'
191
+ end
192
+ end
193
+ end
167
194
  end
168
195
  end
169
196
  end
data/spec/runner_spec.rb CHANGED
@@ -100,6 +100,17 @@ describe Commander do
100
100
  expect(quiet).to be true
101
101
  end
102
102
 
103
+ it 'should be inherited by commands when provided before the command name' do
104
+ option = nil
105
+ new_command_runner '--global-option', 'option-value', 'command_name' do
106
+ global_option('--global-option=GLOBAL', 'A global option')
107
+ command :command_name do |c|
108
+ c.when_called { |_, options| option = options.global_option }
109
+ end
110
+ end.run!
111
+ expect(option).to eq('option-value')
112
+ end
113
+
103
114
  it 'should be inherited by commands even when a block is present' do
104
115
  quiet = nil
105
116
  new_command_runner 'foo', '--quiet' do
@@ -132,6 +143,57 @@ describe Commander do
132
143
  end.run!
133
144
  expect(quiet).to be false
134
145
  end
146
+
147
+ it 'should allow command arguments before the global option' do
148
+ config = nil
149
+ args = nil
150
+ new_command_runner 'foo', '--config', 'config-value', 'arg1', 'arg2' do
151
+ global_option('-c', '--config CONFIG', String)
152
+ command :foo do |c|
153
+ c.when_called do |arguments, options|
154
+ options.default(config: 'default')
155
+ args = arguments
156
+ config = options.config
157
+ end
158
+ end
159
+ end.run!
160
+ expect(config).to eq('config-value')
161
+ expect(args).to eq(%w(arg1 arg2))
162
+ end
163
+
164
+ it 'should allow command arguments after the global option' do
165
+ config = nil
166
+ args = nil
167
+ new_command_runner 'foo', 'arg1', 'arg2', '--config', 'config-value' do
168
+ global_option('-c', '--config CONFIG', String)
169
+ command :foo do |c|
170
+ c.when_called do |arguments, options|
171
+ options.default(config: 'default')
172
+ args = arguments
173
+ config = options.config
174
+ end
175
+ end
176
+ end.run!
177
+ expect(config).to eq('config-value')
178
+ expect(args).to eq(%w(arg1 arg2))
179
+ end
180
+
181
+ it 'allows global options in the form option=value' do
182
+ config = nil
183
+ args = nil
184
+ new_command_runner 'test', 'arg1', '--config=config-value', 'arg2' do
185
+ global_option('-c', '--config CONFIG', String)
186
+ command :test do |c|
187
+ c.when_called do |arguments, options|
188
+ options.default(config: 'default')
189
+ args = arguments
190
+ config = options.config
191
+ end
192
+ end
193
+ end.run!
194
+ expect(config).to eq('config-value')
195
+ expect(args).to eq(%w[arg1 arg2])
196
+ end
135
197
  end
136
198
 
137
199
  describe '#parse_global_options' do
@@ -326,6 +388,20 @@ describe Commander do
326
388
  command_runner.remove_global_options options, args
327
389
  expect(args).to eq(%w(--versionCode something))
328
390
  end
391
+
392
+ it 'should remove specified switches value provided via equals' do
393
+ options = [{ switches: ['--global GLOBAL'] }]
394
+ args = ['--command', '--global=option-value', 'arg']
395
+ command_runner.remove_global_options options, args
396
+ expect(args).to eq(['--command', 'arg'])
397
+ end
398
+
399
+ it 'should not remove extra values after switches' do
400
+ options = [{ switches: ['--global GLOBAL'] }]
401
+ args = ['--global', '--command', 'arg']
402
+ command_runner.remove_global_options options, args
403
+ expect(args).to eq(['--command', 'arg'])
404
+ end
329
405
  end
330
406
 
331
407
  describe '--trace' do
@@ -334,7 +410,7 @@ describe Commander do
334
410
  new_command_runner 'foo' do
335
411
  command(:foo) { |c| c.when_called { fail 'cookies!' } }
336
412
  end.run!
337
- end.to raise_error(SystemExit, /error: cookies!. Use --trace/)
413
+ end.to raise_error(TestSystemExit, /error: cookies!. Use --trace/)
338
414
  end
339
415
 
340
416
  it 'should display callstack when using this switch' do
@@ -363,7 +439,7 @@ describe Commander do
363
439
  new_command_runner 'help', '--trace' do
364
440
  never_trace!
365
441
  end.run!
366
- end.to raise_error(SystemExit, /invalid option: --trace/)
442
+ end.to raise_error(TestSystemExit, /invalid option: --trace/)
367
443
  end
368
444
 
369
445
  it 'should not prompt to use --trace switch on errors' do
@@ -373,7 +449,7 @@ describe Commander do
373
449
  never_trace!
374
450
  command(:foo) { |c| c.when_called { fail 'cookies!' } }
375
451
  end.run!
376
- rescue SystemExit => e
452
+ rescue TestSystemExit => e
377
453
  msg = e.message
378
454
  end
379
455
  expect(msg).to match(/error: cookies!/)
@@ -430,7 +506,7 @@ describe Commander do
430
506
  it 'should output an invalid option message' do
431
507
  expect do
432
508
  run('test', '--invalid-option')
433
- end.to raise_error(SystemExit, /invalid option: --invalid-option/)
509
+ end.to raise_error(TestSystemExit, /invalid option: --invalid-option/)
434
510
  end
435
511
  end
436
512
 
@@ -438,7 +514,7 @@ describe Commander do
438
514
  it 'should output an invalid command message' do
439
515
  expect do
440
516
  run('foo')
441
- end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
517
+ end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
442
518
  end
443
519
  end
444
520
 
@@ -446,7 +522,7 @@ describe Commander do
446
522
  it 'should output an invalid command message' do
447
523
  expect do
448
524
  run('help', 'does_not_exist')
449
- end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
525
+ end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
450
526
  end
451
527
  end
452
528
 
@@ -454,7 +530,7 @@ describe Commander do
454
530
  it 'should output an invalid command message' do
455
531
  expect do
456
532
  run('--help', 'does_not_exist')
457
- end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
533
+ end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
458
534
  end
459
535
  end
460
536
 
@@ -462,7 +538,7 @@ describe Commander do
462
538
  it 'should output an invalid option message' do
463
539
  expect do
464
540
  run('--help', 'test', '--invalid-option')
465
- end.to raise_error(SystemExit, /invalid option: --invalid-option/)
541
+ end.to raise_error(TestSystemExit, /invalid option: --invalid-option/)
466
542
  end
467
543
  end
468
544
 
@@ -495,6 +571,13 @@ describe Commander do
495
571
  expect(command_runner.command_name_from_args).to eq('test')
496
572
  end
497
573
 
574
+ it 'should locate command when provided after a global argument with value' do
575
+ new_command_runner '--global-option', 'option-value', 'test' do
576
+ global_option('--global-option=GLOBAL', 'A global option')
577
+ end
578
+ expect(command_runner.command_name_from_args).to eq('test')
579
+ end
580
+
498
581
  it 'should support multi-word commands' do
499
582
  new_command_runner '--help', '--arbitrary', 'some', 'long', 'command', 'foo'
500
583
  command('some long command') {}
@@ -643,4 +726,18 @@ describe Commander do
643
726
  end.run!
644
727
  end
645
728
  end
729
+
730
+ describe 'with double dash' do
731
+ it 'should interpret the remainder as arguments' do
732
+ new_command_runner 'foo', '--', '-x' do
733
+ command('foo') do |c|
734
+ c.option '-x', 'Switch'
735
+ c.when_called do |args, options|
736
+ expect(args).to eq(%w(-x))
737
+ expect(options.x).to be_nil
738
+ end
739
+ end
740
+ end.run!
741
+ end
742
+ end
646
743
  end
data/spec/spec_helper.rb CHANGED
@@ -14,13 +14,22 @@ require 'commander'
14
14
  require 'commander/methods'
15
15
 
16
16
  # Mock terminal IO streams so we can spec against them
17
-
18
17
  def mock_terminal
19
18
  @input = StringIO.new
20
19
  @output = StringIO.new
21
20
  HighLine.default_instance = HighLine.new(@input, @output)
22
21
  end
23
22
 
23
+ # Stub Kernel.abort
24
+ TestSystemExit = Class.new(RuntimeError)
25
+ module Commander
26
+ class Runner
27
+ def abort(message)
28
+ fail TestSystemExit, message
29
+ end
30
+ end
31
+ end
32
+
24
33
  # Create test command for usage within several specs
25
34
 
26
35
  def create_test_command
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.7
4
+ version: 4.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-10-22 00:00:00.000000000 Z
12
+ date: 2020-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: highline
@@ -140,7 +140,12 @@ files:
140
140
  homepage: https://github.com/commander-rb/commander
141
141
  licenses:
142
142
  - MIT
143
- metadata: {}
143
+ metadata:
144
+ bug_tracker_uri: https://github.com/commander-rb/commander/issues
145
+ changelog_uri: https://github.com/commander-rb/commander/blob/master/History.rdoc
146
+ documentation_uri: https://www.rubydoc.info/gems/commander/4.5.2
147
+ homepage_uri: https://github.com/commander-rb/commander
148
+ source_code_uri: https://github.com/commander-rb/commander/tree/v4.5.2
144
149
  post_install_message:
145
150
  rdoc_options: []
146
151
  require_paths:
@@ -156,8 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
161
  - !ruby/object:Gem::Version
157
162
  version: '0'
158
163
  requirements: []
159
- rubyforge_project:
160
- rubygems_version: 2.7.4
164
+ rubygems_version: 3.0.6
161
165
  signing_key:
162
166
  specification_version: 4
163
167
  summary: The complete solution for Ruby command-line executables