Skip to content

Commit

Permalink
Remove global options provided in the option=value form
Browse files Browse the repository at this point in the history
  • Loading branch information
orien committed Mar 11, 2020
1 parent 7005a1b commit 2a7b07b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/commander/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,28 @@ def remove_global_options(options, args)
options.each do |option|
switches = option[:switches]
next if switches.empty?
switch_has_arg = switches.any? { |s| s =~ /[ =]/ }
option_takes_argument = switches.any? { |s| s =~ /[ =]/ }
switches = expand_optionally_negative_switches(switches)

past_switch, arg_removed = false, false
args.delete_if do |arg|
break if arg == '--'
if switches.any? { |s| s[0, arg.length] == arg }
arg_removed = !switch_has_arg
past_switch = true
elsif past_switch && !arg_removed && arg !~ /^-/
arg_removed = true
option_argument_needs_removal = false
args.delete_if do |token|
break if token == '--'

# Use just the portion of the token before the = when
# comparing switches.
index_of_equals = token.index('=') if option_takes_argument
token = token[0, index_of_equals] if index_of_equals
token_contains_option_argument = !index_of_equals.nil?

if switches.any? { |s| s[0, token.length] == token }
option_argument_needs_removal =
option_takes_argument && !token_contains_option_argument
true
elsif option_argument_needs_removal && token !~ /^-/
option_argument_needs_removal = false
true
else
arg_removed = true
option_argument_needs_removal = false
false
end
end
Expand Down
31 changes: 31 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@
expect(config).to eq('config-value')
expect(args).to eq(%w(arg1 arg2))
end

it 'allows global options in the form option=value' do
config = nil
args = nil
new_command_runner 'test', 'arg1', '--config=config-value', 'arg2' do
global_option('-c', '--config CONFIG', String)
command :test do |c|
c.when_called do |arguments, options|
options.default(config: 'default')
args = arguments
config = options.config
end
end
end.run!
expect(config).to eq('config-value')
expect(args).to eq(%w[arg1 arg2])
end
end

describe '#parse_global_options' do
Expand Down Expand Up @@ -371,6 +388,20 @@
command_runner.remove_global_options options, args
expect(args).to eq(%w(--versionCode something))
end

it 'should remove specified switches value provided via equals' do
options = [{ switches: ['--global GLOBAL'] }]
args = ['--command', '--global=option-value', 'arg']
command_runner.remove_global_options options, args
expect(args).to eq(['--command', 'arg'])
end

it 'should not remove extra values after switches' do
options = [{ switches: ['--global GLOBAL'] }]
args = ['--global', '--command', 'arg']
command_runner.remove_global_options options, args
expect(args).to eq(['--command', 'arg'])
end
end

describe '--trace' do
Expand Down

0 comments on commit 2a7b07b

Please sign in to comment.