Class: Cri::OptionDefinition

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

Overview

The definition of an option.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(short:, long:, desc:, argument:, multiple:, block:, hidden:, default:, transform:) ⇒ OptionDefinition

Returns a new instance of OptionDefinition.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cri/option_definition.rb', line 16

def initialize(short:, long:, desc:, argument:, multiple:, block:, hidden:, default:, transform:)
  @short     = short
  @long      = long
  @desc      = desc
  @argument  = argument
  @multiple  = multiple
  @block     = block
  @hidden    = hidden
  @default   = default
  @transform = transform

  if @short.nil? && @long.nil?
    raise ArgumentError, 'short and long options cannot both be nil'
  end

  if @default && @argument == :forbidden
    raise ArgumentError, 'a default value cannot be specified for flag options'
  end

  @default = false if @argument == :forbidden
end

Instance Attribute Details

#argumentObject (readonly)

Returns the value of attribute argument.



9
10
11
# File 'lib/cri/option_definition.rb', line 9

def argument
  @argument
end

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/cri/option_definition.rb', line 11

def block
  @block
end

#defaultObject (readonly)

Returns the value of attribute default.



13
14
15
# File 'lib/cri/option_definition.rb', line 13

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



8
9
10
# File 'lib/cri/option_definition.rb', line 8

def desc
  @desc
end

#hiddenObject (readonly)

Returns the value of attribute hidden.



12
13
14
# File 'lib/cri/option_definition.rb', line 12

def hidden
  @hidden
end

#longObject (readonly)

Returns the value of attribute long.



7
8
9
# File 'lib/cri/option_definition.rb', line 7

def long
  @long
end

#multipleObject (readonly)

Returns the value of attribute multiple.



10
11
12
# File 'lib/cri/option_definition.rb', line 10

def multiple
  @multiple
end

#shortObject (readonly)

Returns the value of attribute short.



6
7
8
# File 'lib/cri/option_definition.rb', line 6

def short
  @short
end

#transformObject (readonly)

Returns the value of attribute transform.



14
15
16
# File 'lib/cri/option_definition.rb', line 14

def transform
  @transform
end

Instance Method Details

#formatted_nameObject



52
53
54
# File 'lib/cri/option_definition.rb', line 52

def formatted_name
  @long ? '--' + @long : '-' + @short
end

#to_hObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cri/option_definition.rb', line 38

def to_h
  {
    short: @short,
    long: @long,
    desc: @desc,
    argument: @argument,
    multiple: @multiple,
    block: @block,
    hidden: @hidden,
    default: @default,
    transform: @transform,
  }
end