Skip to content

Commit 7611ef2

Browse files
committed
Merge pull request #462 from b-dean/disable-class-options
Add ability to disable class options for a command
2 parents 42817ca + eda9080 commit 7611ef2

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-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:

spec/thor_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,23 @@ def shell
422422
expect(capture(:stdout) { MyScript.start(%w[help foo]) }).to match(/Usage:/)
423423
end
424424
end
425+
426+
context "with required class_options" do
427+
let(:klass) do
428+
Class.new(Thor) do
429+
class_option :foo, :required => true
430+
431+
desc "bar", "do something"
432+
def bar
433+
end
434+
end
435+
end
436+
437+
it "shows the command help" do
438+
content = capture(:stdout) { klass.start(%w{help}) }
439+
expect(content).to match(/Commands:/)
440+
end
441+
end
425442
end
426443

427444
describe "when creating commands" do

0 commit comments

Comments
 (0)