Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use --help after command and argument #95

Merged
merged 1 commit into from
Mar 15, 2020
Merged

Use --help after command and argument #95

merged 1 commit into from
Mar 15, 2020

Conversation

orien
Copy link
Contributor

@orien orien commented Mar 14, 2020

Context

It is reported in #94 that using --help after a command name and command argument does not work.

$ mycli command-name command-arg --help
invalid command. Use --help for more information

Investigation finds the help command is assuming the the command name is all the arguments joined together.

command = command args.join(' ')

Change

Fix #94 by changing the line in question to ignore the command argument when searching for command name. Just take the longest valid command name found in the arguments.

Considerations

This also changes the behaviour of the help command in its alternate form. This will ignore the argument and provide the documentation for the specified command, where previously it would give an error message.

mycli help command-name command-arg

lib/commander/runner.rb Outdated Show resolved Hide resolved
# Supports multi-word commands, using the largest possible match.

private def longest_valid_command_name_from(args)
valid_command_names_from(*args.dup).max
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract a new method to keep things DRY.

I've made a small optimisation change here. I predict max will be faster than stort.last as it'll get the same result without creating a new Array.

- valid_command_names_from(*args.dup).sort.last
+ valid_command_names_from(*args.dup).max

Copy link
Member

@ggilder ggilder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I think with a couple additional tests this will be ready to merge.

spec/runner_spec.rb Show resolved Hide resolved
@ggilder ggilder merged commit 83c255e into commander-rb:master Mar 15, 2020
@orien orien deleted the help-after-command-argument branch March 16, 2020 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: --help no longer works anywhere on the command line
2 participants