Class: Tomo::CLI::Rules::ValueSwitch
- Includes:
- Tomo::Colors
- Defined in:
- lib/tomo/cli/rules/value_switch.rb
Instance Method Summary collapse
- #candidates(switch = nil, state:, literal: false) ⇒ Object
-
#initialize(key, *switches, values_proc:, callback_proc:) ⇒ ValueSwitch
constructor
A new instance of ValueSwitch.
- #match(arg, literal: false) ⇒ Object
- #process(switch, arg = nil, state:) ⇒ Object
Methods included from Tomo::Colors
Methods inherited from Switch
Constructor Details
#initialize(key, *switches, values_proc:, callback_proc:) ⇒ ValueSwitch
Returns a new instance of ValueSwitch.
7 8 9 10 11 |
# File 'lib/tomo/cli/rules/value_switch.rb', line 7 def initialize(key, *switches, values_proc:, callback_proc:) super(key, *switches, callback_proc:) @values_proc = values_proc end |
Instance Method Details
#candidates(switch = nil, state:, literal: false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tomo/cli/rules/value_switch.rb', line 33 def candidates(switch=nil, state:, literal: false) return [] if literal vals = values(state) return vals.reject { |val| val.start_with?("-") } if switch switches.each_with_object([]) do |each_switch, result| result << each_switch vals.each do |value| result << "#{each_switch}=#{value}" if each_switch.start_with?("--") end end end |
#match(arg, literal: false) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/tomo/cli/rules/value_switch.rb', line 13 def match(arg, literal: false) return nil if literal return 2 if switches.include?(arg) 1 if arg.start_with?("--") && switches.include?(arg.split("=").first) end |
#process(switch, arg = nil, state:) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tomo/cli/rules/value_switch.rb', line 20 def process(switch, arg=nil, state:) value = if switch.include?("=") switch.split("=", 2).last elsif !arg.to_s.start_with?("-") arg end raise_missing_value(switch) if value.nil? callback_proc&.call(value) state.parsed_option(key, value) end |