mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Added std map in argparser to keep a dictionary of arguments by name. Faster checking when calling parse_args
This commit is contained in:
parent
bacc8dc39b
commit
bb06497e88
@ -31,14 +31,16 @@ struct Argument {
|
|||||||
std::function<std::any(const std::string&)> mAction;
|
std::function<std::any(const std::string&)> mAction;
|
||||||
std::any mValue;
|
std::any mValue;
|
||||||
std::string mRawValue;
|
std::string mRawValue;
|
||||||
|
size_t mNumArgs;
|
||||||
|
|
||||||
Argument() :
|
Argument() :
|
||||||
mNames({}),
|
mNames({}),
|
||||||
mHelp(""),
|
mHelp(""),
|
||||||
mDefaultValue(nullptr),
|
mDefaultValue(nullptr),
|
||||||
mAction(nullptr),
|
mAction([](const std::string& aValue) { return aValue; }),
|
||||||
mValue(nullptr),
|
mValue(nullptr),
|
||||||
mRawValue("") {}
|
mRawValue(""),
|
||||||
|
mNumArgs(1) {}
|
||||||
|
|
||||||
Argument& help(const std::string& aHelp) {
|
Argument& help(const std::string& aHelp) {
|
||||||
mHelp = aHelp;
|
mHelp = aHelp;
|
||||||
@ -55,6 +57,11 @@ struct Argument {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Argument& nargs(size_t aNumArgs) {
|
||||||
|
mNumArgs = aNumArgs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T get() {
|
T get() {
|
||||||
if (!mValue.has_value()) {
|
if (!mValue.has_value()) {
|
||||||
@ -117,6 +124,32 @@ class ArgumentParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parse_args_2(int argc, char * argv[]) {
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
auto tCurrentArgument = argv[i];
|
||||||
|
std::map<std::string, std::shared_ptr<Argument>>::iterator tIterator = mArgumentMap.find(argv[i]);
|
||||||
|
if (tIterator != mArgumentMap.end()) {
|
||||||
|
auto tArgument = tIterator->second;
|
||||||
|
auto tCount = tArgument->mNumArgs;
|
||||||
|
while (tCount > 0) {
|
||||||
|
i = i + 1;
|
||||||
|
if (i < argc) {
|
||||||
|
tArgument->mRawValue = argv[i];
|
||||||
|
if (tArgument->mAction != nullptr)
|
||||||
|
tArgument->mValue = tArgument->mAction(argv[i]);
|
||||||
|
else {
|
||||||
|
if (tArgument->mDefaultValue != nullptr)
|
||||||
|
tArgument->mValue = tArgument->mDefaultValue();
|
||||||
|
else
|
||||||
|
tArgument->mValue = std::string(argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tCount -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Argument& operator[](const char * key) {
|
Argument& operator[](const char * key) {
|
||||||
for (auto& tArgument : mArguments) {
|
for (auto& tArgument : mArguments) {
|
||||||
auto tIndex = std::find(tArgument->mNames.begin(), tArgument->mNames.end(), key);
|
auto tIndex = std::find(tArgument->mNames.begin(), tArgument->mNames.end(), key);
|
||||||
|
@ -11,12 +11,13 @@ int main(int argc, char * argv[]) {
|
|||||||
|
|
||||||
program.add_argument("-n", "--num_iterations")
|
program.add_argument("-n", "--num_iterations")
|
||||||
.help("Number of iterations")
|
.help("Number of iterations")
|
||||||
|
.nargs(2)
|
||||||
.action([](const std::string& value) { return std::stoi(value); });
|
.action([](const std::string& value) { return std::stoi(value); });
|
||||||
|
|
||||||
program.add_argument("-v", "--verbose", "VERBOSE")
|
program.add_argument("-v", "--verbose", "VERBOSE")
|
||||||
.default_value([]() { return true; });
|
.default_value([]() { return true; });
|
||||||
|
|
||||||
program.parse_args(argc, argv);
|
program.parse_args_2(argc, argv);
|
||||||
|
|
||||||
auto config_file = program.get<std::string>("--config");
|
auto config_file = program.get<std::string>("--config");
|
||||||
auto num_iters = program.get<int>("-n");
|
auto num_iters = program.get<int>("-n");
|
||||||
|
Loading…
Reference in New Issue
Block a user