Skip to content

Commit

Permalink
Closes #113, add custom std::ostream& argument to ArgumentParser cons…
Browse files Browse the repository at this point in the history
…tructor
  • Loading branch information
p-ranav committed Nov 4, 2023
1 parent 7a3e0f1 commit 281f1ab
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
9 changes: 5 additions & 4 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1361,14 +1361,15 @@ class ArgumentParser {
explicit ArgumentParser(std::string program_name = {},
std::string version = "1.0",
default_arguments add_args = default_arguments::all,
bool exit_on_default_arguments = true)
bool exit_on_default_arguments = true,
std::ostream &os = std::cout)
: m_program_name(std::move(program_name)), m_version(std::move(version)),
m_exit_on_default_arguments(exit_on_default_arguments),
m_parser_path(m_program_name) {
if ((add_args & default_arguments::help) == default_arguments::help) {
add_argument("-h", "--help")
.action([&](const auto & /*unused*/) {
std::cout << help().str();
os << help().str();
if (m_exit_on_default_arguments) {
std::exit(0);
}
Expand All @@ -1381,7 +1382,7 @@ class ArgumentParser {
if ((add_args & default_arguments::version) == default_arguments::version) {
add_argument("-v", "--version")
.action([&](const auto & /*unused*/) {
std::cout << m_version << std::endl;
os << m_version << std::endl;
if (m_exit_on_default_arguments) {
std::exit(0);
}
Expand Down Expand Up @@ -1845,7 +1846,7 @@ class ArgumentParser {
if (m_positional_arguments.empty()) {

// Ask the user if they argument they provided was a typo
// for some sub-parser,
// for some sub-parser,
// e.g., user provided `git totes` instead of `git notes`
if (!m_subparser_map.empty()) {
throw std::runtime_error(
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
test_repr.cpp
test_required_arguments.cpp
test_scan.cpp
test_stringstream.cpp
test_value_semantics.cpp
test_version.cpp
test_subparsers.cpp
Expand Down
3 changes: 2 additions & 1 deletion test/test_error_reporting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ TEST_CASE("Missing optional argument name" * test_suite("error_reporting")) {
}
}

TEST_CASE("Missing optional argument name (some flag arguments)" * test_suite("error_reporting")) {
TEST_CASE("Missing optional argument name (some flag arguments)" *
test_suite("error_reporting")) {
argparse::ArgumentParser parser("test");
parser.add_argument("-a").flag();
parser.add_argument("-b").flag();
Expand Down
20 changes: 20 additions & 0 deletions test/test_stringstream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>

#include <cmath>
#include <string>
#include <vector>

using doctest::test_suite;

TEST_CASE("Get Version String" * test_suite("stringstream")) {
std::stringstream os;
argparse::ArgumentParser program("test", "1.0",
argparse::default_arguments::all, false, os);
program.parse_args({"test", "--version"});
REQUIRE(os.str() == "1.0\n");
}

0 comments on commit 281f1ab

Please sign in to comment.