Class: Hiiro::Options::Definition
- Inherits:
-
Object
- Object
- Hiiro::Options::Definition
- Defined in:
- lib/hiiro/options.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#flag_ifs ⇒ Object
readonly
Returns the value of attribute flag_ifs.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#multi ⇒ Object
readonly
Returns the value of attribute multi.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#short ⇒ Object
Returns the value of attribute short.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #coerce(value) ⇒ Object
- #flag? ⇒ Boolean
- #flag_active?(values) ⇒ Boolean
-
#initialize(name, short: nil, long: nil, type: :string, default: nil, desc: nil, multi: false, flag_ifs: []) ⇒ Definition
constructor
A new instance of Definition.
- #long_form ⇒ Object
- #match?(arg) ⇒ Boolean
- #short_form ⇒ Object
- #usage_line ⇒ Object
- #value_hint ⇒ Object
Constructor Details
#initialize(name, short: nil, long: nil, type: :string, default: nil, desc: nil, multi: false, flag_ifs: []) ⇒ Definition
Returns a new instance of Definition.
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/hiiro/options.rb', line 265 def initialize(name, short: nil, long: nil, type: :string, default: nil, desc: nil, multi: false, flag_ifs: []) @name = name.to_sym @short = short&.to_s @long = long&.to_sym @type = type @default = default @desc = desc @multi = multi @flag_ifs = flag_ifs.map(&:to_sym) end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
262 263 264 |
# File 'lib/hiiro/options.rb', line 262 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
262 263 264 |
# File 'lib/hiiro/options.rb', line 262 def desc @desc end |
#flag_ifs ⇒ Object (readonly)
Returns the value of attribute flag_ifs.
262 263 264 |
# File 'lib/hiiro/options.rb', line 262 def flag_ifs @flag_ifs end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
262 263 264 |
# File 'lib/hiiro/options.rb', line 262 def long @long end |
#multi ⇒ Object (readonly)
Returns the value of attribute multi.
262 263 264 |
# File 'lib/hiiro/options.rb', line 262 def multi @multi end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
262 263 264 |
# File 'lib/hiiro/options.rb', line 262 def name @name end |
#short ⇒ Object
Returns the value of attribute short.
263 264 265 |
# File 'lib/hiiro/options.rb', line 263 def short @short end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
262 263 264 |
# File 'lib/hiiro/options.rb', line 262 def type @type end |
Instance Method Details
#coerce(value) ⇒ Object
296 297 298 299 300 301 302 |
# File 'lib/hiiro/options.rb', line 296 def coerce(value) case type when :integer then value.to_i when :float then value.to_f else value end end |
#flag? ⇒ Boolean
280 281 282 |
# File 'lib/hiiro/options.rb', line 280 def flag? type == :flag end |
#flag_active?(values) ⇒ Boolean
276 277 278 |
# File 'lib/hiiro/options.rb', line 276 def flag_active?(values) @flag_ifs.any? { |f| values[f] } end |
#long_form ⇒ Object
284 285 286 |
# File 'lib/hiiro/options.rb', line 284 def long_form "--#{(@long || @name).to_s.tr('_', '-')}" end |
#match?(arg) ⇒ Boolean
292 293 294 |
# File 'lib/hiiro/options.rb', line 292 def match?(arg) arg == long_form || arg == short_form end |
#short_form ⇒ Object
288 289 290 |
# File 'lib/hiiro/options.rb', line 288 def short_form short ? "-#{short}" : nil end |
#usage_line ⇒ Object
304 305 306 307 308 309 310 311 312 313 |
# File 'lib/hiiro/options.rb', line 304 def usage_line parts = [] parts << (short_form ? "#{short_form}, #{long_form}" : " #{long_form}") parts[0] = parts[0].ljust(20) parts << value_hint unless flag? parts << desc if desc parts << "(default: #{default.inspect})" if default && !flag? parts << "[multi]" if multi parts.join(" ") end |
#value_hint ⇒ Object
315 316 317 318 319 320 321 |
# File 'lib/hiiro/options.rb', line 315 def value_hint case type when :integer then "<int>" when :float then "<num>" else "<value>" end end |