Simplify creating of help option

This commit is contained in:
Stephan van Veen 2019-05-12 14:46:24 +02:00
parent ecf8286c9e
commit 84adf5b2df

View File

@ -291,6 +291,10 @@ public:
size_t mNumArgs = 1; size_t mNumArgs = 1;
bool mIsOptional = false; bool mIsOptional = false;
bool mIsUsed = false; // relevant for optional arguments. True if used by user bool mIsUsed = false; // relevant for optional arguments. True if used by user
public:
static constexpr auto mHelpOption = "-h";
static constexpr auto mHelpOptionLong = "--help";
}; };
class ArgumentParser { class ArgumentParser {
@ -298,14 +302,11 @@ class ArgumentParser {
explicit ArgumentParser(std::string aProgramName = {}) : explicit ArgumentParser(std::string aProgramName = {}) :
mProgramName(std::move(aProgramName)) mProgramName(std::move(aProgramName))
{ {
std::shared_ptr<Argument> tArgument = std::make_shared<Argument>("-h", "--help"); add_argument(Argument::mHelpOption, Argument::mHelpOptionLong)
tArgument->mHelp = "show this help message and exit"; .help("show this help message and exit")
tArgument->mNumArgs = 0; .nargs(0)
tArgument->mDefaultValue = false; .default_value(false)
tArgument->mImplicitValue = true; .implicit_value(true);
mOptionalArguments.emplace_back(tArgument);
mArgumentMap.insert_or_assign(std::string("-h"), tArgument);
mArgumentMap.insert_or_assign(std::string("--help"), tArgument);
} }
// Parameter packing // Parameter packing
@ -505,7 +506,7 @@ class ArgumentParser {
mProgramName = argv[0]; mProgramName = argv[0];
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
auto tCurrentArgument = std::string(argv[i]); auto tCurrentArgument = std::string(argv[i]);
if (tCurrentArgument == "-h" || tCurrentArgument == "--help") { if (tCurrentArgument == Argument::mHelpOption || tCurrentArgument == Argument::mHelpOptionLong) {
throw std::runtime_error("help called"); throw std::runtime_error("help called");
} }
auto tIterator = mArgumentMap.find(argv[i]); auto tIterator = mArgumentMap.find(argv[i]);