CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Option.hpp
1// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// [CLI11:public_includes:set]
12#include <algorithm>
13#include <functional>
14#include <memory>
15#include <set>
16#include <string>
17#include <tuple>
18#include <utility>
19#include <vector>
20// [CLI11:public_includes:end]
21
22#include "Error.hpp"
23#include "Macros.hpp"
24#include "Split.hpp"
25#include "StringTools.hpp"
26#include "Validators.hpp"
27
28namespace CLI {
29// [CLI11:option_hpp:verbatim]
30
31using results_t = std::vector<std::string>;
33using callback_t = std::function<bool(const results_t &)>;
34
35class Option;
36class App;
37
38using Option_p = std::unique_ptr<Option>;
40enum class MultiOptionPolicy : char {
41 Throw,
42 TakeLast,
43 TakeFirst,
44 Join,
45 TakeAll,
46 Sum,
47 Reverse,
48};
49
52template <typename CRTP> class OptionBase {
53 friend App;
54
55 protected:
57 std::string group_ = std::string("Options");
58
60 bool required_{false};
61
63 bool ignore_case_{false};
64
66 bool ignore_underscore_{false};
67
69 bool configurable_{true};
70
73
75 char delimiter_{'\0'};
76
79
81 MultiOptionPolicy multi_option_policy_{MultiOptionPolicy::Throw};
82
84 template <typename T> void copy_to(T *other) const;
85
86 public:
87 // setters
88
90 CRTP *group(const std::string &name) {
91 if(!detail::valid_alias_name_string(name)) {
92 throw IncorrectConstruction("Group names may not contain newlines or null characters");
93 }
94 group_ = name;
95 return static_cast<CRTP *>(this);
96 }
97
99 CRTP *required(bool value = true) {
100 required_ = value;
101 return static_cast<CRTP *>(this);
102 }
103
105 CRTP *mandatory(bool value = true) { return required(value); }
106
107 CRTP *always_capture_default(bool value = true) {
109 return static_cast<CRTP *>(this);
110 }
111
112 // Getters
113
115 CLI11_NODISCARD const std::string &get_group() const { return group_; }
116
118 CLI11_NODISCARD bool get_required() const { return required_; }
119
121 CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
122
124 CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
125
127 CLI11_NODISCARD bool get_configurable() const { return configurable_; }
128
130 CLI11_NODISCARD bool get_disable_flag_override() const { return disable_flag_override_; }
131
133 CLI11_NODISCARD char get_delimiter() const { return delimiter_; }
134
136 CLI11_NODISCARD bool get_always_capture_default() const { return always_capture_default_; }
137
139 CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const { return multi_option_policy_; }
140
141 // Shortcuts for multi option policy
142
144 CRTP *take_last() {
145 auto *self = static_cast<CRTP *>(this);
146 self->multi_option_policy(MultiOptionPolicy::TakeLast);
147 return self;
148 }
149
151 CRTP *take_first() {
152 auto *self = static_cast<CRTP *>(this);
153 self->multi_option_policy(MultiOptionPolicy::TakeFirst);
154 return self;
155 }
156
158 CRTP *take_all() {
159 auto self = static_cast<CRTP *>(this);
160 self->multi_option_policy(MultiOptionPolicy::TakeAll);
161 return self;
162 }
163
165 CRTP *join() {
166 auto *self = static_cast<CRTP *>(this);
167 self->multi_option_policy(MultiOptionPolicy::Join);
168 return self;
169 }
170
172 CRTP *join(char delim) {
173 auto self = static_cast<CRTP *>(this);
174 self->delimiter_ = delim;
175 self->multi_option_policy(MultiOptionPolicy::Join);
176 return self;
177 }
178
180 CRTP *configurable(bool value = true) {
181 configurable_ = value;
182 return static_cast<CRTP *>(this);
183 }
184
186 CRTP *delimiter(char value = '\0') {
187 delimiter_ = value;
188 return static_cast<CRTP *>(this);
189 }
190};
191
194class OptionDefaults : public OptionBase<OptionDefaults> {
195 public:
196 OptionDefaults() = default;
197
198 // Methods here need a different implementation if they are Option vs. OptionDefault
199
201 OptionDefaults *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw) {
202 multi_option_policy_ = value;
203 return this;
204 }
205
207 OptionDefaults *ignore_case(bool value = true) {
208 ignore_case_ = value;
209 return this;
210 }
211
213 OptionDefaults *ignore_underscore(bool value = true) {
214 ignore_underscore_ = value;
215 return this;
216 }
217
221 return this;
222 }
223
225 OptionDefaults *delimiter(char value = '\0') {
226 delimiter_ = value;
227 return this;
228 }
229};
230
231class Option : public OptionBase<Option> {
232 friend App;
233
234 protected:
237
239 std::vector<std::string> snames_{};
240
242 std::vector<std::string> lnames_{};
243
246 std::vector<std::pair<std::string, std::string>> default_flag_values_{};
247
249 std::vector<std::string> fnames_{};
250
252 std::string pname_{};
253
255 std::string envname_{};
256
260
262 std::string description_{};
263
265 std::string default_str_{};
266
268 std::string option_text_{};
269
273 std::function<std::string()> type_name_{[]() { return std::string(); }};
274
276 std::function<std::string()> default_function_{};
277
281
287
292
294 std::vector<Validator> validators_{};
295
297 std::set<Option *> needs_{};
298
300 std::set<Option *> excludes_{};
301
305
307 App *parent_{nullptr};
308
310 callback_t callback_{};
311
315
317 results_t results_{};
319 results_t proc_results_{};
321 enum class option_state : char {
322 parsing = 0,
323 validated = 2,
324 reduced = 4,
325 callback_run = 6,
326 };
330 bool allow_extra_args_{false};
332 bool flag_like_{false};
336 bool inject_separator_{false};
340 bool force_callback_{false};
342
344 Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
345 : description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
346 std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name));
347 }
348
349 public:
352
353 Option(const Option &) = delete;
354 Option &operator=(const Option &) = delete;
355
357 CLI11_NODISCARD std::size_t count() const { return results_.size(); }
358
360 CLI11_NODISCARD bool empty() const { return results_.empty(); }
361
363 explicit operator bool() const { return !empty() || force_callback_; }
364
366 void clear() {
367 results_.clear();
369 }
370
374
376 Option *expected(int value);
377
379 Option *expected(int value_min, int value_max);
380
383 Option *allow_extra_args(bool value = true) {
384 allow_extra_args_ = value;
385 return this;
386 }
388 CLI11_NODISCARD bool get_allow_extra_args() const { return allow_extra_args_; }
390 Option *trigger_on_parse(bool value = true) {
391 trigger_on_result_ = value;
392 return this;
393 }
395 CLI11_NODISCARD bool get_trigger_on_parse() const { return trigger_on_result_; }
396
398 Option *force_callback(bool value = true) {
399 force_callback_ = value;
400 return this;
401 }
403 CLI11_NODISCARD bool get_force_callback() const { return force_callback_; }
404
407 Option *run_callback_for_default(bool value = true) {
409 return this;
410 }
412 CLI11_NODISCARD bool get_run_callback_for_default() const { return run_callback_for_default_; }
413
415 Option *check(Validator validator, const std::string &validator_name = "");
416
418 Option *check(std::function<std::string(const std::string &)> Validator,
419 std::string Validator_description = "",
420 std::string Validator_name = "");
421
423 Option *transform(Validator Validator, const std::string &Validator_name = "");
424
426 Option *transform(const std::function<std::string(std::string)> &func,
427 std::string transform_description = "",
428 std::string transform_name = "");
429
431 Option *each(const std::function<void(std::string)> &func);
432
434 Validator *get_validator(const std::string &Validator_name = "");
435
437 Validator *get_validator(int index);
438
441 if(opt != this) {
442 needs_.insert(opt);
443 }
444 return this;
445 }
446
448 template <typename T = App> Option *needs(std::string opt_name) {
449 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
450 if(opt == nullptr) {
451 throw IncorrectConstruction::MissingOption(opt_name);
452 }
453 return needs(opt);
454 }
455
457 template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
458 needs(opt);
459 return needs(opt1, args...); // NOLINT(readability-suspicious-call-argument)
460 }
461
463 bool remove_needs(Option *opt);
464
466 Option *excludes(Option *opt);
467
469 template <typename T = App> Option *excludes(std::string opt_name) {
470 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
471 if(opt == nullptr) {
472 throw IncorrectConstruction::MissingOption(opt_name);
473 }
474 return excludes(opt);
475 }
476
478 template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
479 excludes(opt);
480 return excludes(opt1, args...);
481 }
482
484 bool remove_excludes(Option *opt);
485
487 Option *envname(std::string name) {
488 envname_ = std::move(name);
489 return this;
490 }
491
496 template <typename T = App> Option *ignore_case(bool value = true);
497
502 template <typename T = App> Option *ignore_underscore(bool value = true);
503
505 Option *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw);
506
508 Option *disable_flag_override(bool value = true) {
510 return this;
511 }
515
517 CLI11_NODISCARD int get_type_size() const { return type_size_min_; }
518
520 CLI11_NODISCARD int get_type_size_min() const { return type_size_min_; }
522 CLI11_NODISCARD int get_type_size_max() const { return type_size_max_; }
523
525 CLI11_NODISCARD bool get_inject_separator() const { return inject_separator_; }
526
528 CLI11_NODISCARD std::string get_envname() const { return envname_; }
529
531 CLI11_NODISCARD std::set<Option *> get_needs() const { return needs_; }
532
534 CLI11_NODISCARD std::set<Option *> get_excludes() const { return excludes_; }
535
537 CLI11_NODISCARD std::string get_default_str() const { return default_str_; }
538
540 CLI11_NODISCARD callback_t get_callback() const { return callback_; }
541
543 CLI11_NODISCARD const std::vector<std::string> &get_lnames() const { return lnames_; }
544
546 CLI11_NODISCARD const std::vector<std::string> &get_snames() const { return snames_; }
547
549 CLI11_NODISCARD const std::vector<std::string> &get_fnames() const { return fnames_; }
551 CLI11_NODISCARD const std::string &get_single_name() const {
552 if(!lnames_.empty()) {
553 return lnames_[0];
554 }
555 if(!snames_.empty()) {
556 return snames_[0];
557 }
558 if(!pname_.empty()) {
559 return pname_;
560 }
561 return envname_;
562 }
564 CLI11_NODISCARD int get_expected() const { return expected_min_; }
565
567 CLI11_NODISCARD int get_expected_min() const { return expected_min_; }
569 CLI11_NODISCARD int get_expected_max() const { return expected_max_; }
570
572 CLI11_NODISCARD int get_items_expected_min() const { return type_size_min_ * expected_min_; }
573
575 CLI11_NODISCARD int get_items_expected_max() const {
576 int t = type_size_max_;
577 return detail::checked_multiply(t, expected_max_) ? t : detail::expected_max_vector_size;
578 }
580 CLI11_NODISCARD int get_items_expected() const { return get_items_expected_min(); }
581
583 CLI11_NODISCARD bool get_positional() const { return !pname_.empty(); }
584
586 CLI11_NODISCARD bool nonpositional() const { return (!lnames_.empty() || !snames_.empty()); }
587
589 CLI11_NODISCARD bool has_description() const { return !description_.empty(); }
590
592 CLI11_NODISCARD const std::string &get_description() const { return description_; }
593
595 Option *description(std::string option_description) {
596 description_ = std::move(option_description);
597 return this;
598 }
599
600 Option *option_text(std::string text) {
601 option_text_ = std::move(text);
602 return this;
603 }
604
605 CLI11_NODISCARD const std::string &get_option_text() const { return option_text_; }
606
610
615 CLI11_NODISCARD std::string get_name(bool positional = false,
616 bool all_options = false
617 ) const;
618
622
624 void run_callback();
625
627 CLI11_NODISCARD const std::string &matching_name(const Option &other) const;
628
630 bool operator==(const Option &other) const { return !matching_name(other).empty(); }
631
633 CLI11_NODISCARD bool check_name(const std::string &name) const;
634
636 CLI11_NODISCARD bool check_sname(std::string name) const {
637 return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
638 }
639
641 CLI11_NODISCARD bool check_lname(std::string name) const {
642 return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
643 }
644
646 CLI11_NODISCARD bool check_fname(std::string name) const {
647 if(fnames_.empty()) {
648 return false;
649 }
650 return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
651 }
652
655 CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const;
656
658 Option *add_result(std::string s);
659
661 Option *add_result(std::string s, int &results_added);
662
664 Option *add_result(std::vector<std::string> s);
665
667 CLI11_NODISCARD const results_t &results() const { return results_; }
668
670 CLI11_NODISCARD results_t reduced_results() const;
671
673 template <typename T> void results(T &output) const {
674 bool retval = false;
675 if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
676 const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
677 retval = detail::lexical_conversion<T, T>(res, output);
678 } else {
679 results_t res;
680 if(results_.empty()) {
681 if(!default_str_.empty()) {
682 // _add_results takes an rvalue only
683 _add_result(std::string(default_str_), res);
684 _validate_results(res);
685 results_t extra;
686 _reduce_results(extra, res);
687 if(!extra.empty()) {
688 res = std::move(extra);
689 }
690 } else {
691 res.emplace_back();
692 }
693 } else {
694 res = reduced_results();
695 }
696 retval = detail::lexical_conversion<T, T>(res, output);
697 }
698 if(!retval) {
700 }
701 }
702
704 template <typename T> CLI11_NODISCARD T as() const {
705 T output;
706 results(output);
707 return output;
708 }
709
711 CLI11_NODISCARD bool get_callback_run() const { return (current_option_state_ == option_state::callback_run); }
712
716
718 Option *type_name_fn(std::function<std::string()> typefun) {
719 type_name_ = std::move(typefun);
720 return this;
721 }
722
724 Option *type_name(std::string typeval) {
725 type_name_fn([typeval]() { return typeval; });
726 return this;
727 }
728
730 Option *type_size(int option_type_size);
731
733 Option *type_size(int option_type_size_min, int option_type_size_max);
734
736 void inject_separator(bool value = true) { inject_separator_ = value; }
737
739 Option *default_function(const std::function<std::string()> &func) {
740 default_function_ = func;
741 return this;
742 }
743
748 }
749 return this;
750 }
751
753 Option *default_str(std::string val) {
754 default_str_ = std::move(val);
755 return this;
756 }
757
760 template <typename X> Option *default_val(const X &val) {
761 std::string val_str = detail::to_string(val);
762 auto old_option_state = current_option_state_;
763 results_t old_results{std::move(results_)};
764 results_.clear();
765 try {
766 add_result(val_str);
767 // if trigger_on_result_ is set the callback already ran
769 run_callback(); // run callback sets the state, we need to reset it again
771 } else {
772 _validate_results(results_);
773 current_option_state_ = old_option_state;
774 }
775 } catch(const CLI::Error &) {
776 // this should be done
777 results_ = std::move(old_results);
778 current_option_state_ = old_option_state;
779 throw;
780 }
781 results_ = std::move(old_results);
782 default_str_ = std::move(val_str);
783 return this;
784 }
785
787 CLI11_NODISCARD std::string get_type_name() const;
788
789 private:
791 void _validate_results(results_t &res) const;
792
796 void _reduce_results(results_t &out, const results_t &original) const;
797
798 // Run a result through the Validators
799 std::string _validate(std::string &result, int index) const;
800
802 int _add_result(std::string &&result, std::vector<std::string> &res) const;
803};
804
805// [CLI11:option_hpp:end]
806} // namespace CLI
807
808#ifndef CLI11_COMPILE
809#include "impl/Option_inl.hpp" // IWYU pragma: export
810#endif
Creates a command line program, with very few defaults.
Definition App.hpp:90
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition Error.hpp:205
All errors derive from this one.
Definition Error.hpp:73
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition Error.hpp:96
Definition Option.hpp:52
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition Option.hpp:105
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition Option.hpp:158
CRTP * group(const std::string &name)
Changes the group membership.
Definition Option.hpp:90
CRTP * join()
Set the multi option policy to join.
Definition Option.hpp:165
bool always_capture_default_
Automatically capture default value.
Definition Option.hpp:78
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:81
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition Option.hpp:172
CLI11_NODISCARD bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition Option.hpp:136
CLI11_NODISCARD char get_delimiter() const
Get the current delimiter char.
Definition Option.hpp:133
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:118
CRTP * take_first()
Set the multi option policy to take last.
Definition Option.hpp:151
CLI11_NODISCARD bool get_ignore_case() const
The status of ignore case.
Definition Option.hpp:121
bool ignore_case_
Ignore the case when matching (option, not value)
Definition Option.hpp:63
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:180
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition Option.hpp:186
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:139
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:127
bool configurable_
Allow this option to be given in a configuration file.
Definition Option.hpp:69
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:72
bool required_
True if this is a required option.
Definition Option.hpp:60
CRTP * take_last()
Set the multi option policy to take last.
Definition Option.hpp:144
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:75
std::string group_
The group membership.
Definition Option.hpp:57
CLI11_NODISCARD bool get_ignore_underscore() const
The status of ignore_underscore.
Definition Option.hpp:124
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition Option.hpp:66
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:130
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:115
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
Definition Option_inl.hpp:24
CRTP * required(bool value=true)
Set the option as required.
Definition Option.hpp:99
Definition Option.hpp:194
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition Option.hpp:201
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition Option.hpp:207
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition Option.hpp:213
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition Option.hpp:225
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition Option.hpp:219
Definition Option.hpp:231
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:462
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:36
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:583
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:265
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition Option.hpp:334
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:528
CLI11_NODISCARD bool check_name(const std::string &name) const
Check a name. Requires "-" or "--" for short / long, supports positional name.
Definition Option_inl.hpp:349
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:724
std::function< std::string()> type_name_
Definition Option.hpp:273
option_state
enumeration for the option state machine
Definition Option.hpp:321
@ reduced
a subset of results has been generated
@ callback_run
the callback has been executed
@ validated
the results have been validated
@ parsing
The option is currently collecting parsed results.
option_state current_option_state_
Whether the callback has run (needed for INI parsing)
Definition Option.hpp:328
std::string option_text_
If given, replace the text that describes the option type and usage in the help text.
Definition Option.hpp:268
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:286
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
Definition Option_inl.hpp:444
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:505
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:646
std::string pname_
A positional name.
Definition Option.hpp:252
int expected_min_
The minimum number of expected values.
Definition Option.hpp:289
Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
Making an option by hand is not defined, it must be made by the App class.
Definition Option.hpp:344
Option * ignore_case(bool value=true)
Definition Option_inl.hpp:179
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:297
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition Option.hpp:739
CLI11_NODISCARD int get_type_size_min() const
The minimum number of arguments the option expects.
Definition Option.hpp:520
void run_callback()
Process the callback.
Definition Option_inl.hpp:286
CLI11_NODISCARD bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:636
bool trigger_on_result_
flag indicating that the option should trigger the validation and callback chain on each result when ...
Definition Option.hpp:338
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:332
CLI11_NODISCARD std::string get_name(bool positional=false, bool all_options=false) const
Gets a comma separated list of names. Will include / prefer the positional name if positional is true...
Definition Option_inl.hpp:233
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:300
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:340
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:711
CLI11_NODISCARD bool get_force_callback() const
The status of force_callback.
Definition Option.hpp:403
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:249
CLI11_NODISCARD bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition Option.hpp:412
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing)
Definition Option.hpp:537
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:586
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:572
CLI11_NODISCARD const std::string & matching_name(const Option &other) const
If options share any of the same names, find it.
Definition Option_inl.hpp:312
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:641
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:667
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition Option.hpp:508
CLI11_NODISCARD int get_items_expected_max() const
Get the maximum number of items expected to be returned and used for the callback.
Definition Option.hpp:575
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:239
results_t proc_results_
results after reduction
Definition Option.hpp:319
bool remove_excludes(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition Option_inl.hpp:169
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:357
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:407
void inject_separator(bool value=true)
Set the value of the separator injection flag.
Definition Option.hpp:736
Option * allow_extra_args(bool value=true)
Definition Option.hpp:383
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition Option_inl.hpp:220
Option * trigger_on_parse(bool value=true)
Set the value of trigger_on_parse which specifies that the option callback should be triggered on eve...
Definition Option.hpp:390
CLI11_NODISCARD callback_t get_callback() const
Get the callback function.
Definition Option.hpp:540
CLI11_NODISCARD bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:525
Option * excludes(Option *opt)
Sets excluded options.
Definition Option_inl.hpp:154
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:307
CLI11_NODISCARD bool get_trigger_on_parse() const
The status of trigger on parse.
Definition Option.hpp:395
CLI11_NODISCARD const std::vector< std::string > & get_lnames() const
Get the long names.
Definition Option.hpp:543
int expected_max_
The maximum number of expected values.
Definition Option.hpp:291
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:534
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
Definition Option_inl.hpp:378
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:469
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:580
Validator * get_validator(const std::string &Validator_name="")
Get a named Validator.
Definition Option_inl.hpp:124
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:531
std::string description_
The description for help strings.
Definition Option.hpp:262
CLI11_NODISCARD const std::vector< std::string > & get_snames() const
Get the short names.
Definition Option.hpp:546
CLI11_NODISCARD int get_type_size_max() const
The maximum number of arguments the option expects.
Definition Option.hpp:522
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:336
Option * check(Validator validator, const std::string &validator_name="")
Adds a Validator with a built in type name.
Definition Option_inl.hpp:76
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:592
CLI11_NODISCARD const std::string & get_single_name() const
Get a single name for the option, first of lname, pname, sname, envname.
Definition Option.hpp:551
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:564
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:310
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:360
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:567
void clear()
Clear the parsed results (mostly for testing)
Definition Option.hpp:366
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:569
Option * capture_default_str()
Capture the default value from the original value (if it can be captured)
Definition Option.hpp:745
void results(T &output) const
Get the results as a specified type.
Definition Option.hpp:673
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:753
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:517
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:255
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty)
Definition Option.hpp:276
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:388
Option * ignore_underscore(bool value=true)
Definition Option_inl.hpp:199
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:246
std::vector< Validator > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:294
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition Option.hpp:487
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition Option.hpp:718
Option * default_val(const X &val)
Definition Option.hpp:760
Option * needs(Option *opt)
Sets required options.
Definition Option.hpp:440
int type_size_max_
Definition Option.hpp:284
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:457
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:330
Option * description(std::string option_description)
Set the description.
Definition Option.hpp:595
Option * transform(Validator Validator, const std::string &Validator_name="")
Adds a transforming Validator with a built in type name.
Definition Option_inl.hpp:92
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:242
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:398
Option * add_result(std::string s)
Puts a result at the end.
Definition Option_inl.hpp:424
bool remove_needs(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition Option_inl.hpp:144
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional)
Definition Option.hpp:630
Option * each(const std::function< void(std::string)> &func)
Adds a user supplied function to run on each item passed in (communicate though lambda capture)
Definition Option_inl.hpp:114
Option * needs(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:448
CLI11_NODISCARD const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition Option.hpp:549
CLI11_NODISCARD bool has_description() const
True if option has description.
Definition Option.hpp:589
results_t results_
complete Results of parsing
Definition Option.hpp:317
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:704
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:478
Some validators that are provided.
Definition Validators.hpp:55