Skip to content

Commit eda9080

Browse files
committed
allow commands to disable class options
specifically the help command needs to do this so it will always work, even when there are required class options on the class. Fixes #363
1 parent 089c30e commit eda9080

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

lib/thor.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ def method_option(name, options = {})
156156
end
157157
alias_method :option, :method_option
158158

159+
def disable_class_options
160+
@disable_class_options = true
161+
end
162+
159163
# Prints help information for the given command.
160164
#
161165
# ==== Parameters
@@ -380,11 +384,12 @@ def create_command(meth) #:nodoc:
380384
@usage ||= nil
381385
@desc ||= nil
382386
@long_desc ||= nil
387+
@disable_class_options ||= nil
383388

384389
if @usage && @desc
385390
base_class = @hide ? Thor::HiddenCommand : Thor::Command
386-
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
387-
@usage, @desc, @long_desc, @method_options, @hide = nil
391+
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options, @disable_class_options)
392+
@usage, @desc, @long_desc, @method_options, @hide, @disable_class_options = nil
388393
true
389394
elsif all_commands[meth] || meth == "method_missing"
390395
true
@@ -470,6 +475,7 @@ def help(command = nil, subcommand = true); super; end
470475
map HELP_MAPPINGS => :help
471476

472477
desc "help [COMMAND]", "Describe available commands or one specific command"
478+
disable_class_options
473479
def help(command = nil, subcommand = false)
474480
if command
475481
if self.class.subcommands.include? command

lib/thor/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Base
4242
# config<Hash>:: Configuration for this Thor class.
4343
#
4444
def initialize(args = [], local_options = {}, config = {}) # rubocop:disable MethodLength
45-
parse_options = self.class.class_options
45+
parse_options = config[:current_command] && config[:current_command].disable_class_options ? {} : self.class.class_options
4646

4747
# The start method splits inbound arguments at the first argument
4848
# that looks like an option (starts with - or --). It then calls

lib/thor/command.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Thor
2-
class Command < Struct.new(:name, :description, :long_description, :usage, :options)
2+
class Command < Struct.new(:name, :description, :long_description, :usage, :options, :disable_class_options)
33
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
44

5-
def initialize(name, description, long_description, usage, options = nil)
6-
super(name.to_s, description, long_description, usage, options || {})
5+
def initialize(name, description, long_description, usage, options = nil, disable_class_options = false)
6+
super(name.to_s, description, long_description, usage, options || {}, disable_class_options)
77
end
88

99
def initialize_copy(other) #:nodoc:

0 commit comments

Comments
 (0)