Skip to content

Commit 312d5ee

Browse files
committed
Changes following CR by @jarro2783:
* Simplified the separation of the short name from the long names using `std::partition` * Using C-style logical operators: `!`, `&&`, `||` rather than `not`, `and`, `or` * `std::move()`ing from a vector of strings instead of just copying it.
1 parent d994b9b commit 312d5ee

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

include/cxxopts.hpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ inline OptionNames split_option_names(const std::string &text)
659659
"abcdefghijklmnopqrstuvwxyz"
660660
"0123456789"
661661
"_-";
662-
if (not std::isalnum(text[token_start_pos]) or
662+
if (!std::isalnum(text[token_start_pos]) ||
663663
text.find_first_not_of(option_name_valid_chars, token_start_pos) < next_delimiter_pos) {
664664
throw_or_mimic<exceptions::invalid_option_format>(text);
665665
}
@@ -783,7 +783,7 @@ inline bool IsFalseText(const std::string &text)
783783
// (without considering which or how many are single-character)
784784
inline OptionNames split_option_names(const std::string &text)
785785
{
786-
if (not std::regex_match(text.c_str(), option_specifier))
786+
if (!std::regex_match(text.c_str(), option_specifier))
787787
{
788788
throw_or_mimic<exceptions::invalid_option_format>(text);
789789
}
@@ -2169,34 +2169,30 @@ OptionAdder::operator()
21692169
std::string arg_help
21702170
)
21712171
{
2172-
OptionNames all_option_names = values::parser_tool::split_option_names(opts);
2173-
// Note: All names will be non-empty!
2174-
std::string short_sw {""};
2175-
OptionNames long_option_names = all_option_names;
2176-
auto first_length_1_name_it = std::find_if(long_option_names.cbegin(), long_option_names.cend(),
2177-
[&](const std::string& name) { return name.length() == 1; }
2178-
);
2179-
if (first_length_1_name_it != long_option_names.cend()) {
2180-
auto have_second_length_1_name = std::any_of(first_length_1_name_it + 1, long_option_names.cend(),
2181-
[&](const std::string& name) { return name.length() == 1; }
2172+
OptionNames option_names = values::parser_tool::split_option_names(opts);
2173+
// Note: All names will be non-empty; but we must separate the short
2174+
// (length-1) and longer names
2175+
std::string short_name {""};
2176+
auto first_short_name_iter =
2177+
std::partition(option_names.begin(), option_names.end(),
2178+
[&](const std::string& name) { return name.length() > 1; }
21822179
);
2183-
if (have_second_length_1_name) {
2184-
throw_or_mimic<exceptions::invalid_option_format>(opts);
2185-
}
2186-
short_sw = *first_length_1_name_it;
2187-
long_option_names.erase(first_length_1_name_it);
2188-
}
2189-
if (short_sw.empty() && long_option_names.empty())
2190-
{
2180+
auto num_length_1_names = (option_names.end() - first_short_name_iter);
2181+
switch(num_length_1_names) {
2182+
case 1:
2183+
short_name = *first_short_name_iter;
2184+
option_names.erase(first_short_name_iter);
2185+
case 0:
2186+
break;
2187+
default:
21912188
throw_or_mimic<exceptions::invalid_option_format>(opts);
2192-
}
2193-
2189+
};
21942190

21952191
m_options.add_option
21962192
(
21972193
m_group,
2198-
short_sw,
2199-
long_option_names,
2194+
short_name,
2195+
option_names,
22002196
desc,
22012197
value,
22022198
std::move(arg_help)
@@ -2618,7 +2614,7 @@ Options::help_one_group(const std::string& g) const
26182614

26192615
for (const auto& o : group->second.options)
26202616
{
2621-
assert(not o.l.empty());
2617+
assert(!o.l.empty());
26222618
if (m_positional_set.find(o.l.front()) != m_positional_set.end() &&
26232619
!m_show_positional)
26242620
{
@@ -2641,7 +2637,7 @@ Options::help_one_group(const std::string& g) const
26412637
auto fiter = format.begin();
26422638
for (const auto& o : group->second.options)
26432639
{
2644-
assert(not o.l.empty());
2640+
assert(!o.l.empty());
26452641
if (m_positional_set.find(o.l.front()) != m_positional_set.end() &&
26462642
!m_show_positional)
26472643
{

0 commit comments

Comments
 (0)