Skip to content

Commit

Permalink
Merge pull request #95 from orien/help-after-command-argument
Browse files Browse the repository at this point in the history
Use --help after command and argument
  • Loading branch information
ggilder committed Mar 15, 2020
2 parents b8abd42 + ea4d912 commit 83c255e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/commander/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,18 @@ def active_command
##
# Attempts to locate a command name from within the arguments.
# Supports multi-word commands, using the largest possible match.
# Returns the default command, if no valid commands found in the args.

def command_name_from_args
@__command_name_from_args ||= (valid_command_names_from(*@args.dup).sort.last || @default_command)
@__command_name_from_args ||= (longest_valid_command_name_from(@args) || @default_command)
end

##
# Attempts to locate a command name from within the provided arguments.
# Supports multi-word commands, using the largest possible match.

private def longest_valid_command_name_from(args)
valid_command_names_from(*args.dup).max
end

##
Expand Down Expand Up @@ -304,7 +313,7 @@ def create_default_commands
if args.empty?
say help_formatter.render
else
command = command args.join(' ')
command = command(longest_valid_command_name_from(args))
begin
require_valid_command command
rescue InvalidCommandError => e
Expand Down
16 changes: 16 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,22 @@
expect(run('test', '--help')).to eq("Implement help for test here\n")
end

it 'can be used after the command and command arguments' do
expect(run('test', 'command-arg', '--help')).to eq("Implement help for test here\n")
end

it 'can be used before a single-word command with command arguments' do
expect(run('help', 'test', 'command-arg')).to eq("Implement help for test here\n")
end

it 'can be used before a multi-word command with command arguments' do
expect(
run('help', 'module', 'install', 'command-arg') do
command('module install') { |c| c.when_called { say 'whee!' } }
end
).to eq("Implement help for module install here\n")
end

describe 'help_paging program information' do
it 'enables paging when enabled' do
run('--help') { program :help_paging, true }
Expand Down

0 comments on commit 83c255e

Please sign in to comment.