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:
parent
6a6d64581d
commit
aac957507d
@ -389,7 +389,7 @@ class App {
|
||||
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 = "") {
|
||||
if(help_ptr_ != nullptr) {
|
||||
remove_option(help_ptr_);
|
||||
@ -398,7 +398,8 @@ class App {
|
||||
|
||||
// Empty name will simply remove the help flag
|
||||
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);
|
||||
}
|
||||
|
||||
@ -412,9 +413,10 @@ class App {
|
||||
help_all_ptr_ = nullptr;
|
||||
}
|
||||
|
||||
// Empty name will simply remove the help flag
|
||||
// Empty name will simply remove the help all flag
|
||||
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);
|
||||
}
|
||||
|
||||
@ -1281,13 +1283,9 @@ class App {
|
||||
_parse_single(args, positional_only);
|
||||
}
|
||||
|
||||
if(help_ptr_ != nullptr && help_ptr_->count() > 0) {
|
||||
throw CallForHelp();
|
||||
}
|
||||
|
||||
if(help_all_ptr_ != nullptr && help_all_ptr_->count() > 0) {
|
||||
throw CallForAllHelp();
|
||||
}
|
||||
for(const Option_p &opt : options_)
|
||||
if(opt->get_short_circuit() && opt->count() > 0)
|
||||
opt->run_callback();
|
||||
|
||||
// Process an INI file
|
||||
if(config_ptr_ != nullptr) {
|
||||
|
@ -209,6 +209,9 @@ class Option : public OptionBase<Option> {
|
||||
/// Options store a callback to do all the work
|
||||
callback_t callback_;
|
||||
|
||||
/// Options can short-circuit for help options or similar (called before parsing is validated)
|
||||
bool short_circuit_{false};
|
||||
|
||||
///@}
|
||||
/// @name Parsing results
|
||||
///@{
|
||||
@ -383,6 +386,14 @@ class Option : public OptionBase<Option> {
|
||||
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
|
||||
///@{
|
||||
@ -402,6 +413,12 @@ class Option : public OptionBase<Option> {
|
||||
/// The default value (for help printing)
|
||||
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
|
||||
const std::vector<std::string> get_lnames() const { return lnames_; }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user