mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Merge pull request #332 from rouault/consume_dry_run
Add a dry_run argument to Argument::consume(), and change ArgumentParser private section to protected
This commit is contained in:
commit
0fa7d59964
@ -897,11 +897,16 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The dry_run parameter can be set to true to avoid running the actions,
|
||||||
|
* and setting m_is_used. This may be used by a pre-processing step to do
|
||||||
|
* a first iteration over arguments.
|
||||||
|
*/
|
||||||
template <typename Iterator>
|
template <typename Iterator>
|
||||||
Iterator consume(Iterator start, Iterator end,
|
Iterator consume(Iterator start, Iterator end,
|
||||||
std::string_view used_name = {}) {
|
std::string_view used_name = {}, bool dry_run = false) {
|
||||||
if (!m_is_repeatable && m_is_used) {
|
if (!m_is_repeatable && m_is_used) {
|
||||||
throw std::runtime_error("Duplicate argument");
|
throw std::runtime_error(
|
||||||
|
std::string("Duplicate argument ").append(used_name));
|
||||||
}
|
}
|
||||||
m_used_name = used_name;
|
m_used_name = used_name;
|
||||||
|
|
||||||
@ -923,9 +928,11 @@ public:
|
|||||||
const auto num_args_min = m_num_args_range.get_min();
|
const auto num_args_min = m_num_args_range.get_min();
|
||||||
std::size_t dist = 0;
|
std::size_t dist = 0;
|
||||||
if (num_args_max == 0) {
|
if (num_args_max == 0) {
|
||||||
m_values.emplace_back(m_implicit_value);
|
if (!dry_run) {
|
||||||
std::visit([](const auto &f) { f({}); }, m_action);
|
m_values.emplace_back(m_implicit_value);
|
||||||
m_is_used = true;
|
std::visit([](const auto &f) { f({}); }, m_action);
|
||||||
|
m_is_used = true;
|
||||||
|
}
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
if ((dist = static_cast<std::size_t>(std::distance(start, end))) >=
|
if ((dist = static_cast<std::size_t>(std::distance(start, end))) >=
|
||||||
@ -962,12 +969,16 @@ public:
|
|||||||
Iterator first, last;
|
Iterator first, last;
|
||||||
Argument &self;
|
Argument &self;
|
||||||
};
|
};
|
||||||
std::visit(ActionApply{start, end, *this}, m_action);
|
if (!dry_run) {
|
||||||
m_is_used = true;
|
std::visit(ActionApply{start, end, *this}, m_action);
|
||||||
|
m_is_used = true;
|
||||||
|
}
|
||||||
return end;
|
return end;
|
||||||
}
|
}
|
||||||
if (m_default_value.has_value()) {
|
if (m_default_value.has_value()) {
|
||||||
m_is_used = true;
|
if (!dry_run) {
|
||||||
|
m_is_used = true;
|
||||||
|
}
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
throw std::runtime_error("Too few arguments for '" +
|
throw std::runtime_error("Too few arguments for '" +
|
||||||
@ -1162,6 +1173,31 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* positional:
|
||||||
|
* _empty_
|
||||||
|
* '-'
|
||||||
|
* '-' decimal-literal
|
||||||
|
* !'-' anything
|
||||||
|
*/
|
||||||
|
static bool is_positional(std::string_view name,
|
||||||
|
std::string_view prefix_chars) {
|
||||||
|
auto first = lookahead(name);
|
||||||
|
|
||||||
|
if (first == eof) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (prefix_chars.find(static_cast<char>(first)) !=
|
||||||
|
std::string_view::npos) {
|
||||||
|
name.remove_prefix(1);
|
||||||
|
if (name.empty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return is_decimal_literal(name);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class NArgsRange {
|
class NArgsRange {
|
||||||
std::size_t m_min;
|
std::size_t m_min;
|
||||||
@ -1397,30 +1433,6 @@ private:
|
|||||||
return !is_positional(name, prefix_chars);
|
return !is_positional(name, prefix_chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* positional:
|
|
||||||
* _empty_
|
|
||||||
* '-'
|
|
||||||
* '-' decimal-literal
|
|
||||||
* !'-' anything
|
|
||||||
*/
|
|
||||||
static bool is_positional(std::string_view name,
|
|
||||||
std::string_view prefix_chars) {
|
|
||||||
auto first = lookahead(name);
|
|
||||||
|
|
||||||
if (first == eof) {
|
|
||||||
return true;
|
|
||||||
} else if (prefix_chars.find(static_cast<char>(first)) !=
|
|
||||||
std::string_view::npos) {
|
|
||||||
name.remove_prefix(1);
|
|
||||||
if (name.empty()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return is_decimal_literal(name);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get argument value given a type
|
* Get argument value given a type
|
||||||
* @throws std::logic_error in case of incompatible types
|
* @throws std::logic_error in case of incompatible types
|
||||||
@ -1966,7 +1978,7 @@ public:
|
|||||||
|
|
||||||
void set_suppress(bool suppress) { m_suppress = suppress; }
|
void set_suppress(bool suppress) { m_suppress = suppress; }
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
bool is_valid_prefix_char(char c) const {
|
bool is_valid_prefix_char(char c) const {
|
||||||
return m_prefix_chars.find(c) != std::string::npos;
|
return m_prefix_chars.find(c) != std::string::npos;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user