diff --git a/lib/thor.rb b/lib/thor.rb index 6880b6d7a..cf8caae35 100644 --- a/lib/thor.rb +++ b/lib/thor.rb @@ -229,6 +229,7 @@ def subcommand(subcommand, subcommand_class) define_method(subcommand) do |*args| args, opts = Thor::Arguments.split(args) + args.unshift("help") if opts.include? "--help" or opts.include? "-h" invoke subcommand_class, args, opts, :invoked_via_subcommand => true, :class_options => options end end diff --git a/spec/subcommand_spec.rb b/spec/subcommand_spec.rb index 721fedb42..aa56fc032 100644 --- a/spec/subcommand_spec.rb +++ b/spec/subcommand_spec.rb @@ -25,6 +25,19 @@ output = capture(:stdout) { TestSubcommands::Parent.start(%w[sub print_opt --opt output]) } expect(output).to eq("output") end + + it "accepts the help switch and calls the help command on the subcommand" do + output = capture(:stdout) { TestSubcommands::Parent.start(%w[sub print_opt --help])} + sub_help = capture(:stdout) { TestSubcommands::Parent.start(%w[sub help print_opt])} + expect(output).to eq(sub_help) + end + + it "accepts the help short switch and calls the help command on the subcommand" do + output = capture(:stdout) { TestSubcommands::Parent.start(%w[sub print_opt -h])} + sub_help = capture(:stdout) { TestSubcommands::Parent.start(%w[sub help print_opt])} + expect(output).to eq(sub_help) + end + end end