Skip to content

Commit

Permalink
Fix command name identification
Browse files Browse the repository at this point in the history
Specifically, in the case where a global option value is provided before
the command name.
  • Loading branch information
orien committed Mar 8, 2020
1 parent adaa197 commit ecc2b8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/commander/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def command_name_from_args
# Returns array of valid command names found within _args_.

def valid_command_names_from(*args)
remove_global_options options, args
arg_string = args.delete_if { |value| value =~ /^-/ }.join ' '
commands.keys.find_all { |name| name if arg_string =~ /^#{name}\b/ }
end
Expand Down
18 changes: 18 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@
expect(quiet).to be true
end

it 'should be inherited by commands when provided before the command name' do
option = nil
new_command_runner '--global-option', 'option-value', 'command_name' do
global_option('--global-option=GLOBAL', 'A global option')
command :command_name do |c|
c.when_called { |_, options| option = options.global_option }
end
end.run!
expect(option).to eq('option-value')
end

it 'should be inherited by commands even when a block is present' do
quiet = nil
new_command_runner 'foo', '--quiet' do
Expand Down Expand Up @@ -495,6 +506,13 @@
expect(command_runner.command_name_from_args).to eq('test')
end

it 'should locate command when provided after a global argument with value' do
new_command_runner '--global-option', 'option-value', 'test' do
global_option('--global-option=GLOBAL', 'A global option')
end
expect(command_runner.command_name_from_args).to eq('test')
end

it 'should support multi-word commands' do
new_command_runner '--help', '--arbitrary', 'some', 'long', 'command', 'foo'
command('some long command') {}
Expand Down

0 comments on commit ecc2b8d

Please sign in to comment.