Skip to content

Commit

Permalink
Adding a few tools to make manual validation easier
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed May 4, 2018
1 parent f7d51f6 commit 799bdd1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
22 changes: 16 additions & 6 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <memory>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <iterator>

// CLI Library includes
#include "CLI/Error.hpp"
#include "CLI/FormatterFwd.hpp"
#include "CLI/Ini.hpp"
#include "CLI/Macros.hpp"
#include "CLI/Option.hpp"
#include "CLI/Split.hpp"
#include "CLI/StringTools.hpp"
#include "CLI/TypeTools.hpp"
#include "CLI/FormatterFwd.hpp"

namespace CLI {

Expand Down Expand Up @@ -1096,6 +1096,16 @@ class App {
return options;
}

/// Get an option by name
const Option *get_option(std::string name) const {
for(const Option_p &opt : options_) {
if(opt->check_name(name)) {
return opt.get();
}
}
throw OptionNotFound(name);
}

/// Check the status of ignore_case
bool get_ignore_case() const { return ignore_case_; }

Expand Down Expand Up @@ -1404,28 +1414,28 @@ class App {
if(!op->get_configurable())
throw INIError::NotConfigurable(current.fullname);

if(op->results_.empty()) {
if(op->empty()) {
// Flag parsing
if(op->get_type_size() == 0) {
if(current.inputs.size() == 1) {
std::string val = current.inputs.at(0);
val = detail::to_lower(val);
if(val == "true" || val == "on" || val == "yes")
op->results_ = {""};
op->set_results({""});
else if(val == "false" || val == "off" || val == "no")
;
else
try {
size_t ui = std::stoul(val);
for(size_t i = 0; i < ui; i++)
op->results_.emplace_back("");
op->add_result("");
} catch(const std::invalid_argument &) {
throw ConversionError::TrueFalse(current.fullname);
}
} else
throw ConversionError::TooManyInputsFlag(current.fullname);
} else {
op->results_ = current.inputs;
op->set_results(current.inputs);
op->run_callback();
}
}
Expand Down
13 changes: 11 additions & 2 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ class Option : public OptionBase<Option> {
/// Count the total number of times an option was passed
size_t count() const { return results_.size(); }

/// True if the option was not passed
size_t empty() const { return results_.empty(); }

/// This class is true if option is passed.
operator bool() const { return count() > 0; }
operator bool() const { return !empty(); }

/// Clear the parsed results (mostly for testing)
void clear() { results_.clear(); }
Expand Down Expand Up @@ -593,12 +596,18 @@ class Option : public OptionBase<Option> {
return std::find(std::begin(lnames_), std::end(lnames_), name) != std::end(lnames_);
}

/// Puts a result at the end, unless last_ is set, in which case it just keeps the last one
/// Puts a result at the end
void add_result(std::string s) {
results_.push_back(s);
callback_run_ = false;
}

/// Set the results vector all at once
void set_results(std::vector<std::string> results) {
results_ = results;
callback_run_ = false;
}

/// Get a copy of the results
std::vector<std::string> results() const { return results_; }

Expand Down

0 comments on commit 799bdd1

Please sign in to comment.