10 #include "CLI/Error.hpp" 11 #include "CLI/StringTools.hpp" 17 inline bool split_short(
const std::string ¤t, std::string &name, std::string &rest) {
18 if(current.size() > 1 && current[0] ==
'-' && valid_first_char(current[1])) {
19 name = current.substr(1, 1);
20 rest = current.substr(2);
27 inline bool split_long(
const std::string ¤t, std::string &name, std::string &value) {
28 if(current.size() > 2 && current.substr(0, 2) ==
"--" && valid_first_char(current[2])) {
29 auto loc = current.find_first_of(
'=');
30 if(loc != std::string::npos) {
31 name = current.substr(2, loc - 2);
32 value = current.substr(loc + 1);
34 name = current.substr(2);
43 inline bool split_windows_style(
const std::string ¤t, std::string &name, std::string &value) {
44 if(current.size() > 1 && current[0] ==
'/' && valid_first_char(current[1])) {
45 auto loc = current.find_first_of(
':');
46 if(loc != std::string::npos) {
47 name = current.substr(1, loc - 1);
48 value = current.substr(loc + 1);
50 name = current.substr(1);
59 inline std::vector<std::string> split_names(std::string current) {
60 std::vector<std::string> output;
62 while((val = current.find(
",")) != std::string::npos) {
63 output.push_back(trim_copy(current.substr(0, val)));
64 current = current.substr(val + 1);
66 output.push_back(trim_copy(current));
71 inline std::vector<std::pair<std::string, std::string>> get_default_flag_values(
const std::string &str) {
72 std::vector<std::string> flags = split_names(str);
73 flags.erase(std::remove_if(flags.begin(),
75 [](
const std::string &name) {
76 return ((name.empty()) || (!(((name.find_first_of(
'{') != std::string::npos) &&
77 (name.back() ==
'}')) ||
81 std::vector<std::pair<std::string, std::string>> output;
82 output.reserve(flags.size());
83 for(
auto &flag : flags) {
84 auto def_start = flag.find_first_of(
'{');
85 std::string defval =
"false";
86 if((def_start != std::string::npos) && (flag.back() ==
'}')) {
87 defval = flag.substr(def_start + 1);
89 flag.erase(def_start, std::string::npos);
91 flag.erase(0, flag.find_first_not_of(
"-!"));
92 output.emplace_back(flag, defval);
98 inline std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>
99 get_names(
const std::vector<std::string> &input) {
101 std::vector<std::string> short_names;
102 std::vector<std::string> long_names;
103 std::string pos_name;
105 for(std::string name : input) {
106 if(name.length() == 0)
108 else if(name.length() > 1 && name[0] ==
'-' && name[1] !=
'-') {
109 if(name.length() == 2 && valid_first_char(name[1]))
110 short_names.emplace_back(1, name[1]);
112 throw BadNameString::OneCharName(name);
113 }
else if(name.length() > 2 && name.substr(0, 2) ==
"--") {
114 name = name.substr(2);
115 if(valid_name_string(name))
116 long_names.push_back(name);
118 throw BadNameString::BadLongName(name);
119 }
else if(name ==
"-" || name ==
"--") {
120 throw BadNameString::DashesOnly(name);
122 if(pos_name.length() > 0)
123 throw BadNameString::MultiPositionalNames(name);
128 return std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>(
129 short_names, long_names, pos_name);