Class: Tomo::CLI::Rules::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/tomo/cli/rules/switch.rb

Direct Known Subclasses

ValueSwitch

Instance Method Summary collapse

Constructor Details

#initialize(key, *switches, callback_proc:, required: false, &convert_proc) ⇒ Switch

Returns a new instance of Switch.



5
6
7
8
9
10
11
# File 'lib/tomo/cli/rules/switch.rb', line 5

def initialize(key, *switches, callback_proc:, required: false, &convert_proc)
  @key = key
  @switches = switches
  @callback_proc = callback_proc
  @convert_proc = convert_proc || proc { true }
  @required = required
end

Instance Method Details

#candidates(literal: false, **_kwargs) ⇒ Object



25
26
27
# File 'lib/tomo/cli/rules/switch.rb', line 25

def candidates(literal: false, **_kwargs)
  literal ? [] : switches
end

#match(arg, literal: false) ⇒ Object



13
14
15
16
17
# File 'lib/tomo/cli/rules/switch.rb', line 13

def match(arg, literal: false)
  return nil if literal

  1 if switches.include?(arg)
end

#multiple?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/tomo/cli/rules/switch.rb', line 33

def multiple?
  true
end

#process(arg, state:) ⇒ Object



19
20
21
22
23
# File 'lib/tomo/cli/rules/switch.rb', line 19

def process(arg, state:)
  value = convert_proc.call(arg)
  callback_proc&.call(value)
  state.parsed_option(key, value)
end

#required?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/tomo/cli/rules/switch.rb', line 29

def required?
  @required
end