parameters_parser.cc (3647B)
1 #include "parameters_parser.hh" 2 3 #include "ranges.hh" 4 #include "flags.hh" 5 6 namespace Kakoune 7 { 8 9 String generate_switches_doc(const SwitchMap& switches) 10 { 11 String res; 12 if (switches.empty()) 13 return res; 14 15 auto switch_len = [](auto& sw) { return sw.key.column_length() + (sw.value.arg_completer ? 5 : 0); }; 16 auto switches_len = switches | transform(switch_len); 17 const ColumnCount maxlen = *std::max_element(switches_len.begin(), switches_len.end()); 18 19 for (auto& sw : switches) { 20 res += format("-{} {}{}{}\n", 21 sw.key, 22 sw.value.arg_completer ? "<arg>" : "", 23 String{' ', maxlen - switch_len(sw) + 1}, 24 sw.value.description); 25 } 26 return res; 27 } 28 29 ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& desc, bool ignore_errors) 30 : m_params(params) 31 { 32 const bool switches_only_at_start = desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart; 33 const bool ignore_unknown_switches = desc.flags & ParameterDesc::Flags::IgnoreUnknownSwitches; 34 bool only_pos = desc.flags & ParameterDesc::Flags::SwitchesAsPositional; 35 36 Vector<bool> switch_seen(desc.switches.size(), false); 37 for (size_t i = 0; i < params.size(); ++i) 38 { 39 if (not only_pos and not ignore_unknown_switches and params[i] == "--") 40 { 41 m_state = State::Switch; 42 only_pos = true; 43 } 44 else if (not only_pos and not params[i].empty() and params[i][0_byte] == '-') 45 { 46 StringView switch_name = params[i].substr(1_byte); 47 auto it = desc.switches.find(switch_name); 48 m_state = it == desc.switches.end() and ignore_unknown_switches ? 49 State::Positional : State::Switch; 50 if (it == desc.switches.end()) 51 { 52 if (ignore_unknown_switches) 53 { 54 m_positional_indices.push_back(i); 55 if (switches_only_at_start) 56 only_pos = true; 57 continue; 58 } 59 if (ignore_errors) 60 continue; 61 throw unknown_option(params[i]); 62 } 63 64 auto switch_index = it - desc.switches.begin(); 65 if (switch_seen[switch_index]) 66 { 67 if (ignore_errors) 68 continue; 69 throw runtime_error{format("switch '-{}' specified more than once", it->key)}; 70 } 71 switch_seen[switch_index] = true; 72 73 if (it->value.arg_completer) 74 { 75 if (++i == params.size()) 76 { 77 if (ignore_errors) 78 continue; 79 throw missing_option_value(it->key); 80 } 81 m_state = State::SwitchArgument; 82 } 83 84 m_switches[switch_name.str()] = it->value.arg_completer ? params[i] : StringView{}; 85 } 86 else // positional 87 { 88 m_state = State::Positional; 89 if (switches_only_at_start) 90 only_pos = true; 91 m_positional_indices.push_back(i); 92 } 93 } 94 size_t count = m_positional_indices.size(); 95 if (not ignore_errors and (count > desc.max_positionals or count < desc.min_positionals)) 96 throw wrong_argument_count(); 97 } 98 99 Optional<StringView> ParametersParser::get_switch(StringView name) const 100 { 101 auto it = m_switches.find(name); 102 return it == m_switches.end() ? Optional<StringView>{} 103 : Optional<StringView>{it->value}; 104 } 105 106 }