25inline 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);
35inline 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);
42 name = current.substr(2);
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);
58 name = current.substr(1);
67inline std::vector<std::string>
split_names(std::string current) {
68 std::vector<std::string>
output;
70 while((val = current.find(
",")) != std::string::npos) {
72 current = current.substr(val + 1);
81 flags.erase(std::remove_if(flags.begin(),
83 [](
const std::string &name) {
84 return ((name.empty()) || (!(((name.find_first_of(
'{') != std::string::npos) &&
85 (name.back() ==
'}')) ||
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);
97 flag.erase(def_start, std::string::npos);
99 flag.erase(0, flag.find_first_not_of(
"-!"));
100 output.emplace_back(flag, defval);
106inline std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>
109 std::vector<std::string> short_names;
110 std::vector<std::string> long_names;
111 std::string pos_name;
113 for(std::string name : input) {
114 if(name.length() == 0) {
117 if(name.length() > 1 && name[0] ==
'-' && name[1] !=
'-') {
119 short_names.emplace_back(1, name[1]);
121 throw BadNameString::OneCharName(name);
122 }
else if(name.length() > 2 && name.substr(0, 2) ==
"--") {
123 name = name.substr(2);
125 long_names.push_back(name);
127 throw BadNameString::BadLongName(name);
128 }
else if(name ==
"-" || name ==
"--") {
129 throw BadNameString::DashesOnly(name);
131 if(pos_name.length() > 0)
132 throw BadNameString::MultiPositionalNames(name);
137 return std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>(
138 short_names, long_names, pos_name);
std::vector< std::string > split_names(std::string current)
Definition Split.hpp:67
bool split_long(const std::string ¤t, std::string &name, std::string &value)
Definition Split.hpp:35
bool split_short(const std::string ¤t, std::string &name, std::string &rest)
Definition Split.hpp:25
std::vector< std::string > output
Definition StringTools.hpp:354
std::tuple< std::vector< std::string >, std::vector< std::string >, std::string > get_names(const std::vector< std::string > &input)
Get a vector of short names, one of long names, and a single name.
Definition Split.hpp:107
bool valid_first_char(T c)
Definition StringTools.hpp:219
bool valid_name_string(const std::string &str)
Verify an option/subcommand name.
Definition StringTools.hpp:230
std::string trim_copy(const std::string &str)
Make a copy of the string and then trim it.
Definition StringTools.hpp:144
bool split_windows_style(const std::string ¤t, std::string &name, std::string &value)
Definition Split.hpp:51
std::vector< std::pair< std::string, std::string > > get_default_flag_values(const std::string &str)
extract default flag values either {def} or starting with a !
Definition Split.hpp:79