Class: Ergane::OptionDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ergane/option_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, short: nil, description: nil, default: nil, required: false, optional: false) ⇒ OptionDefinition

Returns a new instance of OptionDefinition.



7
8
9
10
11
12
13
14
15
# File 'lib/ergane/option_definition.rb', line 7

def initialize(name, type = nil, short: nil, description: nil, default: nil, required: false, optional: false)
  @name = name.to_sym
  @type = type
  @short = short&.to_s
  @description = description
  @default = default
  @required = required
  @optional = optional
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/ergane/option_definition.rb', line 5

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/ergane/option_definition.rb', line 5

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/ergane/option_definition.rb', line 5

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



5
6
7
# File 'lib/ergane/option_definition.rb', line 5

def optional
  @optional
end

#requiredObject (readonly)

Returns the value of attribute required.



5
6
7
# File 'lib/ergane/option_definition.rb', line 5

def required
  @required
end

#shortObject (readonly)

Returns the value of attribute short.



5
6
7
# File 'lib/ergane/option_definition.rb', line 5

def short
  @short
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/ergane/option_definition.rb', line 5

def type
  @type
end

Instance Method Details

#attach(parser, store) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ergane/option_definition.rb', line 41

def attach(parser, store)
  args = [short_flag, long_flag].compact
  args << type if type && !boolean?
  args << description if description

  parser.on(*args) { |value| store[name] = value }
end

#boolean?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ergane/option_definition.rb', line 17

def boolean?
  type.nil? || type == TrueClass || type == FalseClass
end

#default_valueObject



21
22
23
# File 'lib/ergane/option_definition.rb', line 21

def default_value
  boolean? ? (default.nil? ? false : default) : default
end

#long_flagObject



25
26
27
28
29
30
31
# File 'lib/ergane/option_definition.rb', line 25

def long_flag
  flag = "--#{name.to_s.tr('_', '-')}"
  unless boolean?
    flag += optional ? "=[VALUE]" : "=VALUE"
  end
  flag
end

#short_flagObject



33
34
35
# File 'lib/ergane/option_definition.rb', line 33

def short_flag
  "-#{short}" if short
end

#signatureObject



37
38
39
# File 'lib/ergane/option_definition.rb', line 37

def signature
  [("#{short_flag}," if short), long_flag].compact.join(" ")
end