Class: UnimatrixParser::Option
- Inherits:
-
Object
- Object
- UnimatrixParser::Option
- Defined in:
- lib/unimatrix_parser.rb
Overview
Option Class
Constant Summary collapse
- FLAG_TYPES =
[ :flag, :bool, :boolean ]
- SINGLE_ARG_TYPES =
[ :int, :integer, :string, :double, :float, :io, :date ]
- MULTI_ARG_TYPES =
[ :ints, :integers, :strings, :doubles, :floats, :ios, :dates ]
- TYPES =
FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES
- INVALID_SHORT_ARG_REGEX =
/[\d-]/
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #array_default? ⇒ Boolean
- #default ⇒ Object
- #description ⇒ Object
- #flag? ⇒ Boolean
-
#initialize(name, description, options) ⇒ Option
constructor
A new instance of Option.
-
#key?(name) ⇒ Boolean
—————————————————————————- Option Methods.
- #long ⇒ Object
- #multi ⇒ Object (also: #multi?)
- #multi_arg? ⇒ Boolean
- #required? ⇒ Boolean
- #short ⇒ Object
- #short=(value) ⇒ Object
- #short? ⇒ Boolean
- #single_arg? ⇒ Boolean
- #type ⇒ Object
Constructor Details
#initialize(name, description, options) ⇒ Option
Returns a new instance of Option.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/unimatrix_parser.rb', line 90 def initialize( name, description, ) [ :description ] = description [ :type ] = normalize_type( [ :type ] ) type_from_default = disambiguate_and_set_type_from_default( ) if [ :type ] && type_from_default && [ :type ] != type_from_default raise ArgumentError, ":type specification and default type don't match " + "(default type is #{ type_from_default })" end [ :type ] = [ :type ] || type_from_default || :flag [ :long ] = set_long( [ :long ], name ) [ :short ] = set_short( [ :short ] ) if [ :short ] && [ :short ] =~ INVALID_SHORT_ARG_REGEX raise ArgumentError, "a short option name can't be a number or a dash" end [ :default ] = false if [ :type ] == :flag && [ :default ].nil? if [ :default ] && [ :multi ] && ![ :default ].kind_of?( Array ) [ :default ] = [ [ :default ] ] end [ :multi ] ||= false self.name = name self. = end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
88 89 90 |
# File 'lib/unimatrix_parser.rb', line 88 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
88 89 90 |
# File 'lib/unimatrix_parser.rb', line 88 def @options end |
Instance Method Details
#array_default? ⇒ Boolean
154 155 156 |
# File 'lib/unimatrix_parser.rb', line 154 def array_default? [ :default ].kind_of?( Array ) end |
#default ⇒ Object
150 151 152 |
# File 'lib/unimatrix_parser.rb', line 150 def default [ :default ] end |
#description ⇒ Object
174 175 176 |
# File 'lib/unimatrix_parser.rb', line 174 def description [ :description ] end |
#flag? ⇒ Boolean
132 133 134 |
# File 'lib/unimatrix_parser.rb', line 132 def flag? type == :flag end |
#key?(name) ⇒ Boolean
Option Methods
124 125 126 |
# File 'lib/unimatrix_parser.rb', line 124 def key?( name ) .key?( name ) end |
#long ⇒ Object
170 171 172 |
# File 'lib/unimatrix_parser.rb', line 170 def long [ :long ] end |
#multi ⇒ Object Also known as: multi?
140 141 142 |
# File 'lib/unimatrix_parser.rb', line 140 def multi [ :multi ] end |
#multi_arg? ⇒ Boolean
146 147 148 |
# File 'lib/unimatrix_parser.rb', line 146 def multi_arg? MULTI_ARG_TYPES.include?( type ) end |
#required? ⇒ Boolean
178 179 180 |
# File 'lib/unimatrix_parser.rb', line 178 def required? [ :required ] end |
#short ⇒ Object
158 159 160 |
# File 'lib/unimatrix_parser.rb', line 158 def short [ :short ] end |
#short=(value) ⇒ Object
166 167 168 |
# File 'lib/unimatrix_parser.rb', line 166 def short=( value ) [ :short ] = value end |
#short? ⇒ Boolean
162 163 164 |
# File 'lib/unimatrix_parser.rb', line 162 def short? short && short != :none end |
#single_arg? ⇒ Boolean
136 137 138 |
# File 'lib/unimatrix_parser.rb', line 136 def single_arg? SINGLE_ARG_TYPES.include?( type ) end |
#type ⇒ Object
128 129 130 |
# File 'lib/unimatrix_parser.rb', line 128 def type [ :type ] end |