module Thor::Base::ArgumentsConcern

@todo document Arguments module.

Public Instance Methods

arguments(command: nil) click to toggle source
# File lib/thor/base/arguments_concern.rb, line 49
def arguments command: nil
  [*class_arguments, *command&.arguments]
end
build_argument(name, options, scope) click to toggle source
# File lib/thor/base/arguments_concern.rb, line 57
def build_argument name, options, scope
  name = name.to_s

  is_thor_reserved_word? name, :argument
  no_commands { attr_accessor name }

  required = if options.key?(:optional)
    !options[:optional]
  elsif options.key?(:required)
    options[:required]
  else
    # If neither `:required` or `:optional` options were provided,
    # default to the argument being required if no `:default` was provided.
    options[: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

  options[:required] = required

  scope << Thor::Argument.new( name, options )
end
class_argument(name, **options) click to toggle source

Adds an argument to the class and creates an attr_accessor for it.

@note

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.

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.

@param [String | Symbol] name

The name of the argument.

@param [Hash] options

@option options [String] :desc

Description for the argument.

@option options [Boolean] :required

If the argument is required or not (opposite of `:optional`).

@option options [Boolean] :optional

If the argument is optional or not (opposite of `:required`).

@option options [:string | :hash | :array | :numeric] :type

The type of the argument.

@option options [Object] :default

Default value for this argument. It cannot be required and
have default values.

@option options [String] :banner

String to show on usage notes.

@return (see class_arguments)

@raise [ArgumentError]

Raised if you supply a required argument after a non-required one.
# File lib/thor/base/arguments_concern.rb, line 168
def class_argument name, **options
  build_argument name, options, class_arguments
end
class_arguments() click to toggle source

Returns this class' class-level arguments, looking up in the ancestors chain.

@return [Array<Thor::Argument>]

# File lib/thor/base/arguments_concern.rb, line 108
def class_arguments
  @class_arguments ||= from_superclass( :class_arguments, [] )
end
method_argument(name, **options) click to toggle source

Adds an argument to the class and creates an attr_accessor for it.

@note

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.

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.

@param [String | Symbol] name

The name of the argument.

@param [Hash] options

@option options [String] :desc

Description for the argument.

@option options [Boolean] :required

If the argument is required or not (opposite of `:optional`).

@option options [Boolean] :optional

If the argument is optional or not (opposite of `:required`).

@option options [:string | :hash | :array | :numeric] :type

The type of the argument.

@option options [Object] :default

Default value for this argument. It cannot be required and
have default values.

@option options [String] :banner

String to show on usage notes.

@return (see class_arguments)

@raise [ArgumentError]

Raised if you supply a required argument after a non-required one.
# File lib/thor/base/arguments_concern.rb, line 263
def method_argument name, **options
  build_argument( name, options, method_arguments )
end
method_arguments() click to toggle source

Returns this class' class-level arguments, looking up in the ancestors chain.

@return [Array<Thor::Argument>]

# File lib/thor/base/arguments_concern.rb, line 203
def method_arguments
  @method_arguments ||= []
end
remove_argument_from(*names, scope:, undefine: false) click to toggle source
# 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) click to toggle source

Removes a previous defined class-level argument. If `:undefine` option is given, un-defines accessors as well.

@param [Array<String | Symbol>] *names

Arguments to be removed

@param [Boolean] undefine:

Un-defines the arguments' setter methods as well.

@example

remove_class_argument :foo

@example

remove_class_argument :foo, :bar, :baz, :undefine => true
# 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) click to toggle source

Removes a previous defined class-level argument. If `:undefine` option is given, un-defines accessors as well.

@param [Array<String | Symbol>] *names

Arguments to be removed

@param [Boolean] undefine:

Un-defines the arguments' setter methods as well.

@example

remove_argument :foo remove_argument :foo, :bar, :baz, :undefine => true
# 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