Skip to content

Commit

Permalink
Make help string of custom options better readable.
Browse files Browse the repository at this point in the history
Lets say you support a custom option that reads a
std::pair<int, float> with a default value. Then
you use the function set_custom_option("INT FLOAT", 2)
or something like this. This results in a help string
that looks like "-p INT FLOAT=(1, 0.00000) x 2 Some text".
This is highly confusing if you just want to use the
program.

This commit adds a flag to CLI::Option that can be set
via set_custom_option that turn off all additions to the
description. So set_custom_option("INT FLOAT", 2, true)
results in something like "-p INT FLOAT Some text".
  • Loading branch information
AbcAeffchen committed Mar 16, 2018
1 parent b4b7d99 commit 1561a1a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ class Option : public OptionBase<Option> {
/// The number of expected values, 0 for flag, -1 for unlimited vector
int expected_{1};

/// A flag to signal if the help string needs special treatment.
bool custom_{false};

/// A private setting to allow args to not be able to accept incorrect expected values
bool changeable_{false};

Expand Down Expand Up @@ -371,6 +374,9 @@ class Option : public OptionBase<Option> {
/// The number of arguments the option expects
int get_expected() const { return expected_; }

/// True if the option is a custom option and needs special treatment for the help string
int get_custom() const { return custom_; }

/// True if this has a default value
int get_default() const { return default_; }

Expand Down Expand Up @@ -447,12 +453,14 @@ class Option : public OptionBase<Option> {
if(get_expected() != 0) {
if(!typeval_.empty())
out << " " << typeval_;
if(!defaultval_.empty())
out << "=" << defaultval_;
if(get_expected() > 1)
out << " x " << get_expected();
if(get_expected() == -1)
out << " ...";
if(!get_custom()) {
if(!defaultval_.empty())
out << "=" << defaultval_;
if(get_expected() > 1)
out << " x " << get_expected();
if(get_expected() == -1)
out << " ...";
}
}
if(!envname_.empty())
out << " (env:" << envname_ << ")";
Expand Down Expand Up @@ -584,9 +592,10 @@ class Option : public OptionBase<Option> {
///@{

/// Set a custom option, typestring, expected; locks changeable unless expected is -1
void set_custom_option(std::string typeval, int expected = 1) {
void set_custom_option(std::string typeval, int expected = 1, bool custom = false) {
typeval_ = typeval;
expected_ = expected;
custom_ = custom;
if(expected == 0)
required_ = false;
changeable_ = expected < 0;
Expand Down

0 comments on commit 1561a1a

Please sign in to comment.