Class: Thor::Command
- Inherits:
-
Struct
- Object
- Struct
- Thor::Command
- Defined in:
- lib/thor/command.rb
Direct Known Subclasses
Constant Summary collapse
- FILE_REGEXP =
/^#{Regexp.escape(File.dirname(__FILE__))}/
Instance Attribute Summary collapse
-
#ancestor_name ⇒ Object
Returns the value of attribute ancestor_name.
-
#description ⇒ Object
Returns the value of attribute description.
-
#long_description ⇒ Object
Returns the value of attribute long_description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#options_relation ⇒ Object
Returns the value of attribute options_relation.
-
#requirements ⇒ Object
Returns the value of attribute requirements.
-
#usage ⇒ Object
Returns the value of attribute usage.
-
#wrap_long_description ⇒ Object
Returns the value of attribute wrap_long_description.
Instance Method Summary collapse
-
#accepts_arguments?(instance, args) ⇒ Boolean
Whether the command method on
instanceaccepts the given positional arguments. -
#formatted_usage(klass, namespace = true, subcommand = false) ⇒ Object
Returns the formatted usage by injecting given required arguments and required options into the given usage.
- #hidden? ⇒ Boolean
-
#initialize(name, description, long_description, wrap_long_description, usage, options = nil, options_relation = nil, requirements = nil) ⇒ Command
constructor
A new instance of Command.
-
#initialize_copy(other) ⇒ Object
:nodoc:.
-
#method_at_least_one_option_names ⇒ Object
:nodoc:.
-
#method_exclusive_option_names ⇒ Object
:nodoc:.
-
#run(instance, args = []) ⇒ Object
By default, a command invokes a method in the thor class.
Constructor Details
#initialize(name, description, long_description, wrap_long_description, usage, options = nil, options_relation = nil, requirements = nil) ⇒ Command
Returns a new instance of Command.
5 6 7 |
# File 'lib/thor/command.rb', line 5 def initialize(name, description, long_description, wrap_long_description, usage, = nil, = nil, requirements = nil) super(name.to_s, description, long_description, wrap_long_description, usage, || {}, || {}, nil, requirements || []) end |
Instance Attribute Details
#ancestor_name ⇒ Object
Returns the value of attribute ancestor_name
2 3 4 |
# File 'lib/thor/command.rb', line 2 def ancestor_name @ancestor_name end |
#description ⇒ Object
Returns the value of attribute description
2 3 4 |
# File 'lib/thor/command.rb', line 2 def description @description end |
#long_description ⇒ Object
Returns the value of attribute long_description
2 3 4 |
# File 'lib/thor/command.rb', line 2 def long_description @long_description end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/thor/command.rb', line 2 def name @name end |
#options ⇒ Object
Returns the value of attribute options
2 3 4 |
# File 'lib/thor/command.rb', line 2 def @options end |
#options_relation ⇒ Object
Returns the value of attribute options_relation
2 3 4 |
# File 'lib/thor/command.rb', line 2 def @options_relation end |
#requirements ⇒ Object
Returns the value of attribute requirements
2 3 4 |
# File 'lib/thor/command.rb', line 2 def requirements @requirements end |
#usage ⇒ Object
Returns the value of attribute usage
2 3 4 |
# File 'lib/thor/command.rb', line 2 def usage @usage end |
#wrap_long_description ⇒ Object
Returns the value of attribute wrap_long_description
2 3 4 |
# File 'lib/thor/command.rb', line 2 def wrap_long_description @wrap_long_description end |
Instance Method Details
#accepts_arguments?(instance, args) ⇒ Boolean
Whether the command method on instance accepts the given
positional arguments. Commands without a public method of their own
(method_missing) are not checked.
44 45 46 47 48 49 50 51 52 |
# File 'lib/thor/command.rb', line 44 def accepts_arguments?(instance, args) #:nodoc: return true unless public_method?(instance) parameters = instance.method(name).parameters required = parameters.count { |type, _| type == :req } optional = parameters.count { |type, _| type == :opt } rest = parameters.any? { |type, _| type == :rest } args.size >= required && (rest || args.size <= required + optional) end |
#formatted_usage(klass, namespace = true, subcommand = false) ⇒ Object
Returns the formatted usage by injecting given required arguments and required options into the given usage.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/thor/command.rb', line 56 def formatted_usage(klass, namespace = true, subcommand = false) if ancestor_name formatted = "#{ancestor_name} ".dup # add space elsif namespace namespace = klass.namespace formatted = "#{namespace.gsub(/^(default)/, '')}:".dup end formatted ||= "#{klass.namespace.split(':').last} ".dup if subcommand formatted ||= "".dup Array(usage).map do |specific_usage| formatted_specific_usage = formatted formatted_specific_usage += required_arguments_for(klass, specific_usage) # Add required options formatted_specific_usage += " #{}" # Strip and go! formatted_specific_usage.strip end.join("\n") end |
#hidden? ⇒ Boolean
16 17 18 |
# File 'lib/thor/command.rb', line 16 def hidden? false end |
#initialize_copy(other) ⇒ Object
:nodoc:
9 10 11 12 13 14 |
# File 'lib/thor/command.rb', line 9 def initialize_copy(other) #:nodoc: super(other) self. = other..dup if other. self. = other..dup if other. self.requirements = other.requirements.dup if other.requirements end |
#method_at_least_one_option_names ⇒ Object
:nodoc:
84 85 86 |
# File 'lib/thor/command.rb', line 84 def method_at_least_one_option_names #:nodoc: self.[:at_least_one_option_names] || [] end |
#method_exclusive_option_names ⇒ Object
:nodoc:
80 81 82 |
# File 'lib/thor/command.rb', line 80 def method_exclusive_option_names #:nodoc: self.[:exclusive_option_names] || [] end |
#run(instance, args = []) ⇒ Object
By default, a command invokes a method in the thor class. You can change this implementation to create custom commands.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/thor/command.rb', line 22 def run(instance, args = []) arity = nil if private_method?(instance) instance.class.handle_no_command_error(name) elsif public_method?(instance) arity = instance.method(name).arity instance.__send__(name, *args) elsif local_method?(instance, :method_missing) instance.__send__(:method_missing, name.to_sym, *args) else instance.class.handle_no_command_error(name) end rescue ArgumentError => e handle_argument_error?(instance, e, caller) ? instance.class.handle_argument_error(self, e, args, arity) : (raise e) rescue NoMethodError => e handle_no_method_error?(instance, e, caller) ? instance.class.handle_no_command_error(name) : (raise e) end |