|
13 | 13 | #include <vector> |
14 | 14 | // [CLI11:public_includes:end] |
15 | 15 |
|
16 | | -#include "Error.hpp" |
17 | | -#include "StringTools.hpp" |
| 16 | +#include "Macros.hpp" |
18 | 17 |
|
19 | 18 | namespace CLI { |
20 | 19 | // [CLI11:split_hpp:verbatim] |
21 | 20 |
|
22 | 21 | namespace detail { |
23 | 22 |
|
24 | 23 | // Returns false if not a short option. Otherwise, sets opt name and rest and returns true |
25 | | -inline bool split_short(const std::string ¤t, std::string &name, std::string &rest) { |
26 | | - if(current.size() > 1 && current[0] == '-' && valid_first_char(current[1])) { |
27 | | - name = current.substr(1, 1); |
28 | | - rest = current.substr(2); |
29 | | - return true; |
30 | | - } |
31 | | - return false; |
32 | | -} |
| 24 | +CLI11_INLINE bool split_short(const std::string ¤t, std::string &name, std::string &rest); |
33 | 25 |
|
34 | 26 | // Returns false if not a long option. Otherwise, sets opt name and other side of = and returns true |
35 | | -inline bool split_long(const std::string ¤t, std::string &name, std::string &value) { |
36 | | - if(current.size() > 2 && current.substr(0, 2) == "--" && valid_first_char(current[2])) { |
37 | | - auto loc = current.find_first_of('='); |
38 | | - if(loc != std::string::npos) { |
39 | | - name = current.substr(2, loc - 2); |
40 | | - value = current.substr(loc + 1); |
41 | | - } else { |
42 | | - name = current.substr(2); |
43 | | - value = ""; |
44 | | - } |
45 | | - return true; |
46 | | - } |
47 | | - return false; |
48 | | -} |
| 27 | +CLI11_INLINE bool split_long(const std::string ¤t, std::string &name, std::string &value); |
49 | 28 |
|
50 | 29 | // Returns false if not a windows style option. Otherwise, sets opt name and value and returns true |
51 | | -inline bool split_windows_style(const std::string ¤t, std::string &name, std::string &value) { |
52 | | - if(current.size() > 1 && current[0] == '/' && valid_first_char(current[1])) { |
53 | | - auto loc = current.find_first_of(':'); |
54 | | - if(loc != std::string::npos) { |
55 | | - name = current.substr(1, loc - 1); |
56 | | - value = current.substr(loc + 1); |
57 | | - } else { |
58 | | - name = current.substr(1); |
59 | | - value = ""; |
60 | | - } |
61 | | - return true; |
62 | | - } |
63 | | - return false; |
64 | | -} |
| 30 | +CLI11_INLINE bool split_windows_style(const std::string ¤t, std::string &name, std::string &value); |
65 | 31 |
|
66 | 32 | // Splits a string into multiple long and short names |
67 | | -inline std::vector<std::string> split_names(std::string current) { |
68 | | - std::vector<std::string> output; |
69 | | - std::size_t val = 0; |
70 | | - while((val = current.find(',')) != std::string::npos) { |
71 | | - output.push_back(trim_copy(current.substr(0, val))); |
72 | | - current = current.substr(val + 1); |
73 | | - } |
74 | | - output.push_back(trim_copy(current)); |
75 | | - return output; |
76 | | -} |
| 33 | +CLI11_INLINE std::vector<std::string> split_names(std::string current); |
77 | 34 |
|
78 | 35 | /// extract default flag values either {def} or starting with a ! |
79 | | -inline std::vector<std::pair<std::string, std::string>> get_default_flag_values(const std::string &str) { |
80 | | - std::vector<std::string> flags = split_names(str); |
81 | | - flags.erase(std::remove_if(flags.begin(), |
82 | | - flags.end(), |
83 | | - [](const std::string &name) { |
84 | | - return ((name.empty()) || (!(((name.find_first_of('{') != std::string::npos) && |
85 | | - (name.back() == '}')) || |
86 | | - (name[0] == '!')))); |
87 | | - }), |
88 | | - flags.end()); |
89 | | - std::vector<std::pair<std::string, std::string>> output; |
90 | | - output.reserve(flags.size()); |
91 | | - for(auto &flag : flags) { |
92 | | - auto def_start = flag.find_first_of('{'); |
93 | | - std::string defval = "false"; |
94 | | - if((def_start != std::string::npos) && (flag.back() == '}')) { |
95 | | - defval = flag.substr(def_start + 1); |
96 | | - defval.pop_back(); |
97 | | - flag.erase(def_start, std::string::npos); // NOLINT(readability-suspicious-call-argument) |
98 | | - } |
99 | | - flag.erase(0, flag.find_first_not_of("-!")); |
100 | | - output.emplace_back(flag, defval); |
101 | | - } |
102 | | - return output; |
103 | | -} |
| 36 | +CLI11_INLINE std::vector<std::pair<std::string, std::string>> get_default_flag_values(const std::string &str); |
104 | 37 |
|
105 | 38 | /// Get a vector of short names, one of long names, and a single name |
106 | | -inline std::tuple<std::vector<std::string>, std::vector<std::string>, std::string> |
107 | | -get_names(const std::vector<std::string> &input) { |
108 | | - |
109 | | - std::vector<std::string> short_names; |
110 | | - std::vector<std::string> long_names; |
111 | | - std::string pos_name; |
112 | | - |
113 | | - for(std::string name : input) { |
114 | | - if(name.length() == 0) { |
115 | | - continue; |
116 | | - } |
117 | | - if(name.length() > 1 && name[0] == '-' && name[1] != '-') { |
118 | | - if(name.length() == 2 && valid_first_char(name[1])) |
119 | | - short_names.emplace_back(1, name[1]); |
120 | | - else |
121 | | - throw BadNameString::OneCharName(name); |
122 | | - } else if(name.length() > 2 && name.substr(0, 2) == "--") { |
123 | | - name = name.substr(2); |
124 | | - if(valid_name_string(name)) |
125 | | - long_names.push_back(name); |
126 | | - else |
127 | | - throw BadNameString::BadLongName(name); |
128 | | - } else if(name == "-" || name == "--") { |
129 | | - throw BadNameString::DashesOnly(name); |
130 | | - } else { |
131 | | - if(pos_name.length() > 0) |
132 | | - throw BadNameString::MultiPositionalNames(name); |
133 | | - pos_name = name; |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - return std::make_tuple(short_names, long_names, pos_name); |
138 | | -} |
| 39 | +CLI11_INLINE std::tuple<std::vector<std::string>, std::vector<std::string>, std::string> |
| 40 | +get_names(const std::vector<std::string> &input); |
139 | 41 |
|
140 | 42 | } // namespace detail |
141 | 43 | // [CLI11:split_hpp:end] |
142 | 44 | } // namespace CLI |
| 45 | + |
| 46 | +#ifndef CLI11_COMPILE |
| 47 | +#include "impl/Split_inl.hpp" |
| 48 | +#endif |
0 commit comments