Class: Thor::Argument

Inherits:
Object
  • Object
show all
Includes:
NRSER::Log::Mixin
Defined in:
lib/thor/parser/argument.rb

Direct Known Subclasses

Option

Constant Summary collapse

VALID_TYPES =

Constants

[:numeric, :hash, :array, :string]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Argument

Returns a new instance of Argument.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

Instance Attribute Details

Attributes



19
20
21
# File 'lib/thor/parser/argument.rb', line 19

def banner
  @banner
end

#completenil, ... (readonly)

Optional block to provide dynamic shell completion.

Returns:

  • (nil)

    This argument does not provide dynamic completion.

  • (Proc<() => Array<String>>)

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

  • (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.)

    Proc<(request:, klass:, command:) => Array<String>] Arity ‘-1` proc that will be passed:

    As in the arity ‘0` case, must return an array of string completion options.



46
47
48
# File 'lib/thor/parser/argument.rb', line 46

def complete
  @complete
end

#defaultObject (readonly)

Attributes



19
20
21
# File 'lib/thor/parser/argument.rb', line 19

def default
  @default
end

#descriptionObject (readonly)

Attributes



19
20
21
# File 'lib/thor/parser/argument.rb', line 19

def description
  @description
end

#enumObject (readonly)

Attributes



19
20
21
# File 'lib/thor/parser/argument.rb', line 19

def enum
  @enum
end

#nameObject (readonly) Also known as: human_name

Attributes



19
20
21
# File 'lib/thor/parser/argument.rb', line 19

def name
  @name
end

#requiredObject (readonly)

Attributes



19
20
21
# File 'lib/thor/parser/argument.rb', line 19

def required
  @required
end

#typeObject (readonly)

Attributes



19
20
21
# File 'lib/thor/parser/argument.rb', line 19

def type
  @type
end

Instance Method Details

#default_bannerObject (protected)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

#required?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/thor/parser/argument.rb', line 82

def required?
  required
end

#show_default?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
# File 'lib/thor/parser/argument.rb', line 86

def show_default?
  case default
  when Array, String, Hash
    !default.empty?
  else
    default
  end
end

#usageObject



78
79
80
# File 'lib/thor/parser/argument.rb', line 78

def usage
  required? ? banner : "[#{banner}]"
end

#valid_type?(type) ⇒ Boolean (protected)

Returns:

  • (Boolean)


109
110
111
# File 'lib/thor/parser/argument.rb', line 109

def valid_type?(type)
  self.class::VALID_TYPES.include?(type.to_sym)
end

#validate!Object (protected)



97
98
99
100
101
102
103
104
105
106
107
# 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