Class: Thor::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/thor/parser/argument.rb

Overview

:nodoc:

Direct Known Subclasses

Option

Constant Summary

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Argument



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/thor/parser/argument.rb', line 8

def initialize(name, options = {})
  class_name = self.class.name.split("::").last

  type = options[:type]

  fail ArgumentError, "#{class_name} name can't be nil."                         if name.nil?
  fail ArgumentError, "Type :#{type} is not valid for #{class_name.downcase}s."  if type && !valid_type?(type)

  @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]

  validate! # Trigger specific validations
end

Instance Attribute Details

Returns the value of attribute banner



5
6
7
# File 'lib/thor/parser/argument.rb', line 5

def banner
  @banner
end

#defaultObject (readonly)

Returns the value of attribute default



5
6
7
# File 'lib/thor/parser/argument.rb', line 5

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description



5
6
7
# File 'lib/thor/parser/argument.rb', line 5

def description
  @description
end

#enumObject (readonly)

Returns the value of attribute enum



5
6
7
# File 'lib/thor/parser/argument.rb', line 5

def enum
  @enum
end

#nameObject (readonly) Also known as: human_name

Returns the value of attribute name



5
6
7
# File 'lib/thor/parser/argument.rb', line 5

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required



5
6
7
# File 'lib/thor/parser/argument.rb', line 5

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type



5
6
7
# File 'lib/thor/parser/argument.rb', line 5

def type
  @type
end

Instance Method Details

#required?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/thor/parser/argument.rb', line 31

def required?
  required
end

#show_default?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'lib/thor/parser/argument.rb', line 35

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

#usageObject



27
28
29
# File 'lib/thor/parser/argument.rb', line 27

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