Skip to content

Commit

Permalink
Added help print and test for requires, envname, and excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Feb 12, 2017
1 parent b3ce333 commit f4d8fd0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
44 changes: 28 additions & 16 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,21 @@ class Option {
/// The first half of the help print, name plus default, etc
std::string help_name() const {
std::stringstream out;
out << get_name(true);
out << get_name(true) << _help_aftername();
return out.str();
}

/// pname with type info
std::string help_pname() const {
std::stringstream out;
out << get_pname() << _help_aftername();
return out.str();
}

/// This is the part after the name is printed but before the description
std::string _help_aftername() const {
std::stringstream out;

if(get_expected() != 0) {
if(typeval != "")
out << " " << typeval;
Expand All @@ -286,22 +300,20 @@ class Option {
if(get_expected() == -1)
out << " ...";
}
if(_envname != "")
out << " (env:" << _envname << ")";
if(_requires.size() > 0) {
out << " Requires:";
for(const Option* opt : _requires)
out << " " << opt->get_name();
}
if(_excludes.size() > 0) {
out << " Excludes:";
for(const Option* opt : _excludes)
out << " " << opt->get_name();
}
return out.str();
}

/// pname with type info
std::string help_pname() const {
std::stringstream out;
out << get_pname();
if(typeval != "")
out << " " << typeval;
if(defaultval != "")
out << "=" << defaultval;
if(get_expected() > 1)
out << " x " << get_expected();
if(get_expected() == -1)
out << " ...";
return out.str();

}

/// Produce a flattened vector of results, vs. a vector of vectors.
Expand Down
21 changes: 20 additions & 1 deletion tests/AppTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "app_helper.hpp"
#include <stdlib.h>

TEST_F(TApp, OneFlagShort) {
app.add_flag("-c,--count");
Expand Down Expand Up @@ -487,6 +488,24 @@ TEST_F(TApp, RequiresChainedFlags) {
EXPECT_NO_THROW(run());
}

TEST_F(TApp, Env) {

setenv("CLI11_TEST_ENV_TMP", "2", true);

int val=1;
CLI::Option* vopt = app.add_option("--tmp", val)->envname("CLI11_TEST_ENV_TMP");

EXPECT_NO_THROW(run());

EXPECT_EQ(2, val);
EXPECT_EQ(1, vopt->count());

app.reset();
vopt->required();
EXPECT_NO_THROW(run());

app.reset();
unsetenv("CLI11_TEST_ENV_TMP");
EXPECT_THROW(run(), CLI::RequiredError);
}

// TODO: add tests for envname
34 changes: 34 additions & 0 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,39 @@ TEST(THelp, RequiredPositionalAndOptions) {
EXPECT_THAT(help, HasSubstr("Options:"));
EXPECT_THAT(help, HasSubstr("Positionals:"));
EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] something"));
}

TEST(THelp, EnvName) {
CLI::App app{"My prog"};
std::string input;
app.add_option("--something", input)->envname("SOME_ENV");

std::string help = app.help();

EXPECT_THAT(help, HasSubstr("SOME_ENV"));

}

TEST(THelp, Requires) {
CLI::App app{"My prog"};

CLI::Option* op1 = app.add_flag("--op1");
app.add_flag("--op2")->requires(op1);

std::string help = app.help();

EXPECT_THAT(help, HasSubstr("Requires: --op1"));

}

TEST(THelp, Excludes) {
CLI::App app{"My prog"};

CLI::Option* op1 = app.add_flag("--op1");
app.add_flag("--op2")->excludes(op1);

std::string help = app.help();

EXPECT_THAT(help, HasSubstr("Excludes: --op1"));

}

0 comments on commit f4d8fd0

Please sign in to comment.