class Thor::Argument

Constants

VALID_TYPES

Constants

Attributes

banner[R]

Attributes

complete[R]

Optional block to provide dynamic shell completion.

@return [nil]

This argument does not provide dynamic completion.

@return [Proc<() => Array<String>>]

Arity `0` proc that will be called with no arguments to provide 
dynamic completion options.

@return [Proc<(request:, klass:, command:) => Array<String>]

Arity `-1` proc that will be passed:

-   {Thor::Completion::Bash::Request} `request:`
    The completion request.

-   {Class<Thor::Base>} `klass:`
    The {Thor} or {Thor::Group} subclass being completed.

-   {Thor::Command} `command:`
    The command being completed.

As in the arity `0` case, must return an array of string completion
options.
default[R]

Attributes

description[R]

Attributes

enum[R]

Attributes

human_name[R]

Attributes

name[R]

Attributes

required[R]

Attributes

type[R]

Attributes

Public Class Methods

new(name, options = {}) click to toggle source
# File lib/thor/parser/argument.rb, line 51
def initialize(name, options = {})
  class_name = self.class.name.split("::").last

  type = options[:type]

  if name.nil?
    raise ArgumentError,
      "#{class_name} name can't be nil."
  end

  if type && !valid_type?(type)
    raise ArgumentError,
      "Type :#{type} is not valid for #{class_name.downcase}s."
  end

  @name         = name.to_s
  @description  = options[:desc]
  @required     = options.key?(:required) ? options[:required] : true
  @type         = (type || :string).to_sym
  @default      = options[:default]
  @banner       = options[:banner] || default_banner
  @enum         = options[:enum]
  @complete     = options[:complete]

  validate! # Trigger specific validations
end

Public Instance Methods

required?() click to toggle source
# File lib/thor/parser/argument.rb, line 82
def required?
  required
end
show_default?() click to toggle source
# File lib/thor/parser/argument.rb, line 86
def show_default?
  case default
  when Array, String, Hash
    !default.empty?
  else
    default
  end
end
usage() click to toggle source
# File lib/thor/parser/argument.rb, line 78
def usage
  required? ? banner : "[#{banner}]"
end

Protected Instance Methods

default_banner() click to toggle source
# File lib/thor/parser/argument.rb, line 113
def default_banner
  case type
  when :boolean
    nil
  when :string, :default
    human_name.upcase
  when :numeric
    "N"
  when :hash
    "key:value"
  when :array
    "one two three"
  end
end
valid_type?(type) click to toggle source
# File lib/thor/parser/argument.rb, line 109
def valid_type?(type)
  self.class::VALID_TYPES.include?(type.to_sym)
end
validate!() click to toggle source
# File lib/thor/parser/argument.rb, line 97
def validate!
  if required? && !default.nil?
    raise ArgumentError,
      "An argument cannot be required and have default value."
  end
  
  if @enum && !@enum.is_a?(Array)
    raise ArgumentError,
      "An argument cannot have an enum other than an array."
  end
end