Skip to content

Commit 481c38a

Browse files
committed
fix 'thor_script subcommand [args] --help'
Currently, doing so leads to [~/dev/flat_fish]% autoproj test --help Usage: autoproj help [COMMAND] instead of the expected result of "autoproj help test" This case is handled internally by thor by transforming the subcommand invocation into help *ARGV There were two problems: - first invoke was called with (klass, args, options, parameters) while invoke expects (klass, subcommand, args, options, parameters) - the --help argument was kept, which ends up as help *ARGV --help i.e. "give me the help of the 'help' subcommand The commit removes the --help option before calling the subcommand, and fixes the call signature. It only does so for the "massaged" 'help' subcommand because invoke() has a way-too-weird call semantic for me to change the call in the general case without fearing to break something else.
1 parent f0c2166 commit 481c38a

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, Hash[: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)