Class: Thor::Argument
- Inherits:
-
Object
- Object
- Thor::Argument
- Includes:
- NRSER::Log::Mixin
- Defined in:
- lib/thor/parser/argument.rb
Direct Known Subclasses
Constant Summary collapse
- VALID_TYPES =
Constants
[:numeric, :hash, :array, :string]
Instance Attribute Summary collapse
-
#banner ⇒ Object
readonly
Attributes ========================================================================.
-
#complete ⇒ nil, ...
readonly
Optional block to provide dynamic shell completion.
-
#default ⇒ Object
readonly
Attributes ========================================================================.
-
#description ⇒ Object
readonly
Attributes ========================================================================.
-
#enum ⇒ Object
readonly
Attributes ========================================================================.
-
#name ⇒ Object
(also: #human_name)
readonly
Attributes ========================================================================.
-
#required ⇒ Object
readonly
Attributes ========================================================================.
-
#type ⇒ Object
readonly
Attributes ========================================================================.
Instance Method Summary collapse
- #default_banner ⇒ Object protected
-
#initialize(name, options = {}) ⇒ Argument
constructor
A new instance of Argument.
- #required? ⇒ Boolean
- #show_default? ⇒ Boolean
- #usage ⇒ Object
- #valid_type?(type) ⇒ Boolean protected
- #validate! ⇒ Object protected
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, = {}) class_name = self.class.name.split("::").last type = [: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 = [:desc] @required = .key?(:required) ? [:required] : true @type = (type || :string).to_sym @default = [:default] = [:banner] || @enum = [:enum] @complete = [:complete] validate! # Trigger specific validations end |
Instance Attribute Details
#banner ⇒ Object (readonly)
Attributes
19 20 21 |
# File 'lib/thor/parser/argument.rb', line 19 def end |
#complete ⇒ nil, ... (readonly)
Optional block to provide dynamic shell completion.
46 47 48 |
# File 'lib/thor/parser/argument.rb', line 46 def complete @complete end |
#default ⇒ Object (readonly)
Attributes
19 20 21 |
# File 'lib/thor/parser/argument.rb', line 19 def default @default end |
#description ⇒ Object (readonly)
Attributes
19 20 21 |
# File 'lib/thor/parser/argument.rb', line 19 def description @description end |
#enum ⇒ Object (readonly)
Attributes
19 20 21 |
# File 'lib/thor/parser/argument.rb', line 19 def enum @enum end |
#name ⇒ Object (readonly) Also known as: human_name
Attributes
19 20 21 |
# File 'lib/thor/parser/argument.rb', line 19 def name @name end |
#required ⇒ Object (readonly)
Attributes
19 20 21 |
# File 'lib/thor/parser/argument.rb', line 19 def required @required end |
#type ⇒ Object (readonly)
Attributes
19 20 21 |
# File 'lib/thor/parser/argument.rb', line 19 def type @type end |
Instance Method Details
#default_banner ⇒ Object (protected)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/thor/parser/argument.rb', line 113 def 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
82 83 84 |
# File 'lib/thor/parser/argument.rb', line 82 def required? required end |
#show_default? ⇒ 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 |
#usage ⇒ Object
78 79 80 |
# File 'lib/thor/parser/argument.rb', line 78 def usage required? ? : "[#{banner}]" end |
#valid_type?(type) ⇒ Boolean (protected)
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 |