Skip to content

Commit d1f0f48

Browse files
dherrera-metahenryiii
authored andcommitted
[precompile] Split Split.hpp
1 parent c24bf75 commit d1f0f48

File tree

4 files changed

+160
-106
lines changed

4 files changed

+160
-106
lines changed

CLI11.hpp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ namespace {namespace} {{
5252

5353
{split_hpp}
5454

55+
{split_inl_hpp}
56+
5557
{config_fwd_hpp}
5658

5759
{validators_hpp}

include/CLI/Split.hpp

Lines changed: 12 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -13,130 +13,36 @@
1313
#include <vector>
1414
// [CLI11:public_includes:end]
1515

16-
#include "Error.hpp"
17-
#include "StringTools.hpp"
16+
#include "Macros.hpp"
1817

1918
namespace CLI {
2019
// [CLI11:split_hpp:verbatim]
2120

2221
namespace detail {
2322

2423
// Returns false if not a short option. Otherwise, sets opt name and rest and returns true
25-
inline bool split_short(const std::string &current, 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 &current, std::string &name, std::string &rest);
3325

3426
// 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 &current, 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 &current, std::string &name, std::string &value);
4928

5029
// 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 &current, 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 &current, std::string &name, std::string &value);
6531

6632
// 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);
7734

7835
/// 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);
10437

10538
/// 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);
13941

14042
} // namespace detail
14143
// [CLI11:split_hpp:end]
14244
} // namespace CLI
45+
46+
#ifndef CLI11_COMPILE
47+
#include "impl/Split_inl.hpp"
48+
#endif

include/CLI/impl/Split_inl.hpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright (c) 2017-2022, University of Cincinnati, developed by Henry Schreiner
2+
// under NSF AWARD 1414736 and by the respective contributors.
3+
// All rights reserved.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
#pragma once
8+
9+
// This include is only needed for IDEs to discover symbols
10+
#include <CLI/Split.hpp>
11+
12+
// [CLI11:public_includes:set]
13+
#include <string>
14+
#include <tuple>
15+
#include <utility>
16+
#include <vector>
17+
// [CLI11:public_includes:end]
18+
19+
#include <CLI/Error.hpp>
20+
#include <CLI/StringTools.hpp>
21+
22+
namespace CLI {
23+
// [CLI11:split_inl_hpp:verbatim]
24+
25+
namespace detail {
26+
27+
CLI11_INLINE bool split_short(const std::string &current, std::string &name, std::string &rest) {
28+
if(current.size() > 1 && current[0] == '-' && valid_first_char(current[1])) {
29+
name = current.substr(1, 1);
30+
rest = current.substr(2);
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
CLI11_INLINE bool split_long(const std::string &current, std::string &name, std::string &value) {
37+
if(current.size() > 2 && current.substr(0, 2) == "--" && valid_first_char(current[2])) {
38+
auto loc = current.find_first_of('=');
39+
if(loc != std::string::npos) {
40+
name = current.substr(2, loc - 2);
41+
value = current.substr(loc + 1);
42+
} else {
43+
name = current.substr(2);
44+
value = "";
45+
}
46+
return true;
47+
}
48+
return false;
49+
}
50+
51+
CLI11_INLINE bool split_windows_style(const std::string &current, 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+
}
65+
66+
CLI11_INLINE std::vector<std::string> split_names(std::string current) {
67+
std::vector<std::string> output;
68+
std::size_t val = 0;
69+
while((val = current.find(',')) != std::string::npos) {
70+
output.push_back(trim_copy(current.substr(0, val)));
71+
current = current.substr(val + 1);
72+
}
73+
output.push_back(trim_copy(current));
74+
return output;
75+
}
76+
77+
CLI11_INLINE std::vector<std::pair<std::string, std::string>> get_default_flag_values(const std::string &str) {
78+
std::vector<std::string> flags = split_names(str);
79+
flags.erase(std::remove_if(flags.begin(),
80+
flags.end(),
81+
[](const std::string &name) {
82+
return ((name.empty()) || (!(((name.find_first_of('{') != std::string::npos) &&
83+
(name.back() == '}')) ||
84+
(name[0] == '!'))));
85+
}),
86+
flags.end());
87+
std::vector<std::pair<std::string, std::string>> output;
88+
output.reserve(flags.size());
89+
for(auto &flag : flags) {
90+
auto def_start = flag.find_first_of('{');
91+
std::string defval = "false";
92+
if((def_start != std::string::npos) && (flag.back() == '}')) {
93+
defval = flag.substr(def_start + 1);
94+
defval.pop_back();
95+
flag.erase(def_start, std::string::npos); // NOLINT(readability-suspicious-call-argument)
96+
}
97+
flag.erase(0, flag.find_first_not_of("-!"));
98+
output.emplace_back(flag, defval);
99+
}
100+
return output;
101+
}
102+
103+
CLI11_INLINE std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>
104+
get_names(const std::vector<std::string> &input) {
105+
106+
std::vector<std::string> short_names;
107+
std::vector<std::string> long_names;
108+
std::string pos_name;
109+
110+
for(std::string name : input) {
111+
if(name.length() == 0) {
112+
continue;
113+
}
114+
if(name.length() > 1 && name[0] == '-' && name[1] != '-') {
115+
if(name.length() == 2 && valid_first_char(name[1]))
116+
short_names.emplace_back(1, name[1]);
117+
else
118+
throw BadNameString::OneCharName(name);
119+
} else if(name.length() > 2 && name.substr(0, 2) == "--") {
120+
name = name.substr(2);
121+
if(valid_name_string(name))
122+
long_names.push_back(name);
123+
else
124+
throw BadNameString::BadLongName(name);
125+
} else if(name == "-" || name == "--") {
126+
throw BadNameString::DashesOnly(name);
127+
} else {
128+
if(pos_name.length() > 0)
129+
throw BadNameString::MultiPositionalNames(name);
130+
pos_name = name;
131+
}
132+
}
133+
134+
return std::make_tuple(short_names, long_names, pos_name);
135+
}
136+
137+
} // namespace detail
138+
// [CLI11:split_inl_hpp:end]
139+
} // namespace CLI

src/Split.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2017-2022, University of Cincinnati, developed by Henry Schreiner
2+
// under NSF AWARD 1414736 and by the respective contributors.
3+
// All rights reserved.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
#include <CLI/impl/Split_inl.hpp>

0 commit comments

Comments
 (0)