Module: Thor::Base::ArgumentsConcern
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/thor/base/arguments_concern.rb
Class-Level Argument Class Methods collapse
-
.class_argument(name, **options) ⇒ Object
Adds an argument to the class and creates an attr_accessor for it.
-
.class_arguments ⇒ Array<Thor::Argument>
Returns this class' class-level arguments, looking up in the ancestors chain.
-
.remove_class_argument(*names, undefine: false) ⇒ Object
Removes a previous defined class-level argument.
Method-Specific Argument Class Methods collapse
-
.method_argument(name, **options) ⇒ Object
(also: argument, arg)
Adds an argument to the class and creates an attr_accessor for it.
-
.method_arguments ⇒ Array<Thor::Argument>
Returns this class' class-level arguments, looking up in the ancestors chain.
-
.remove_method_argument(*names, undefine: false) ⇒ Object
Removes a previous defined class-level argument.
Class Method Summary collapse
-
.arguments(command: nil) ⇒ Object
==========================================================================.
-
.build_argument(name, options, scope) ⇒ Object
protected
========================================================================.
- .remove_argument_from(*names, scope:, undefine: false) ⇒ Object protected
Class Method Details
.arguments(command: nil) ⇒ Object
==========================================================================
49 50 51 |
# File 'lib/thor/base/arguments_concern.rb', line 49 def arguments command: nil [*class_arguments, *command&.arguments] end |
.build_argument(name, options, scope) ⇒ Object (protected)
========================================================================
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/thor/base/arguments_concern.rb', line 57 def build_argument name, , scope name = name.to_s is_thor_reserved_word? name, :argument no_commands { attr_accessor name } required = if .key?(:optional) ![:optional] elsif .key?(:required) [:required] else # If neither `:required` or `:optional` options were provided, # default to the argument being required if no `:default` was provided. [:default].nil? end scope.delete_if { |argument| argument.name == name } if required scope.each do |argument| next if argument.required? raise ArgumentError, "You cannot have #{ name.inspect } as required argument " \ "after the non-required argument #{ argument.human_name.inspect }." end end [:required] = required scope << Thor::Argument.new( name, ) end |
.class_argument(name, **options) ⇒ Object
This used to just be called .argument, and apparently arguments
were class-level only. I don't know why.
Atli switches them to mirror options, with class and command levels.
Adds an argument to the class and creates an attr_accessor for it.
Arguments are different from options in several aspects. The first one is how they are parsed from the command line, arguments are retrieved from position:
thor command NAME
Instead of:
thor command --name=NAME
Besides, arguments are used inside your code as an accessor (self.argument), while options are all kept in a hash (self.options).
Finally, arguments cannot have type :default or :boolean but can be optional (supplying :optional => :true or :required => false), although you cannot have a required argument after a non-required argument. If you try it, an error is raised.
168 169 170 |
# File 'lib/thor/base/arguments_concern.rb', line 168 def class_argument name, ** build_argument name, , class_arguments end |
.class_arguments ⇒ Array<Thor::Argument>
Returns this class' class-level arguments, looking up in the ancestors chain.
108 109 110 |
# File 'lib/thor/base/arguments_concern.rb', line 108 def class_arguments @class_arguments ||= from_superclass( :class_arguments, [] ) end |
.method_argument(name, **options) ⇒ Object Also known as: argument, arg
This used to just be called .argument, and apparently arguments
were class-level only. I don't know why.
Atli switches them to mirror options, with class and command levels.
Adds an argument to the class and creates an attr_accessor for it.
Arguments are different from options in several aspects. The first one is how they are parsed from the command line, arguments are retrieved from position:
thor command NAME
Instead of:
thor command --name=NAME
Besides, arguments are used inside your code as an accessor (self.argument), while options are all kept in a hash (self.options).
Finally, arguments cannot have type :default or :boolean but can be optional (supplying :optional => :true or :required => false), although you cannot have a required argument after a non-required argument. If you try it, an error is raised.
263 264 265 |
# File 'lib/thor/base/arguments_concern.rb', line 263 def method_argument name, ** build_argument( name, , method_arguments ) end |
.method_arguments ⇒ Array<Thor::Argument>
Returns this class' class-level arguments, looking up in the ancestors chain.
203 204 205 |
# File 'lib/thor/base/arguments_concern.rb', line 203 def method_arguments @method_arguments ||= [] end |
.remove_argument_from(*names, scope:, undefine: false) ⇒ Object (protected)
90 91 92 93 94 95 |
# File 'lib/thor/base/arguments_concern.rb', line 90 def remove_argument_from *names, scope:, undefine: false names.each do |name| scope.delete_if { |a| a.name == name.to_s } undef_method name, "#{name}=" if undefine end end |
.remove_class_argument(*names, undefine: false) ⇒ Object
Removes a previous defined class-level argument. If :undefine option is
given, un-defines accessors as well.
188 189 190 |
# File 'lib/thor/base/arguments_concern.rb', line 188 def remove_class_argument *names, undefine: false remove_argument_from *names, scope: class_arguments, undefine: undefine end |
.remove_method_argument(*names, undefine: false) ⇒ Object
Removes a previous defined class-level argument. If :undefine option is
given, un-defines accessors as well.
284 285 286 |
# File 'lib/thor/base/arguments_concern.rb', line 284 def remove_method_argument *names, undefine: false remove_argument_from *names, scope: method_arguments, undefine: undefine end |