Class: Hiiro::Options::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#defaultObject (readonly)

Returns the value of attribute default.



262
263
264
# File 'lib/hiiro/options.rb', line 262

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



262
263
264
# File 'lib/hiiro/options.rb', line 262

def desc
  @desc
end

#flag_ifsObject (readonly)

Returns the value of attribute flag_ifs.



262
263
264
# File 'lib/hiiro/options.rb', line 262

def flag_ifs
  @flag_ifs
end

#longObject (readonly)

Returns the value of attribute long.



262
263
264
# File 'lib/hiiro/options.rb', line 262

def long
  @long
end

#multiObject (readonly)

Returns the value of attribute multi.



262
263
264
# File 'lib/hiiro/options.rb', line 262

def multi
  @multi
end

#nameObject (readonly)

Returns the value of attribute name.



262
263
264
# File 'lib/hiiro/options.rb', line 262

def name
  @name
end

#shortObject

Returns the value of attribute short.



263
264
265
# File 'lib/hiiro/options.rb', line 263

def short
  @short
end

#typeObject (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

Returns:

  • (Boolean)


280
281
282
# File 'lib/hiiro/options.rb', line 280

def flag?
  type == :flag
end

#flag_active?(values) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/hiiro/options.rb', line 276

def flag_active?(values)
  @flag_ifs.any? { |f| values[f] }
end

#long_formObject



284
285
286
# File 'lib/hiiro/options.rb', line 284

def long_form
  "--#{(@long || @name).to_s.tr('_', '-')}"
end

#match?(arg) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/hiiro/options.rb', line 292

def match?(arg)
  arg == long_form || arg == short_form
end

#short_formObject



288
289
290
# File 'lib/hiiro/options.rb', line 288

def short_form
  short ? "-#{short}" : nil
end

#usage_lineObject



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_hintObject



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