mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Merge pull request #71 from lichray/simplify-misc
Simplify a few internals
This commit is contained in:
commit
c13e83d0b3
@ -73,11 +73,6 @@ struct is_container<
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
static constexpr bool is_container_v = is_container<T>::value;
|
static constexpr bool is_container_v = is_container<T>::value;
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct is_string_like
|
|
||||||
: std::conjunction<std::is_constructible<std::string, T>,
|
|
||||||
std::is_convertible<T, std::string_view>> {};
|
|
||||||
|
|
||||||
template <typename T> constexpr bool standard_signed_integer = false;
|
template <typename T> constexpr bool standard_signed_integer = false;
|
||||||
template <> constexpr bool standard_signed_integer<signed char> = true;
|
template <> constexpr bool standard_signed_integer<signed char> = true;
|
||||||
template <> constexpr bool standard_signed_integer<short int> = true;
|
template <> constexpr bool standard_signed_integer<short int> = true;
|
||||||
@ -271,10 +266,10 @@ class Argument {
|
|||||||
-> std::ostream &;
|
-> std::ostream &;
|
||||||
|
|
||||||
template <size_t N, size_t... I>
|
template <size_t N, size_t... I>
|
||||||
explicit Argument(std::string(&&a)[N], std::index_sequence<I...>)
|
explicit Argument(std::string_view(&&a)[N], std::index_sequence<I...>)
|
||||||
: mIsOptional((is_optional(a[I]) || ...)), mIsRequired(false),
|
: mIsOptional((is_optional(a[I]) || ...)), mIsRequired(false),
|
||||||
mIsUsed(false) {
|
mIsUsed(false) {
|
||||||
((void)mNames.push_back(std::move(a[I])), ...);
|
((void)mNames.emplace_back(a[I]), ...);
|
||||||
std::sort(
|
std::sort(
|
||||||
mNames.begin(), mNames.end(), [](const auto &lhs, const auto &rhs) {
|
mNames.begin(), mNames.end(), [](const auto &lhs, const auto &rhs) {
|
||||||
return lhs.size() == rhs.size() ? lhs < rhs : lhs.size() < rhs.size();
|
return lhs.size() == rhs.size() ? lhs < rhs : lhs.size() < rhs.size();
|
||||||
@ -282,14 +277,9 @@ class Argument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Argument() = default;
|
template <size_t N>
|
||||||
|
explicit Argument(std::string_view(&&a)[N])
|
||||||
template <typename... Args,
|
: Argument(std::move(a), std::make_index_sequence<N>{}) {}
|
||||||
std::enable_if_t<
|
|
||||||
std::conjunction_v<details::is_string_like<Args>...>, int> = 0>
|
|
||||||
explicit Argument(Args &&... args)
|
|
||||||
: Argument({std::string(std::forward<Args>(args))...},
|
|
||||||
std::make_index_sequence<sizeof...(Args)>{}) {}
|
|
||||||
|
|
||||||
Argument &help(std::string aHelp) {
|
Argument &help(std::string aHelp) {
|
||||||
mHelp = std::move(aHelp);
|
mHelp = std::move(aHelp);
|
||||||
@ -383,12 +373,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Iterator>
|
template <typename Iterator>
|
||||||
Iterator consume(Iterator start, Iterator end, std::string usedName = {}) {
|
Iterator consume(Iterator start, Iterator end,
|
||||||
|
std::string_view usedName = {}) {
|
||||||
if (mIsUsed) {
|
if (mIsUsed) {
|
||||||
throw std::runtime_error("Duplicate argument");
|
throw std::runtime_error("Duplicate argument");
|
||||||
}
|
}
|
||||||
mIsUsed = true;
|
mIsUsed = true;
|
||||||
mUsedName = std::move(usedName);
|
mUsedName = usedName;
|
||||||
if (mNumArgs == 0) {
|
if (mNumArgs == 0) {
|
||||||
mValues.emplace_back(mImplicitValue);
|
mValues.emplace_back(mImplicitValue);
|
||||||
return start;
|
return start;
|
||||||
@ -731,7 +722,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> mNames;
|
std::vector<std::string> mNames;
|
||||||
std::string mUsedName;
|
std::string_view mUsedName;
|
||||||
std::string mHelp;
|
std::string mHelp;
|
||||||
std::any mDefaultValue;
|
std::any mDefaultValue;
|
||||||
std::any mImplicitValue;
|
std::any mImplicitValue;
|
||||||
@ -780,8 +771,9 @@ public:
|
|||||||
// Parameter packing
|
// Parameter packing
|
||||||
// Call add_argument with variadic number of string arguments
|
// Call add_argument with variadic number of string arguments
|
||||||
template <typename... Targs> Argument &add_argument(Targs... Fargs) {
|
template <typename... Targs> Argument &add_argument(Targs... Fargs) {
|
||||||
|
using array_of_sv = std::string_view[sizeof...(Targs)];
|
||||||
auto tArgument = mOptionalArguments.emplace(cend(mOptionalArguments),
|
auto tArgument = mOptionalArguments.emplace(cend(mOptionalArguments),
|
||||||
std::move(Fargs)...);
|
array_of_sv{Fargs...});
|
||||||
|
|
||||||
if (!tArgument->mIsOptional)
|
if (!tArgument->mIsOptional)
|
||||||
mPositionalArguments.splice(cend(mPositionalArguments),
|
mPositionalArguments.splice(cend(mPositionalArguments),
|
||||||
@ -941,7 +933,7 @@ private:
|
|||||||
std::exit(0);
|
std::exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
it = tArgument->consume(std::next(it), end, tCurrentArgument);
|
it = tArgument->consume(std::next(it), end, tIterator->first);
|
||||||
} else if (const auto &tCompoundArgument = tCurrentArgument;
|
} else if (const auto &tCompoundArgument = tCurrentArgument;
|
||||||
tCompoundArgument.size() > 1 && tCompoundArgument[0] == '-' &&
|
tCompoundArgument.size() > 1 && tCompoundArgument[0] == '-' &&
|
||||||
tCompoundArgument[1] != '-') {
|
tCompoundArgument[1] != '-') {
|
||||||
@ -951,7 +943,7 @@ private:
|
|||||||
auto tIterator2 = mArgumentMap.find(tHypotheticalArgument);
|
auto tIterator2 = mArgumentMap.find(tHypotheticalArgument);
|
||||||
if (tIterator2 != mArgumentMap.end()) {
|
if (tIterator2 != mArgumentMap.end()) {
|
||||||
auto tArgument = tIterator2->second;
|
auto tArgument = tIterator2->second;
|
||||||
it = tArgument->consume(it, end, tHypotheticalArgument);
|
it = tArgument->consume(it, end, tIterator2->first);
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("Unknown argument");
|
throw std::runtime_error("Unknown argument");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user