Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to disable class options for a command #462

Merged
merged 2 commits into from Jul 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/thor.rb
Expand Up @@ -156,6 +156,10 @@ def method_option(name, options = {})
end
alias_method :option, :method_option

def disable_class_options
@disable_class_options = true
end

# Prints help information for the given command.
#
# ==== Parameters
Expand Down Expand Up @@ -380,11 +384,12 @@ def create_command(meth) #:nodoc:
@usage ||= nil
@desc ||= nil
@long_desc ||= nil
@disable_class_options ||= nil

if @usage && @desc
base_class = @hide ? Thor::HiddenCommand : Thor::Command
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
@usage, @desc, @long_desc, @method_options, @hide = nil
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options, @disable_class_options)
@usage, @desc, @long_desc, @method_options, @hide, @disable_class_options = nil
true
elsif all_commands[meth] || meth == "method_missing"
true
Expand Down Expand Up @@ -470,6 +475,7 @@ def help(command = nil, subcommand = true); super; end
map HELP_MAPPINGS => :help

desc "help [COMMAND]", "Describe available commands or one specific command"
disable_class_options
def help(command = nil, subcommand = false)
if command
if self.class.subcommands.include? command
Expand Down
2 changes: 1 addition & 1 deletion lib/thor/base.rb
Expand Up @@ -42,7 +42,7 @@ module Base
# config<Hash>:: Configuration for this Thor class.
#
def initialize(args = [], local_options = {}, config = {}) # rubocop:disable MethodLength
parse_options = self.class.class_options
parse_options = config[:current_command] && config[:current_command].disable_class_options ? {} : self.class.class_options

# The start method splits inbound arguments at the first argument
# that looks like an option (starts with - or --). It then calls
Expand Down
6 changes: 3 additions & 3 deletions lib/thor/command.rb
@@ -1,9 +1,9 @@
class Thor
class Command < Struct.new(:name, :description, :long_description, :usage, :options)
class Command < Struct.new(:name, :description, :long_description, :usage, :options, :disable_class_options)
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/

def initialize(name, description, long_description, usage, options = nil)
super(name.to_s, description, long_description, usage, options || {})
def initialize(name, description, long_description, usage, options = nil, disable_class_options = false)
super(name.to_s, description, long_description, usage, options || {}, disable_class_options)
end

def initialize_copy(other) #:nodoc:
Expand Down
17 changes: 17 additions & 0 deletions spec/thor_spec.rb
Expand Up @@ -422,6 +422,23 @@ def shell
expect(capture(:stdout) { MyScript.start(%w[help foo]) }).to match(/Usage:/)
end
end

context "with required class_options" do
let(:klass) do
Class.new(Thor) do
class_option :foo, :required => true

desc "bar", "do something"
def bar
end
end
end

it "shows the command help" do
content = capture(:stdout) { klass.start(%w{help}) }
expect(content).to match(/Commands:/)
end
end
end

describe "when creating commands" do
Expand Down