Skip to content

Commit b23e2cf

Browse files
alias --help to help on subcommands
When the --help option is present it will change the args array to include the help command.
1 parent aaeccb6 commit b23e2cf

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/thor.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def subcommand(subcommand, subcommand_class)
229229

230230
define_method(subcommand) do |*args|
231231
args, opts = Thor::Arguments.split(args)
232+
args.unshift("help") if opts.include? "--help"
232233
invoke subcommand_class, args, opts, :invoked_via_subcommand => true, :class_options => options
233234
end
234235
end
@@ -353,6 +354,7 @@ def dispatch(meth, given_args, given_opts, config) #:nodoc:
353354
command = Thor::DynamicCommand.new(meth)
354355
end
355356

357+
356358
opts = given_opts || opts || []
357359
config.merge!(:current_command => command, :command_options => command.options)
358360

spec/subcommand_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
output = capture(:stdout) { TestSubcommands::Parent.start(%w[sub print_opt --opt output]) }
2626
expect(output).to eq("output")
2727
end
28+
29+
it "accepts the help switch and calls the help command on the subcommand" do
30+
output = capture(:stdout) { TestSubcommands::Parent.start(%w[sub print_opt --help])}
31+
sub_help = capture(:stdout) { TestSubcommands::Parent.start(%w[sub help print_opt])}
32+
expect(output).to eq(sub_help)
33+
end
34+
2835
end
2936

3037
end

0 commit comments

Comments
 (0)