Skip to content

Commit 81778d4

Browse files
committed
Merge pull request #500 from doudou/fix_subcommand_minus_minus_help
fix 'thor_script subcommand [args] --help' 🌈
2 parents 2389868 + 481c38a commit 81778d4

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/thor.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,11 @@ def subcommand(subcommand, subcommand_class)
235235

236236
define_method(subcommand) do |*args|
237237
args, opts = Thor::Arguments.split(args)
238-
args.unshift("help") if opts.include? "--help" or opts.include? "-h"
239-
invoke subcommand_class, args, opts, :invoked_via_subcommand => true, :class_options => options
238+
invoke_args = [args, opts, { :invoked_via_subcommand => true, :class_options => options }]
239+
if opts.delete('--help') or opts.delete("-h")
240+
invoke_args.unshift 'help'
241+
end
242+
invoke subcommand_class, *invoke_args
240243
end
241244
end
242245
alias_method :subtask, :subcommand

spec/thor_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,18 @@ def bar
441441
end
442442
end
443443

444+
describe "subcommands" do
445+
it "triggers a subcommand help when passed --help" do
446+
parent = Class.new(Thor)
447+
child = Class.new(Thor)
448+
parent.desc 'child', 'child subcommand'
449+
parent.subcommand 'child', child
450+
parent.desc 'dummy', 'dummy'
451+
expect(child).to receive(:help).with(anything, anything)
452+
parent.start ['child', '--help']
453+
end
454+
end
455+
444456
describe "when creating commands" do
445457
it "prints a warning if a public method is created without description or usage" do
446458
expect(capture(:stdout) do

0 commit comments

Comments
 (0)