1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 20:53:52 +00:00

Adding short_circuit to simplify parse procedure

This commit is contained in:
Henry Fredrick Schreiner 2018-05-07 12:38:51 +02:00 committed by Henry Schreiner
parent 6a6d64581d
commit aac957507d
2 changed files with 26 additions and 11 deletions

View File

@ -389,7 +389,7 @@ class App {
return opt; return opt;
} }
/// Set a help flag, replaced the existing one if present /// Set a help flag, replace the existing one if present
Option *set_help_flag(std::string name = "", std::string description = "") { Option *set_help_flag(std::string name = "", std::string description = "") {
if(help_ptr_ != nullptr) { if(help_ptr_ != nullptr) {
remove_option(help_ptr_); remove_option(help_ptr_);
@ -398,7 +398,8 @@ class App {
// Empty name will simply remove the help flag // Empty name will simply remove the help flag
if(!name.empty()) { if(!name.empty()) {
help_ptr_ = add_flag(name, description); help_ptr_ = add_flag_function(name, [](size_t) -> void { throw CallForHelp(); }, description);
help_ptr_->short_circuit(true);
help_ptr_->configurable(false); help_ptr_->configurable(false);
} }
@ -412,9 +413,10 @@ class App {
help_all_ptr_ = nullptr; help_all_ptr_ = nullptr;
} }
// Empty name will simply remove the help flag // Empty name will simply remove the help all flag
if(!name.empty()) { if(!name.empty()) {
help_all_ptr_ = add_flag(name, description); help_all_ptr_ = add_flag_function(name, [](size_t) -> void { throw CallForAllHelp(); }, description);
help_all_ptr_->short_circuit(true);
help_all_ptr_->configurable(false); help_all_ptr_->configurable(false);
} }
@ -1281,13 +1283,9 @@ class App {
_parse_single(args, positional_only); _parse_single(args, positional_only);
} }
if(help_ptr_ != nullptr && help_ptr_->count() > 0) { for(const Option_p &opt : options_)
throw CallForHelp(); if(opt->get_short_circuit() && opt->count() > 0)
} opt->run_callback();
if(help_all_ptr_ != nullptr && help_all_ptr_->count() > 0) {
throw CallForAllHelp();
}
// Process an INI file // Process an INI file
if(config_ptr_ != nullptr) { if(config_ptr_ != nullptr) {

View File

@ -209,6 +209,9 @@ class Option : public OptionBase<Option> {
/// Options store a callback to do all the work /// Options store a callback to do all the work
callback_t callback_; callback_t callback_;
/// Options can short-circuit for help options or similar (called before parsing is validated)
bool short_circuit_{false};
///@} ///@}
/// @name Parsing results /// @name Parsing results
///@{ ///@{
@ -383,6 +386,14 @@ class Option : public OptionBase<Option> {
return this; return this;
} }
/// Options with a short circuit set will run this function before parsing is finished.
///
/// This is set on help functions, for example, to escape the normal validation.
Option *short_circuit(bool value = true) {
short_circuit_ = value;
return this;
}
///@} ///@}
/// @name Accessors /// @name Accessors
///@{ ///@{
@ -402,6 +413,12 @@ class Option : public OptionBase<Option> {
/// The default value (for help printing) /// The default value (for help printing)
std::string get_defaultval() const { return defaultval_; } std::string get_defaultval() const { return defaultval_; }
/// See if this is supposed to short circuit (skip validation, INI, etc) (Used for help flags)
bool get_short_circuit() const { return short_circuit_; }
/// Get the callback function
callback_t get_callback() const { return callback_; }
/// Get the long names /// Get the long names
const std::vector<std::string> get_lnames() const { return lnames_; } const std::vector<std::string> get_lnames() const { return lnames_; }