Skip to content

Commit

Permalink
Merge pull request #248 from opus-codium/thor-help
Browse files Browse the repository at this point in the history
CLI: Show relevant help when using --help option on a subcommand
  • Loading branch information
ekohl authored Feb 22, 2022
2 parents 18f1809 + 7f531a1 commit 5aeacc6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ Feature: CLI
When I successfully run `msync help`
Then the output should match /Commands:/

Scenario: Use --help options on subcommand should show subcommand help
When I successfully run `msync clone --help`
Then the output should contain:
"""
Usage:
msync clone
"""

Scenario: When overriding a setting from the config file on the command line
Given a puppet module "puppet-test" from "fakenamespace"
And a file named "managed_modules.yml" with:
Expand Down
12 changes: 12 additions & 0 deletions lib/modulesync/cli/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ module CLI
# This class extends ::Thor class to
# - exit with status code sets to `1` on Thor failure (e.g. missing required option)
# - exit with status code sets to `1` when user calls `msync` (or a subcommand) without required arguments
# - show subcommands help using `msync subcommand --help`
class Thor < ::Thor
def self.start(*args)
if (Thor::HELP_MAPPINGS & ARGV).any? && subcommands.none? { |command| command.start_with?(ARGV[0]) }
Thor::HELP_MAPPINGS.each do |cmd|
if (match = ARGV.delete(cmd))
ARGV.unshift match
end
end
end
super
end

desc '_invalid_command_call', 'Invalid command', hide: true
def _invalid_command_call
self.class.new.help
Expand Down

0 comments on commit 5aeacc6

Please sign in to comment.