CLI11 2.7.2
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Option.hpp
1// Copyright (c) 2017-2026, 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 <cstddef>
14#include <cstdint>
15#include <functional>
16#include <iterator>
17#include <memory>
18#include <set>
19#include <string>
20#include <utility>
21#include <vector>
22// [CLI11:public_includes:end]
23
24#include "Error.hpp"
25#include "Macros.hpp"
26#include "Split.hpp"
27#include "StringTools.hpp"
28#include "Validators.hpp"
29
30namespace CLI {
31// [CLI11:option_hpp:verbatim]
32
33using results_t = std::vector<std::string>;
35using callback_t = std::function<bool(const results_t &)>;
36
37class Option;
38class App;
39class ConfigBase;
40
41using Option_p = std::unique_ptr<Option>;
42using Validator_p = std::shared_ptr<Validator>;
43
45enum class MultiOptionPolicy : char {
46 Throw,
47 TakeLast,
48 TakeFirst,
49 Join,
50 TakeAll,
51 Sum,
52 Reverse,
53};
54
56enum class CallbackPriority : std::uint8_t {
57 FirstPreHelp = 0,
58 First = 1,
59 PreRequirementsCheckPreHelp = 2,
60 PreRequirementsCheck = 3,
61 NormalPreHelp = 4,
62 Normal = 5,
63 LastPreHelp = 6,
64 Last = 7
65}; // namespace CLI
66
69template <typename CRTP> class OptionBase {
70 friend App;
71 friend ConfigBase;
72
73 protected:
75 std::string group_ = std::string("OPTIONS");
76
78 bool required_{false};
79
81 bool ignore_case_{false};
82
84 bool ignore_underscore_{false};
85
87 bool configurable_{true};
88
91
93 char delimiter_{'\0'};
94
97
99 MultiOptionPolicy multi_option_policy_{MultiOptionPolicy::Throw};
100
102 CallbackPriority callback_priority_{CallbackPriority::Normal};
103
105 template <typename T> void copy_to(T *other) const;
106
107 public:
108 // setters
109
111 CRTP *group(const std::string &name) {
112 if(!detail::valid_alias_name_string(name)) {
113 throw IncorrectConstruction("Group names may not contain newlines or null characters");
114 }
115 group_ = name;
116 return static_cast<CRTP *>(this);
117 }
118
120 CRTP *required(bool value = true) {
121 required_ = value;
122 return static_cast<CRTP *>(this);
123 }
124
126 CRTP *mandatory(bool value = true) { return required(value); }
127
128 CRTP *always_capture_default(bool value = true) {
130 return static_cast<CRTP *>(this);
131 }
132
133 // Getters
134
136 CLI11_NODISCARD const std::string &get_group() const { return group_; }
137
139 CLI11_NODISCARD bool get_required() const { return required_; }
140
142 CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
143
145 CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
146
148 CLI11_NODISCARD bool get_configurable() const { return configurable_; }
149
151 CLI11_NODISCARD bool get_disable_flag_override() const { return disable_flag_override_; }
152
154 CLI11_NODISCARD char get_delimiter() const { return delimiter_; }
155
157 CLI11_NODISCARD bool get_always_capture_default() const { return always_capture_default_; }
158
160 CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const { return multi_option_policy_; }
161
163 CLI11_NODISCARD CallbackPriority get_callback_priority() const { return callback_priority_; }
164
165 // Shortcuts for multi option policy
166
168 CRTP *take_last() {
169 auto *self = static_cast<CRTP *>(this);
170 self->multi_option_policy(MultiOptionPolicy::TakeLast);
171 return self;
172 }
173
175 CRTP *take_first() {
176 auto *self = static_cast<CRTP *>(this);
177 self->multi_option_policy(MultiOptionPolicy::TakeFirst);
178 return self;
179 }
180
182 CRTP *take_all() {
183 auto self = static_cast<CRTP *>(this);
184 self->multi_option_policy(MultiOptionPolicy::TakeAll);
185 return self;
186 }
187
189 CRTP *join() {
190 auto *self = static_cast<CRTP *>(this);
191 self->multi_option_policy(MultiOptionPolicy::Join);
192 return self;
193 }
194
196 CRTP *join(char delim) {
197 auto self = static_cast<CRTP *>(this);
198 self->delimiter_ = delim;
199 self->multi_option_policy(MultiOptionPolicy::Join);
200 return self;
201 }
202
204 CRTP *configurable(bool value = true) {
205 configurable_ = value;
206 return static_cast<CRTP *>(this);
207 }
208
210 CRTP *delimiter(char value = '\0') {
211 delimiter_ = value;
212 return static_cast<CRTP *>(this);
213 }
214};
215
218class OptionDefaults : public OptionBase<OptionDefaults> {
219 public:
220 OptionDefaults() = default;
221
222 // Methods here need a different implementation if they are Option vs. OptionDefault
223
225 OptionDefaults *callback_priority(CallbackPriority value = CallbackPriority::Normal) {
226 callback_priority_ = value;
227 return this;
228 }
229
231 OptionDefaults *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw) {
232 multi_option_policy_ = value;
233 return this;
234 }
235
237 OptionDefaults *ignore_case(bool value = true) {
238 ignore_case_ = value;
239 return this;
240 }
241
243 OptionDefaults *ignore_underscore(bool value = true) {
244 ignore_underscore_ = value;
245 return this;
246 }
247
249 OptionDefaults *disable_flag_override(bool value = true) {
251 return this;
252 }
253
255 OptionDefaults *delimiter(char value = '\0') {
256 delimiter_ = value;
257 return this;
258 }
259};
260
261class Option : public OptionBase<Option> {
262 friend App;
263 friend ConfigBase;
264
265 protected:
268
270 std::vector<std::string> snames_{};
271
273 std::vector<std::string> lnames_{};
274
277 std::vector<std::pair<std::string, std::string>> default_flag_values_{};
278
280 std::vector<std::string> fnames_{};
281
283 std::string pname_{};
284
286 std::string envname_{};
287
291
293 std::string description_{};
294
296 std::string default_str_{};
297
299 std::string option_text_{};
300
304 std::function<std::string()> type_name_{[]() { return std::string(); }};
305
307 std::function<std::string()> default_function_{};
308
312
318
323
325 std::vector<Validator_p> validators_{};
326
328 std::set<Option *> needs_{};
329
331 std::set<Option *> excludes_{};
332
336
338 App *parent_{nullptr};
339
341 callback_t callback_{};
342
346
348 results_t results_{};
350 mutable results_t proc_results_{};
352 enum class option_state : char {
357 };
358
361 bool allow_extra_args_{false};
363 bool flag_like_{false};
367 bool inject_separator_{false};
371 bool force_callback_{false};
372
374 Option(std::string option_name,
375 std::string option_description,
376 callback_t callback,
377 App *parent,
378 bool allow_non_standard = false);
379
380 public:
383
384 Option(const Option &) = delete;
385 Option &operator=(const Option &) = delete;
386
388 CLI11_NODISCARD std::size_t count() const { return results_.size(); }
389
391 CLI11_NODISCARD bool empty() const { return results_.empty(); }
392
394 explicit operator bool() const { return !empty() || force_callback_; }
395
397 void clear();
398
402
404 Option *expected(int value);
405
407 Option *expected(int value_min, int value_max);
408
411 Option *allow_extra_args(bool value = true) {
412 allow_extra_args_ = value;
413 return this;
414 }
415
416 CLI11_NODISCARD bool get_allow_extra_args() const { return allow_extra_args_; }
418 Option *trigger_on_parse(bool value = true) {
419 trigger_on_result_ = value;
420 return this;
421 }
422
423 CLI11_NODISCARD bool get_trigger_on_parse() const { return trigger_on_result_; }
424
426 Option *force_callback(bool value = true) {
427 force_callback_ = value;
428 return this;
429 }
430
431 CLI11_NODISCARD bool get_force_callback() const { return force_callback_; }
432
435 Option *run_callback_for_default(bool value = true) {
437 return this;
438 }
439
440 CLI11_NODISCARD bool get_run_callback_for_default() const { return run_callback_for_default_; }
441
444 Option *callback_priority(CallbackPriority value = CallbackPriority::Normal) {
445 callback_priority_ = value;
446 return this;
447 }
448
450 Option *check(Validator_p validator);
451
453 Option *check(Validator validator, const std::string &validator_name = "");
454
456 Option *check(std::function<std::string(const std::string &)> validator_func,
457 std::string validator_description = "",
458 std::string validator_name = "");
459
461 Option *transform(Validator_p validator);
462
464 Option *transform(Validator validator, const std::string &transform_name = "");
465
467 Option *transform(Validator validator, const std::string &transform_description, const std::string &transform_name);
468
470 Option *transform(const std::function<std::string(std::string)> &transform_func,
471 std::string transform_description = "",
472 std::string transform_name = "");
473
475 Option *each(const std::function<void(std::string)> &func);
476
478 Validator *get_validator(const std::string &validator_name = "");
479
481 Validator *get_validator(int index);
482
484 Option *needs(Option *opt);
485
487 template <typename T = App> Option *needs(std::string opt_name) {
488 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
489 if(opt == nullptr) {
490 throw IncorrectConstruction::MissingOption(opt_name);
491 }
492 return needs(opt);
493 }
494
496 template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
497 needs(opt);
498 return needs(opt1, args...); // NOLINT(readability-suspicious-call-argument)
499 }
500
502 bool remove_needs(Option *opt);
503
505 Option *excludes(Option *opt);
506
508 template <typename T = App> Option *excludes(std::string opt_name) {
509 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
510 if(opt == nullptr) {
511 throw IncorrectConstruction::MissingOption(opt_name);
512 }
513 return excludes(opt);
514 }
515
517 template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
518 excludes(opt);
519 return excludes(opt1, args...);
520 }
521
523 bool remove_excludes(Option *opt);
524
526 Option *envname(std::string name) {
527 envname_ = std::move(name);
528 return this;
529 }
530
535 template <typename T = App> Option *ignore_case(bool value = true);
536
541 template <typename T = App> Option *ignore_underscore(bool value = true);
542
544 Option *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw);
545
547 Option *disable_flag_override(bool value = true) {
549 return this;
550 }
551
554
556 CLI11_NODISCARD int get_type_size() const { return type_size_min_; }
557
559 CLI11_NODISCARD int get_type_size_min() const { return type_size_min_; }
561 CLI11_NODISCARD int get_type_size_max() const { return type_size_max_; }
562
564 CLI11_NODISCARD bool get_inject_separator() const { return inject_separator_; }
565
567 CLI11_NODISCARD std::string get_envname() const { return envname_; }
568
570 CLI11_NODISCARD std::set<Option *> get_needs() const { return needs_; }
571
573 CLI11_NODISCARD std::set<Option *> get_excludes() const { return excludes_; }
574
576 CLI11_NODISCARD std::string get_default_str() const { return default_str_; }
577
579 CLI11_NODISCARD callback_t get_callback() const { return callback_; }
580
582 CLI11_NODISCARD const std::vector<std::string> &get_lnames() const { return lnames_; }
583
585 CLI11_NODISCARD const std::vector<std::string> &get_snames() const { return snames_; }
586
588 CLI11_NODISCARD const std::vector<std::string> &get_fnames() const { return fnames_; }
590 CLI11_NODISCARD const std::string &get_single_name() const;
592 CLI11_NODISCARD int get_expected() const { return expected_min_; }
593
595 CLI11_NODISCARD int get_expected_min() const { return expected_min_; }
597 CLI11_NODISCARD int get_expected_max() const { return expected_max_; }
598
600 CLI11_NODISCARD int get_items_expected_min() const { return type_size_min_ * expected_min_; }
601
603 CLI11_NODISCARD int get_items_expected_max() const {
604 int t = type_size_max_;
605 return detail::checked_multiply(t, expected_max_) ? t : detail::expected_max_vector_size;
606 }
607
608 CLI11_NODISCARD int get_items_expected() const { return get_items_expected_min(); }
609
611 CLI11_NODISCARD bool get_positional() const { return !pname_.empty(); }
612
614 CLI11_NODISCARD bool nonpositional() const { return (!lnames_.empty() || !snames_.empty()); }
615
617 CLI11_NODISCARD bool has_description() const { return !description_.empty(); }
618
620 CLI11_NODISCARD const std::string &get_description() const { return description_; }
621
623 Option *description(std::string option_description) {
624 description_ = std::move(option_description);
625 return this;
626 }
627
628 Option *option_text(std::string text) {
629 option_text_ = std::move(text);
630 return this;
631 }
632
633 CLI11_NODISCARD const std::string &get_option_text() const { return option_text_; }
634
638
644 CLI11_NODISCARD std::string get_name(bool positional = false,
645 bool all_options = false,
646 bool disable_default_flag_values = false
647 ) const;
648
652
654 void run_callback();
655
657 CLI11_NODISCARD const std::string &matching_name(const Option &other) const;
658
660 bool operator==(const Option &other) const { return !matching_name(other).empty(); }
661
663 CLI11_NODISCARD bool check_name(const std::string &name) const;
664
666 CLI11_NODISCARD bool check_sname(std::string name) const {
667 return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
668 }
669
671 CLI11_NODISCARD bool check_lname(std::string name) const {
672 return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
673 }
674
676 CLI11_NODISCARD bool check_fname(std::string name) const {
677 if(fnames_.empty()) {
678 return false;
679 }
680 return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
681 }
682
685 CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const;
686
688 Option *add_result(std::string s);
689
691 Option *add_result(std::string s, int &results_added);
692
694 Option *add_result(std::vector<std::string> s);
695
697 CLI11_NODISCARD const results_t &results() const { return results_; }
698
700 CLI11_NODISCARD results_t reduced_results() const;
701
703 template <typename T> void results(T &output) const {
704 bool retval = false;
705 if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
706 const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
707 if(!res.empty()) {
708 retval = detail::lexical_conversion<T, T>(res, output);
709 } else {
710 results_t res2;
711 res2.emplace_back();
712 proc_results_ = std::move(res2);
713 retval = detail::lexical_conversion<T, T>(proc_results_, output);
714 }
715
716 } else {
717 results_t res;
718 if(results_.empty()) {
719 if(!default_str_.empty()) {
720 // _add_results takes an rvalue only
721 _add_result(std::string(default_str_), res);
722 _validate_results(res);
723 results_t extra;
724 _reduce_results(extra, res);
725 if(!extra.empty()) {
726 res = std::move(extra);
727 }
728 } else {
729 res.emplace_back();
730 }
731 } else {
732 res = reduced_results();
733 }
734 // store the results in a stable location if the output is a view
735 proc_results_ = std::move(res);
736 retval = detail::lexical_conversion<T, T>(proc_results_, output);
737 }
738 if(!retval) {
740 }
741 }
742
744 template <typename T> CLI11_NODISCARD T as() const {
745 T output;
746 results(output);
747 return output;
748 }
749
751 CLI11_NODISCARD bool get_callback_run() const { return (current_option_state_ == option_state::callback_run); }
752
756
758 Option *type_name_fn(std::function<std::string()> typefun) {
759 type_name_ = std::move(typefun);
760 return this;
761 }
762
764 Option *type_name(std::string typeval);
765
767 Option *type_size(int option_type_size);
768
770 Option *type_size(int option_type_size_min, int option_type_size_max);
771
773 void inject_separator(bool value = true) { inject_separator_ = value; }
774
776 Option *default_function(const std::function<std::string()> &func) {
777 default_function_ = func;
778 return this;
779 }
780
783
785 Option *default_str(std::string val) {
786 default_str_ = std::move(val);
787 return this;
788 }
789
792 template <typename X> Option *default_val(const X &val) {
793 std::string val_str = detail::value_string(val);
794 auto old_option_state = current_option_state_;
795 results_t old_results{std::move(results_)};
796 results_.clear();
797 try {
798 add_result(val_str);
799 // if trigger_on_result_ is set the callback already ran
801 run_callback(); // run callback sets the state, we need to reset it again
803 } else {
804 _validate_results(results_);
805 current_option_state_ = old_option_state;
806 }
807 } catch(const ConversionError &err) {
808 // this should be done
809 results_ = std::move(old_results);
810 current_option_state_ = old_option_state;
811
812 throw ConversionError(
813 get_name(), std::string("given default value(\"") + val_str + "\") produces an error : " + err.what());
814 } catch(const CLI::Error &) {
815 results_ = std::move(old_results);
816 current_option_state_ = old_option_state;
817 throw;
818 }
819 results_ = std::move(old_results);
820 default_str_ = std::move(val_str);
821 return this;
822 }
823
825 CLI11_NODISCARD std::string get_type_name() const;
826
827 private:
829 void _validate_results(results_t &res) const;
830
834 void _reduce_results(results_t &out, const results_t &original) const;
835
836 // Run a result through the Validators
837 std::string _validate(std::string &result, int index) const;
838
840 int _add_result(std::string &&result, std::vector<std::string> &res) const;
841};
842
843// [CLI11:option_hpp:end]
844} // namespace CLI
845
846#ifndef CLI11_COMPILE
847#include "impl/Option_inl.hpp" // IWYU pragma: export
848#endif
Creates a command line program, with very few defaults.
Definition App.hpp:115
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:70
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition Error.hpp:206
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:97
Definition Option.hpp:69
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition Option.hpp:126
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition Option.hpp:182
CRTP * group(const std::string &name)
Changes the group membership.
Definition Option.hpp:111
CRTP * join()
Set the multi option policy to join.
Definition Option.hpp:189
bool always_capture_default_
Automatically capture default value.
Definition Option.hpp:96
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:99
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition Option.hpp:196
CLI11_NODISCARD CallbackPriority get_callback_priority() const
The priority of callback.
Definition Option.hpp:163
CLI11_NODISCARD bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition Option.hpp:157
CLI11_NODISCARD char get_delimiter() const
Get the current delimiter char.
Definition Option.hpp:154
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:139
CRTP * take_first()
Set the multi option policy to take last.
Definition Option.hpp:175
CLI11_NODISCARD bool get_ignore_case() const
The status of ignore case.
Definition Option.hpp:142
bool ignore_case_
Ignore the case when matching (option, not value).
Definition Option.hpp:81
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:204
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition Option.hpp:210
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:160
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:148
bool configurable_
Allow this option to be given in a configuration file.
Definition Option.hpp:87
CallbackPriority callback_priority_
Priority of callback.
Definition Option.hpp:102
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:90
bool required_
True if this is a required option.
Definition Option.hpp:78
CRTP * take_last()
Set the multi option policy to take last.
Definition Option.hpp:168
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:93
std::string group_
The group membership.
Definition Option.hpp:75
CLI11_NODISCARD bool get_ignore_underscore() const
The status of ignore_underscore.
Definition Option.hpp:145
bool ignore_underscore_
Ignore underscores when matching (option, not value).
Definition Option.hpp:84
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:151
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:136
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase).
Definition Option_inl.hpp:31
CRTP * required(bool value=true)
Set the option as required.
Definition Option.hpp:120
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition Option.hpp:231
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition Option.hpp:237
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition Option.hpp:243
OptionDefaults * callback_priority(CallbackPriority value=CallbackPriority::Normal)
Set the callback priority.
Definition Option.hpp:225
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition Option.hpp:255
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition Option.hpp:249
Definition Option.hpp:261
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:531
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:56
Option * check(Validator_p validator)
Adds a shared validator.
Definition Option_inl.hpp:96
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:611
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:296
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition Option.hpp:365
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:567
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:418
std::function< std::string()> type_name_
Definition Option.hpp:304
option_state
enumeration for the option state machine
Definition Option.hpp:352
@ reduced
a subset of results has been generated
Definition Option.hpp:355
@ callback_run
the callback has been executed
Definition Option.hpp:356
@ validated
the results have been validated
Definition Option.hpp:354
@ parsing
The option is currently collecting parsed results.
Definition Option.hpp:353
option_state current_option_state_
Whether the callback has run (needed for INI parsing).
Definition Option.hpp:359
std::string option_text_
If given, replace the text that describes the option type and usage in the help text.
Definition Option.hpp:299
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:317
Option * callback_priority(CallbackPriority value=CallbackPriority::Normal)
Definition Option.hpp:444
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
Definition Option_inl.hpp:513
void clear()
Clear the parsed results (mostly for testing).
Definition Option_inl.hpp:50
Option * capture_default_str()
Capture the default value from the original value (if it can be captured).
Definition Option_inl.hpp:592
Option * needs(Option *opt)
Sets required options.
Definition Option_inl.hpp:197
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:599
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:676
std::string pname_
A positional name.
Definition Option.hpp:283
int expected_min_
The minimum number of expected values.
Definition Option.hpp:320
Option * transform(Validator_p validator)
Adds a shared Validator.
Definition Option_inl.hpp:125
Option * ignore_case(bool value=true)
Definition Option_inl.hpp:239
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:328
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition Option.hpp:776
CLI11_NODISCARD int get_type_size_min() const
The minimum number of arguments the option expects.
Definition Option.hpp:559
void run_callback()
Process the callback.
Definition Option_inl.hpp:347
CLI11_NODISCARD std::string get_name(bool positional=false, bool all_options=false, bool disable_default_flag_values=false) const
Gets a comma separated list of names. Will include / prefer the positional name if positional is true...
Definition Option_inl.hpp:294
CLI11_NODISCARD bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:666
bool trigger_on_result_
flag indicating that the option should trigger the validation and callback chain on each result when ...
Definition Option.hpp:369
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:363
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:331
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:371
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:751
CLI11_NODISCARD bool get_force_callback() const
The status of force_callback.
Definition Option.hpp:431
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:280
CLI11_NODISCARD bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition Option.hpp:440
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing).
Definition Option.hpp:576
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:614
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:600
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:380
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:671
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:697
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition Option.hpp:547
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:603
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:270
results_t proc_results_
results after reduction
Definition Option.hpp:350
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:229
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:388
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:435
void inject_separator(bool value=true)
Set the value of the separator injection flag.
Definition Option.hpp:773
Option * allow_extra_args(bool value=true)
Definition Option.hpp:411
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy).
Definition Option_inl.hpp:280
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:418
CLI11_NODISCARD callback_t get_callback() const
Get the callback function.
Definition Option.hpp:579
CLI11_NODISCARD bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:564
std::vector< Validator_p > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:325
CLI11_NODISCARD const std::string & get_single_name() const
Get a single name for the option, first of lname, sname, pname, envname.
Definition Option_inl.hpp:574
Option * excludes(Option *opt)
Sets excluded options.
Definition Option_inl.hpp:214
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:338
CLI11_NODISCARD bool get_trigger_on_parse() const
The status of trigger on parse.
Definition Option.hpp:423
CLI11_NODISCARD const std::vector< std::string > & get_lnames() const
Get the long names.
Definition Option.hpp:582
int expected_max_
The maximum number of expected values.
Definition Option.hpp:322
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:573
Option(std::string option_name, std::string option_description, callback_t callback, App *parent, bool allow_non_standard=false)
Making an option by hand is not defined, it must be made by the App class.
Definition Option_inl.hpp:44
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
Definition Option_inl.hpp:447
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:508
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:608
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:570
std::string description_
The description for help strings.
Definition Option.hpp:293
CLI11_NODISCARD const std::vector< std::string > & get_snames() const
Get the short names.
Definition Option.hpp:585
CLI11_NODISCARD int get_type_size_max() const
The maximum number of arguments the option expects.
Definition Option.hpp:561
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:367
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:620
Validator * get_validator(const std::string &validator_name="")
Get a named Validator.
Definition Option_inl.hpp:177
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:592
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:341
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:391
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:595
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:597
void results(T &output) const
Get the results as a specified type.
Definition Option.hpp:703
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value).
Definition Option.hpp:785
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:556
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:286
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty).
Definition Option.hpp:307
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:416
Option * ignore_underscore(bool value=true)
Definition Option_inl.hpp:259
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:277
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition Option.hpp:526
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition Option.hpp:758
Option * default_val(const X &val)
Definition Option.hpp:792
int type_size_max_
Definition Option.hpp:315
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:496
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:361
Option * description(std::string option_description)
Set the description.
Definition Option.hpp:623
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:273
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:426
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option_inl.hpp:587
Option * add_result(std::string s)
Puts a result at the end.
Definition Option_inl.hpp:493
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:204
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional).
Definition Option.hpp:660
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:166
Option * needs(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:487
CLI11_NODISCARD const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition Option.hpp:588
CLI11_NODISCARD bool has_description() const
True if option has description.
Definition Option.hpp:617
results_t results_
complete Results of parsing
Definition Option.hpp:348
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:744
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:517
Some validators that are provided.
Definition Validators.hpp:55