Removed static from lambda function, updated copy constructor for m_assign_chars

This commit is contained in:
Pranav Srinivas Kumar 2022-09-21 10:58:25 -07:00
parent 632ca2fcf8
commit d56515f6df

View File

@ -845,7 +845,7 @@ private:
if (first == eof) { if (first == eof) {
return true; return true;
} else if (prefix_chars.find(static_cast<char>(first)) != } else if (prefix_chars.find(static_cast<char>(first)) !=
std::string::npos) { std::string_view::npos) {
name.remove_prefix(1); name.remove_prefix(1);
if (name.empty()) { if (name.empty()) {
return true; return true;
@ -968,7 +968,8 @@ public:
ArgumentParser(const ArgumentParser &other) ArgumentParser(const ArgumentParser &other)
: m_program_name(other.m_program_name), m_version(other.m_version), : m_program_name(other.m_program_name), m_version(other.m_version),
m_description(other.m_description), m_epilog(other.m_epilog), m_description(other.m_description), m_epilog(other.m_epilog),
m_prefix_chars(other.m_prefix_chars), m_is_parsed(other.m_is_parsed), m_prefix_chars(other.m_prefix_chars),
m_assign_chars(other.m_assign_chars), m_is_parsed(other.m_is_parsed),
m_positional_arguments(other.m_positional_arguments), m_positional_arguments(other.m_positional_arguments),
m_optional_arguments(other.m_optional_arguments), m_optional_arguments(other.m_optional_arguments),
m_parser_path(other.m_parser_path), m_subparsers(other.m_subparsers) { m_parser_path(other.m_parser_path), m_subparsers(other.m_subparsers) {
@ -1260,20 +1261,15 @@ private:
* options table, should be split. * options table, should be split.
*/ */
std::vector<std::string> std::vector<std::string>
preprocess_arguments(const std::vector<std::string> &raw_arguments) { preprocess_arguments(const std::vector<std::string> &raw_arguments) const {
std::vector<std::string> arguments; std::vector<std::string> arguments{};
for (const auto &arg : raw_arguments) { for (const auto &arg : raw_arguments) {
// Check that:
// - We don't have an argument named exactly this
// - The argument starts with a prefix char, e.g., "--"
// - The argument contains an assign char, e.g., "="
std::size_t eqpos = arg.find_first_of(m_assign_chars);
static const auto argument_starts_with_prefix_chars = const auto argument_starts_with_prefix_chars =
[this](const std::string &a) { [this](const std::string &a) -> bool {
if (a.size() > 0) { if (!a.empty()) {
const auto legal_prefix = [this](char c) { const auto legal_prefix = [this](char c) -> bool {
return m_prefix_chars.find(c) != std::string::npos; return m_prefix_chars.find(c) != std::string::npos;
}; };
@ -1293,24 +1289,28 @@ private:
// i.e., the argument must start with 2 prefix chars, e.g, // i.e., the argument must start with 2 prefix chars, e.g,
// '--foo' e,g, './test --foo=Bar -DARG=yes' // '--foo' e,g, './test --foo=Bar -DARG=yes'
if (a.size() > 1) { if (a.size() > 1) {
if (legal_prefix(a[0]) && legal_prefix(a[1])) { return (legal_prefix(a[0]) && legal_prefix(a[1]));
return true;
}
} }
} }
} }
return false; return false;
}; };
// Check that:
// - We don't have an argument named exactly this
// - The argument starts with a prefix char, e.g., "--"
// - The argument contains an assign char, e.g., "="
auto assign_char_pos = arg.find_first_of(m_assign_chars);
if (m_argument_map.find(arg) == m_argument_map.end() && if (m_argument_map.find(arg) == m_argument_map.end() &&
argument_starts_with_prefix_chars(arg) && argument_starts_with_prefix_chars(arg) &&
eqpos != std::string::npos) { assign_char_pos != std::string::npos) {
// Get the name of the potential option, and check it exists // Get the name of the potential option, and check it exists
std::string opt_name = arg.substr(0, eqpos); std::string opt_name = arg.substr(0, assign_char_pos);
if (m_argument_map.find(opt_name) != m_argument_map.end()) { if (m_argument_map.find(opt_name) != m_argument_map.end()) {
// This is the name of an option! Split it into two parts // This is the name of an option! Split it into two parts
arguments.push_back(std::move(opt_name)); arguments.push_back(std::move(opt_name));
arguments.push_back(arg.substr(eqpos + 1)); arguments.push_back(arg.substr(assign_char_pos + 1));
continue; continue;
} }
} }