File indexing completed on 2025-01-30 10:03:36
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009
0010 #include <CLI/Split.hpp>
0011
0012
0013 #include <string>
0014 #include <tuple>
0015 #include <utility>
0016 #include <vector>
0017
0018
0019 #include <CLI/Error.hpp>
0020 #include <CLI/StringTools.hpp>
0021
0022 namespace CLI {
0023
0024
0025 namespace detail {
0026
0027 CLI11_INLINE bool split_short(const std::string ¤t, std::string &name, std::string &rest) {
0028 if(current.size() > 1 && current[0] == '-' && valid_first_char(current[1])) {
0029 name = current.substr(1, 1);
0030 rest = current.substr(2);
0031 return true;
0032 }
0033 return false;
0034 }
0035
0036 CLI11_INLINE bool split_long(const std::string ¤t, std::string &name, std::string &value) {
0037 if(current.size() > 2 && current.substr(0, 2) == "--" && valid_first_char(current[2])) {
0038 auto loc = current.find_first_of('=');
0039 if(loc != std::string::npos) {
0040 name = current.substr(2, loc - 2);
0041 value = current.substr(loc + 1);
0042 } else {
0043 name = current.substr(2);
0044 value = "";
0045 }
0046 return true;
0047 }
0048 return false;
0049 }
0050
0051 CLI11_INLINE bool split_windows_style(const std::string ¤t, std::string &name, std::string &value) {
0052 if(current.size() > 1 && current[0] == '/' && valid_first_char(current[1])) {
0053 auto loc = current.find_first_of(':');
0054 if(loc != std::string::npos) {
0055 name = current.substr(1, loc - 1);
0056 value = current.substr(loc + 1);
0057 } else {
0058 name = current.substr(1);
0059 value = "";
0060 }
0061 return true;
0062 }
0063 return false;
0064 }
0065
0066 CLI11_INLINE std::vector<std::string> split_names(std::string current) {
0067 std::vector<std::string> output;
0068 std::size_t val = 0;
0069 while((val = current.find(',')) != std::string::npos) {
0070 output.push_back(trim_copy(current.substr(0, val)));
0071 current = current.substr(val + 1);
0072 }
0073 output.push_back(trim_copy(current));
0074 return output;
0075 }
0076
0077 CLI11_INLINE std::vector<std::pair<std::string, std::string>> get_default_flag_values(const std::string &str) {
0078 std::vector<std::string> flags = split_names(str);
0079 flags.erase(std::remove_if(flags.begin(),
0080 flags.end(),
0081 [](const std::string &name) {
0082 return ((name.empty()) || (!(((name.find_first_of('{') != std::string::npos) &&
0083 (name.back() == '}')) ||
0084 (name[0] == '!'))));
0085 }),
0086 flags.end());
0087 std::vector<std::pair<std::string, std::string>> output;
0088 output.reserve(flags.size());
0089 for(auto &flag : flags) {
0090 auto def_start = flag.find_first_of('{');
0091 std::string defval = "false";
0092 if((def_start != std::string::npos) && (flag.back() == '}')) {
0093 defval = flag.substr(def_start + 1);
0094 defval.pop_back();
0095 flag.erase(def_start, std::string::npos);
0096 }
0097 flag.erase(0, flag.find_first_not_of("-!"));
0098 output.emplace_back(flag, defval);
0099 }
0100 return output;
0101 }
0102
0103 CLI11_INLINE std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>
0104 get_names(const std::vector<std::string> &input) {
0105
0106 std::vector<std::string> short_names;
0107 std::vector<std::string> long_names;
0108 std::string pos_name;
0109
0110 for(std::string name : input) {
0111 if(name.length() == 0) {
0112 continue;
0113 }
0114 if(name.length() > 1 && name[0] == '-' && name[1] != '-') {
0115 if(name.length() == 2 && valid_first_char(name[1]))
0116 short_names.emplace_back(1, name[1]);
0117 else
0118 throw BadNameString::OneCharName(name);
0119 } else if(name.length() > 2 && name.substr(0, 2) == "--") {
0120 name = name.substr(2);
0121 if(valid_name_string(name))
0122 long_names.push_back(name);
0123 else
0124 throw BadNameString::BadLongName(name);
0125 } else if(name == "-" || name == "--") {
0126 throw BadNameString::DashesOnly(name);
0127 } else {
0128 if(pos_name.length() > 0)
0129 throw BadNameString::MultiPositionalNames(name);
0130 pos_name = name;
0131 }
0132 }
0133
0134 return std::make_tuple(short_names, long_names, pos_name);
0135 }
0136
0137 }
0138
0139 }