12#include "../Split.hpp"
21#include "../Error.hpp"
22#include "../StringTools.hpp"
29CLI11_INLINE
bool split_short(
const std::string ¤t, std::string &name, std::string &rest) {
30 if(current.size() > 1 && current[0] ==
'-' && valid_first_char(current[1])) {
31 name = current.substr(1, 1);
32 rest = current.substr(2);
38CLI11_INLINE
bool split_long(
const std::string ¤t, std::string &name, std::string &value) {
39 if(current.size() > 2 && current.compare(0, 2,
"--") == 0 && valid_first_char(current[2])) {
40 auto loc = current.find_first_of(
'=');
41 if(loc != std::string::npos) {
42 name = current.substr(2, loc - 2);
43 value = current.substr(loc + 1);
45 name = current.substr(2);
53CLI11_INLINE
bool split_windows_style(
const std::string ¤t, std::string &name, std::string &value) {
54 if(current.size() > 1 && current[0] ==
'/' && valid_first_char(current[1])) {
55 auto loc = current.find_first_of(
':');
56 if(loc != std::string::npos) {
57 name = current.substr(1, loc - 1);
58 value = current.substr(loc + 1);
60 name = current.substr(1);
68CLI11_INLINE std::vector<std::string> split_names(std::string current) {
69 std::vector<std::string> output;
71 while((val = current.find(
',')) != std::string::npos) {
72 output.push_back(trim_copy(current.substr(0, val)));
73 current = current.substr(val + 1);
75 output.push_back(trim_copy(current));
79CLI11_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(),
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);
105CLI11_INLINE std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>
106get_names(
const std::vector<std::string> &input,
bool allow_non_standard) {
108 std::vector<std::string> short_names;
109 std::vector<std::string> long_names;
110 std::string pos_name;
111 for(std::string name : input) {
112 if(name.length() == 0) {
115 if(name.length() > 1 && name[0] ==
'-' && name[1] !=
'-') {
116 if(name.length() == 2 && valid_first_char(name[1])) {
117 short_names.emplace_back(1, name[1]);
118 }
else if(name.length() > 2) {
119 if(allow_non_standard) {
120 name = name.substr(1);
121 if(valid_name_string(name)) {
122 short_names.push_back(name);
124 throw BadNameString::BadLongName(name);
127 throw BadNameString::MissingDash(name);
130 throw BadNameString::OneCharName(name);
132 }
else if(name.length() > 2 && name.substr(0, 2) ==
"--") {
133 name = name.substr(2);
134 if(valid_name_string(name)) {
135 long_names.push_back(name);
137 throw BadNameString::BadLongName(name);
139 }
else if(name ==
"-" || name ==
"--" || name ==
"++") {
140 throw BadNameString::ReservedName(name);
142 if(!pos_name.empty()) {
143 throw BadNameString::MultiPositionalNames(name);
145 if(valid_name_string(name)) {
148 throw BadNameString::BadPositionalName(name);
152 return std::make_tuple(short_names, long_names, pos_name);