Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default --help to print help and exit #61

Merged
merged 1 commit into from
Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions include/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SOFTWARE.
#pragma once
#include <algorithm>
#include <any>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <iterator>
Expand Down Expand Up @@ -372,20 +373,15 @@ class Argument {
bool mIsOptional : 1;
bool mIsRequired : 1;
bool mIsUsed : 1; // True if the optional argument is used by user

static constexpr auto mHelpOption = "-h";
static constexpr auto mHelpOptionLong = "--help";
};

class ArgumentParser {
public:
explicit ArgumentParser(std::string aProgramName = {})
: mProgramName(std::move(aProgramName)) {
add_argument(Argument::mHelpOption, Argument::mHelpOptionLong)
add_argument("-h", "--help")
.help("show this help message and exit")
.nargs(0)
.default_value(false)
.implicit_value(true);
.nargs(0);
}

ArgumentParser(ArgumentParser &&) noexcept = default;
Expand Down Expand Up @@ -542,20 +538,26 @@ class ArgumentParser {
auto positionalArgumentIt = std::begin(mPositionalArguments);
for (auto it = std::next(std::begin(aArguments)); it != end;) {
const auto &tCurrentArgument = *it;
if (tCurrentArgument == Argument::mHelpOption ||
tCurrentArgument == Argument::mHelpOptionLong) {
throw std::runtime_error("help called");
}
if (Argument::is_positional(tCurrentArgument)) {
if (positionalArgumentIt == std::end(mPositionalArguments)) {
throw std::runtime_error(
"Maximum number of positional arguments exceeded");
}
auto tArgument = positionalArgumentIt++;
it = tArgument->consume(it, end);
} else if (auto tIterator = mArgumentMap.find(tCurrentArgument);
tIterator != mArgumentMap.end()) {
continue;
}

auto tIterator = mArgumentMap.find(tCurrentArgument);
if (tIterator != mArgumentMap.end()) {
auto tArgument = tIterator->second;

// the first optional argument is --help
if (tArgument == mOptionalArguments.begin()) {
std::cout << *this;
std::exit(0);
}

it = tArgument->consume(std::next(it), end, tCurrentArgument);
} else if (const auto &tCompoundArgument = tCurrentArgument;
tCompoundArgument.size() > 1 && tCompoundArgument[0] == '-' &&
Expand Down Expand Up @@ -607,7 +609,7 @@ class ArgumentParser {

void index_argument(list_iterator argIt) {
for (auto &mName : std::as_const(argIt->mNames))
mArgumentMap.emplace(mName, argIt);
mArgumentMap.insert_or_assign(mName, argIt);
}

std::string mProgramName;
Expand Down
24 changes: 24 additions & 0 deletions test/test_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,27 @@ TEST_CASE("Users can format help message" * test_suite("help")) {
auto msg = program.help().str();
REQUIRE(msg == s.str());
}

TEST_CASE("Users can override the help options" * test_suite("help")) {
GIVEN("a program that meant to take -h as a normal option") {
argparse::ArgumentParser program("test");
program.add_argument("input");
program.add_argument("-h").implicit_value('h').default_value('x');

WHEN("provided -h without fulfilling other requirements") {
THEN("validation fails") {
REQUIRE_THROWS_AS(program.parse_args({"test", "-h"}),
std::runtime_error);
}
}

WHEN("provided arguments to all parameters") {
program.parse_args({"test", "-h", "some input"});

THEN("these parameters get their values") {
REQUIRE(program["-h"] == 'h');
REQUIRE(program.get("input") == "some input");
}
}
}
}