CLI11
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 <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;
37class ConfigBase;
38
39using Option_p = std::unique_ptr<Option>;
40using Validator_p = std::shared_ptr<Validator>;
41
43enum class MultiOptionPolicy : char {
44 Throw,
45 TakeLast,
46 TakeFirst,
47 Join,
48 TakeAll,
49 Sum,
50 Reverse,
51};
52
54enum class CallbackPriority : std::uint8_t {
55 FirstPreHelp = 0,
56 First = 1,
57 PreRequirementsCheckPreHelp = 2,
58 PreRequirementsCheck = 3,
59 NormalPreHelp = 4,
60 Normal = 5,
61 LastPreHelp = 6,
62 Last = 7
63}; // namespace CLI
64
67template <typename CRTP> class OptionBase {
68 friend App;
69 friend ConfigBase;
70
71 protected:
73 std::string group_ = std::string("OPTIONS");
74
76 bool required_{false};
77
79 bool ignore_case_{false};
80
82 bool ignore_underscore_{false};
83
85 bool configurable_{true};
86
89
91 char delimiter_{'\0'};
92
95
97 MultiOptionPolicy multi_option_policy_{MultiOptionPolicy::Throw};
98
100 CallbackPriority callback_priority_{CallbackPriority::Normal};
101
103 template <typename T> void copy_to(T *other) const;
104
105 public:
106 // setters
107
109 CRTP *group(const std::string &name) {
110 if(!detail::valid_alias_name_string(name)) {
111 throw IncorrectConstruction("Group names may not contain newlines or null characters");
112 }
113 group_ = name;
114 return static_cast<CRTP *>(this);
115 }
116
118 CRTP *required(bool value = true) {
119 required_ = value;
120 return static_cast<CRTP *>(this);
121 }
122
124 CRTP *mandatory(bool value = true) { return required(value); }
125
126 CRTP *always_capture_default(bool value = true) {
128 return static_cast<CRTP *>(this);
129 }
130
131 // Getters
132
134 CLI11_NODISCARD const std::string &get_group() const { return group_; }
135
137 CLI11_NODISCARD bool get_required() const { return required_; }
138
140 CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
141
143 CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
144
146 CLI11_NODISCARD bool get_configurable() const { return configurable_; }
147
149 CLI11_NODISCARD bool get_disable_flag_override() const { return disable_flag_override_; }
150
152 CLI11_NODISCARD char get_delimiter() const { return delimiter_; }
153
155 CLI11_NODISCARD bool get_always_capture_default() const { return always_capture_default_; }
156
158 CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const { return multi_option_policy_; }
159
161 CLI11_NODISCARD CallbackPriority get_callback_priority() const { return callback_priority_; }
162
163 // Shortcuts for multi option policy
164
166 CRTP *take_last() {
167 auto *self = static_cast<CRTP *>(this);
168 self->multi_option_policy(MultiOptionPolicy::TakeLast);
169 return self;
170 }
171
173 CRTP *take_first() {
174 auto *self = static_cast<CRTP *>(this);
175 self->multi_option_policy(MultiOptionPolicy::TakeFirst);
176 return self;
177 }
178
180 CRTP *take_all() {
181 auto self = static_cast<CRTP *>(this);
182 self->multi_option_policy(MultiOptionPolicy::TakeAll);
183 return self;
184 }
185
187 CRTP *join() {
188 auto *self = static_cast<CRTP *>(this);
189 self->multi_option_policy(MultiOptionPolicy::Join);
190 return self;
191 }
192
194 CRTP *join(char delim) {
195 auto self = static_cast<CRTP *>(this);
196 self->delimiter_ = delim;
197 self->multi_option_policy(MultiOptionPolicy::Join);
198 return self;
199 }
200
202 CRTP *configurable(bool value = true) {
203 configurable_ = value;
204 return static_cast<CRTP *>(this);
205 }
206
208 CRTP *delimiter(char value = '\0') {
209 delimiter_ = value;
210 return static_cast<CRTP *>(this);
211 }
212};
213
216class OptionDefaults : public OptionBase<OptionDefaults> {
217 public:
218 OptionDefaults() = default;
219
220 // Methods here need a different implementation if they are Option vs. OptionDefault
221
223 OptionDefaults *callback_priority(CallbackPriority value = CallbackPriority::Normal) {
224 callback_priority_ = value;
225 return this;
226 }
227
229 OptionDefaults *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw) {
230 multi_option_policy_ = value;
231 return this;
232 }
233
235 OptionDefaults *ignore_case(bool value = true) {
236 ignore_case_ = value;
237 return this;
238 }
239
241 OptionDefaults *ignore_underscore(bool value = true) {
242 ignore_underscore_ = value;
243 return this;
244 }
245
249 return this;
250 }
251
253 OptionDefaults *delimiter(char value = '\0') {
254 delimiter_ = value;
255 return this;
256 }
257};
258
259class Option : public OptionBase<Option> {
260 friend App;
261 friend ConfigBase;
262
263 protected:
266
268 std::vector<std::string> snames_{};
269
271 std::vector<std::string> lnames_{};
272
275 std::vector<std::pair<std::string, std::string>> default_flag_values_{};
276
278 std::vector<std::string> fnames_{};
279
281 std::string pname_{};
282
284 std::string envname_{};
285
289
291 std::string description_{};
292
294 std::string default_str_{};
295
297 std::string option_text_{};
298
302 std::function<std::string()> type_name_{[]() { return std::string(); }};
303
305 std::function<std::string()> default_function_{};
306
310
316
321
323 std::vector<Validator_p> validators_{};
324
326 std::set<Option *> needs_{};
327
329 std::set<Option *> excludes_{};
330
334
336 App *parent_{nullptr};
337
339 callback_t callback_{};
340
344
346 results_t results_{};
348 mutable results_t proc_results_{};
350 enum class option_state : char {
351 parsing = 0,
352 validated = 2,
353 reduced = 4,
354 callback_run = 6,
355 };
359 bool allow_extra_args_{false};
361 bool flag_like_{false};
365 bool inject_separator_{false};
369 bool force_callback_{false};
370
372 Option(std::string option_name,
373 std::string option_description,
374 callback_t callback,
375 App *parent,
376 bool allow_non_standard = false)
377 : description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
378 std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name), allow_non_standard);
379 }
380
381 public:
384
385 Option(const Option &) = delete;
386 Option &operator=(const Option &) = delete;
387
389 CLI11_NODISCARD std::size_t count() const { return results_.size(); }
390
392 CLI11_NODISCARD bool empty() const { return results_.empty(); }
393
395 explicit operator bool() const { return !empty() || force_callback_; }
396
398 void clear() {
399 results_.clear();
400 proc_results_.clear();
402 }
403
407
409 Option *expected(int value);
410
412 Option *expected(int value_min, int value_max);
413
416 Option *allow_extra_args(bool value = true) {
417 allow_extra_args_ = value;
418 return this;
419 }
421 CLI11_NODISCARD bool get_allow_extra_args() const { return allow_extra_args_; }
423 Option *trigger_on_parse(bool value = true) {
424 trigger_on_result_ = value;
425 return this;
426 }
428 CLI11_NODISCARD bool get_trigger_on_parse() const { return trigger_on_result_; }
429
431 Option *force_callback(bool value = true) {
432 force_callback_ = value;
433 return this;
434 }
436 CLI11_NODISCARD bool get_force_callback() const { return force_callback_; }
437
440 Option *run_callback_for_default(bool value = true) {
442 return this;
443 }
445 CLI11_NODISCARD bool get_run_callback_for_default() const { return run_callback_for_default_; }
446
449 Option *callback_priority(CallbackPriority value = CallbackPriority::Normal) {
450 callback_priority_ = value;
451 return this;
452 }
453
455 Option *check(Validator_p validator);
456
458 Option *check(Validator validator, const std::string &validator_name = "");
459
461 Option *check(std::function<std::string(const std::string &)> validator_func,
462 std::string validator_description = "",
463 std::string validator_name = "");
464
466 Option *transform(Validator_p validator);
467
469 Option *transform(Validator validator, const std::string &transform_name = "");
470
472 Option *transform(Validator validator, const std::string &transform_description, const std::string &transform_name);
473
475 Option *transform(const std::function<std::string(std::string)> &transform_func,
476 std::string transform_description = "",
477 std::string transform_name = "");
478
480 Option *each(const std::function<void(std::string)> &func);
481
483 Validator *get_validator(const std::string &validator_name = "");
484
486 Validator *get_validator(int index);
487
490 if(opt != this) {
491 needs_.insert(opt);
492 }
493 return this;
494 }
495
497 template <typename T = App> Option *needs(std::string opt_name) {
498 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
499 if(opt == nullptr) {
500 throw IncorrectConstruction::MissingOption(opt_name);
501 }
502 return needs(opt);
503 }
504
506 template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
507 needs(opt);
508 return needs(opt1, args...); // NOLINT(readability-suspicious-call-argument)
509 }
510
512 bool remove_needs(Option *opt);
513
515 Option *excludes(Option *opt);
516
518 template <typename T = App> Option *excludes(std::string opt_name) {
519 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
520 if(opt == nullptr) {
521 throw IncorrectConstruction::MissingOption(opt_name);
522 }
523 return excludes(opt);
524 }
525
527 template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
528 excludes(opt);
529 return excludes(opt1, args...);
530 }
531
533 bool remove_excludes(Option *opt);
534
536 Option *envname(std::string name) {
537 envname_ = std::move(name);
538 return this;
539 }
540
545 template <typename T = App> Option *ignore_case(bool value = true);
546
551 template <typename T = App> Option *ignore_underscore(bool value = true);
552
554 Option *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw);
555
557 Option *disable_flag_override(bool value = true) {
559 return this;
560 }
564
566 CLI11_NODISCARD int get_type_size() const { return type_size_min_; }
567
569 CLI11_NODISCARD int get_type_size_min() const { return type_size_min_; }
571 CLI11_NODISCARD int get_type_size_max() const { return type_size_max_; }
572
574 CLI11_NODISCARD bool get_inject_separator() const { return inject_separator_; }
575
577 CLI11_NODISCARD std::string get_envname() const { return envname_; }
578
580 CLI11_NODISCARD std::set<Option *> get_needs() const { return needs_; }
581
583 CLI11_NODISCARD std::set<Option *> get_excludes() const { return excludes_; }
584
586 CLI11_NODISCARD std::string get_default_str() const { return default_str_; }
587
589 CLI11_NODISCARD callback_t get_callback() const { return callback_; }
590
592 CLI11_NODISCARD const std::vector<std::string> &get_lnames() const { return lnames_; }
593
595 CLI11_NODISCARD const std::vector<std::string> &get_snames() const { return snames_; }
596
598 CLI11_NODISCARD const std::vector<std::string> &get_fnames() const { return fnames_; }
600 CLI11_NODISCARD const std::string &get_single_name() const {
601 if(!lnames_.empty()) {
602 return lnames_[0];
603 }
604 if(!snames_.empty()) {
605 return snames_[0];
606 }
607 if(!pname_.empty()) {
608 return pname_;
609 }
610 return envname_;
611 }
613 CLI11_NODISCARD int get_expected() const { return expected_min_; }
614
616 CLI11_NODISCARD int get_expected_min() const { return expected_min_; }
618 CLI11_NODISCARD int get_expected_max() const { return expected_max_; }
619
621 CLI11_NODISCARD int get_items_expected_min() const { return type_size_min_ * expected_min_; }
622
624 CLI11_NODISCARD int get_items_expected_max() const {
625 int t = type_size_max_;
626 return detail::checked_multiply(t, expected_max_) ? t : detail::expected_max_vector_size;
627 }
629 CLI11_NODISCARD int get_items_expected() const { return get_items_expected_min(); }
630
632 CLI11_NODISCARD bool get_positional() const { return !pname_.empty(); }
633
635 CLI11_NODISCARD bool nonpositional() const { return (!lnames_.empty() || !snames_.empty()); }
636
638 CLI11_NODISCARD bool has_description() const { return !description_.empty(); }
639
641 CLI11_NODISCARD const std::string &get_description() const { return description_; }
642
644 Option *description(std::string option_description) {
645 description_ = std::move(option_description);
646 return this;
647 }
648
649 Option *option_text(std::string text) {
650 option_text_ = std::move(text);
651 return this;
652 }
653
654 CLI11_NODISCARD const std::string &get_option_text() const { return option_text_; }
655
659
665 CLI11_NODISCARD std::string get_name(bool positional = false,
666 bool all_options = false,
667 bool disable_default_flag_values = false
668 ) const;
669
673
675 void run_callback();
676
678 CLI11_NODISCARD const std::string &matching_name(const Option &other) const;
679
681 bool operator==(const Option &other) const { return !matching_name(other).empty(); }
682
684 CLI11_NODISCARD bool check_name(const std::string &name) const;
685
687 CLI11_NODISCARD bool check_sname(std::string name) const {
688 return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
689 }
690
692 CLI11_NODISCARD bool check_lname(std::string name) const {
693 return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
694 }
695
697 CLI11_NODISCARD bool check_fname(std::string name) const {
698 if(fnames_.empty()) {
699 return false;
700 }
701 return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
702 }
703
706 CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const;
707
709 Option *add_result(std::string s);
710
712 Option *add_result(std::string s, int &results_added);
713
715 Option *add_result(std::vector<std::string> s);
716
718 CLI11_NODISCARD const results_t &results() const { return results_; }
719
721 CLI11_NODISCARD results_t reduced_results() const;
722
724 template <typename T> void results(T &output) const {
725 bool retval = false;
726 if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
727 const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
728 if(!res.empty()) {
729 retval = detail::lexical_conversion<T, T>(res, output);
730 } else {
731 results_t res2;
732 res2.emplace_back();
733 proc_results_ = std::move(res2);
734 retval = detail::lexical_conversion<T, T>(proc_results_, output);
735 }
736
737 } else {
738 results_t res;
739 if(results_.empty()) {
740 if(!default_str_.empty()) {
741 // _add_results takes an rvalue only
742 _add_result(std::string(default_str_), res);
743 _validate_results(res);
744 results_t extra;
745 _reduce_results(extra, res);
746 if(!extra.empty()) {
747 res = std::move(extra);
748 }
749 } else {
750 res.emplace_back();
751 }
752 } else {
753 res = reduced_results();
754 }
755 // store the results in a stable location if the output is a view
756 proc_results_ = std::move(res);
757 retval = detail::lexical_conversion<T, T>(proc_results_, output);
758 }
759 if(!retval) {
761 }
762 }
763
765 template <typename T> CLI11_NODISCARD T as() const {
766 T output;
767 results(output);
768 return output;
769 }
770
772 CLI11_NODISCARD bool get_callback_run() const { return (current_option_state_ == option_state::callback_run); }
773
777
779 Option *type_name_fn(std::function<std::string()> typefun) {
780 type_name_ = std::move(typefun);
781 return this;
782 }
783
785 Option *type_name(std::string typeval) {
786 type_name_fn([typeval]() { return typeval; });
787 return this;
788 }
789
791 Option *type_size(int option_type_size);
792
794 Option *type_size(int option_type_size_min, int option_type_size_max);
795
797 void inject_separator(bool value = true) { inject_separator_ = value; }
798
800 Option *default_function(const std::function<std::string()> &func) {
801 default_function_ = func;
802 return this;
803 }
804
809 }
810 return this;
811 }
812
814 Option *default_str(std::string val) {
815 default_str_ = std::move(val);
816 return this;
817 }
818
821 template <typename X> Option *default_val(const X &val) {
822 std::string val_str = detail::value_string(val);
823 auto old_option_state = current_option_state_;
824 results_t old_results{std::move(results_)};
825 results_.clear();
826 try {
827 add_result(val_str);
828 // if trigger_on_result_ is set the callback already ran
830 run_callback(); // run callback sets the state, we need to reset it again
832 } else {
833 _validate_results(results_);
834 current_option_state_ = old_option_state;
835 }
836 } catch(const ConversionError &err) {
837 // this should be done
838 results_ = std::move(old_results);
839 current_option_state_ = old_option_state;
840
841 throw ConversionError(
842 get_name(), std::string("given default value(\"") + val_str + "\") produces an error : " + err.what());
843 } catch(const CLI::Error &) {
844 results_ = std::move(old_results);
845 current_option_state_ = old_option_state;
846 throw;
847 }
848 results_ = std::move(old_results);
849 default_str_ = std::move(val_str);
850 return this;
851 }
852
854 CLI11_NODISCARD std::string get_type_name() const;
855
856 private:
858 void _validate_results(results_t &res) const;
859
863 void _reduce_results(results_t &out, const results_t &original) const;
864
865 // Run a result through the Validators
866 std::string _validate(std::string &result, int index) const;
867
869 int _add_result(std::string &&result, std::vector<std::string> &res) const;
870};
871
872// [CLI11:option_hpp:end]
873} // namespace CLI
874
875#ifndef CLI11_COMPILE
876#include "impl/Option_inl.hpp" // IWYU pragma: export
877#endif
Creates a command line program, with very few defaults.
Definition App.hpp:114
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:101
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:67
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition Option.hpp:124
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition Option.hpp:180
CRTP * group(const std::string &name)
Changes the group membership.
Definition Option.hpp:109
CRTP * join()
Set the multi option policy to join.
Definition Option.hpp:187
bool always_capture_default_
Automatically capture default value.
Definition Option.hpp:94
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:97
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition Option.hpp:194
CLI11_NODISCARD CallbackPriority get_callback_priority() const
The priority of callback.
Definition Option.hpp:161
CLI11_NODISCARD bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition Option.hpp:155
CLI11_NODISCARD char get_delimiter() const
Get the current delimiter char.
Definition Option.hpp:152
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:137
CRTP * take_first()
Set the multi option policy to take last.
Definition Option.hpp:173
CLI11_NODISCARD bool get_ignore_case() const
The status of ignore case.
Definition Option.hpp:140
bool ignore_case_
Ignore the case when matching (option, not value)
Definition Option.hpp:79
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:202
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition Option.hpp:208
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:158
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:146
bool configurable_
Allow this option to be given in a configuration file.
Definition Option.hpp:85
CallbackPriority callback_priority_
Priority of callback.
Definition Option.hpp:100
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:88
bool required_
True if this is a required option.
Definition Option.hpp:76
CRTP * take_last()
Set the multi option policy to take last.
Definition Option.hpp:166
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:91
std::string group_
The group membership.
Definition Option.hpp:73
CLI11_NODISCARD bool get_ignore_underscore() const
The status of ignore_underscore.
Definition Option.hpp:143
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition Option.hpp:82
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:149
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:134
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
Definition Option_inl.hpp:25
CRTP * required(bool value=true)
Set the option as required.
Definition Option.hpp:118
Definition Option.hpp:216
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition Option.hpp:229
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition Option.hpp:235
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition Option.hpp:241
OptionDefaults * callback_priority(CallbackPriority value=CallbackPriority::Normal)
Set the callback priority.
Definition Option.hpp:223
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition Option.hpp:253
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition Option.hpp:247
Definition Option.hpp:259
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:506
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:38
Option * check(Validator_p validator)
Adds a shared validator.
Definition Option_inl.hpp:78
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:632
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:294
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition Option.hpp:363
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:577
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:393
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:785
std::function< std::string()> type_name_
Definition Option.hpp:302
option_state
enumeration for the option state machine
Definition Option.hpp:350
@ 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:357
std::string option_text_
If given, replace the text that describes the option type and usage in the help text.
Definition Option.hpp:297
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:315
Option * callback_priority(CallbackPriority value=CallbackPriority::Normal)
Definition Option.hpp:449
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
Definition Option_inl.hpp:488
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:549
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:697
std::string pname_
A positional name.
Definition Option.hpp:281
int expected_min_
The minimum number of expected values.
Definition Option.hpp:318
Option * transform(Validator_p validator)
Adds a shared Validator.
Definition Option_inl.hpp:107
Option * ignore_case(bool value=true)
Definition Option_inl.hpp:214
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:326
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition Option.hpp:800
CLI11_NODISCARD int get_type_size_min() const
The minimum number of arguments the option expects.
Definition Option.hpp:569
void run_callback()
Process the callback.
Definition Option_inl.hpp:322
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:269
CLI11_NODISCARD bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:687
bool trigger_on_result_
flag indicating that the option should trigger the validation and callback chain on each result when ...
Definition Option.hpp:367
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:361
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:329
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:369
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:772
CLI11_NODISCARD bool get_force_callback() const
The status of force_callback.
Definition Option.hpp:436
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:278
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.hpp:372
CLI11_NODISCARD bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition Option.hpp:445
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing)
Definition Option.hpp:586
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:635
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:621
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:355
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:692
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:718
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition Option.hpp:557
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:624
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:268
results_t proc_results_
results after reduction
Definition Option.hpp:348
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:204
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:389
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:440
void inject_separator(bool value=true)
Set the value of the separator injection flag.
Definition Option.hpp:797
Option * allow_extra_args(bool value=true)
Definition Option.hpp:416
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition Option_inl.hpp:255
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:423
CLI11_NODISCARD callback_t get_callback() const
Get the callback function.
Definition Option.hpp:589
CLI11_NODISCARD bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:574
std::vector< Validator_p > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:323
Option * excludes(Option *opt)
Sets excluded options.
Definition Option_inl.hpp:189
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:336
CLI11_NODISCARD bool get_trigger_on_parse() const
The status of trigger on parse.
Definition Option.hpp:428
CLI11_NODISCARD const std::vector< std::string > & get_lnames() const
Get the long names.
Definition Option.hpp:592
int expected_max_
The maximum number of expected values.
Definition Option.hpp:320
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:583
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
Definition Option_inl.hpp:422
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:518
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:629
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:580
std::string description_
The description for help strings.
Definition Option.hpp:291
CLI11_NODISCARD const std::vector< std::string > & get_snames() const
Get the short names.
Definition Option.hpp:595
CLI11_NODISCARD int get_type_size_max() const
The maximum number of arguments the option expects.
Definition Option.hpp:571
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:365
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:641
Validator * get_validator(const std::string &validator_name="")
Get a named Validator.
Definition Option_inl.hpp:159
CLI11_NODISCARD const std::string & get_single_name() const
Get a single name for the option, first of lname, sname, pname, envname.
Definition Option.hpp:600
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:613
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:339
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:392
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:616
void clear()
Clear the parsed results (mostly for testing)
Definition Option.hpp:398
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:618
Option * capture_default_str()
Capture the default value from the original value (if it can be captured)
Definition Option.hpp:806
void results(T &output) const
Get the results as a specified type.
Definition Option.hpp:724
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:814
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:566
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:284
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty)
Definition Option.hpp:305
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:421
Option * ignore_underscore(bool value=true)
Definition Option_inl.hpp:234
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:275
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition Option.hpp:536
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition Option.hpp:779
Option * default_val(const X &val)
Definition Option.hpp:821
Option * needs(Option *opt)
Sets required options.
Definition Option.hpp:489
int type_size_max_
Definition Option.hpp:313
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:506
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:359
Option * description(std::string option_description)
Set the description.
Definition Option.hpp:644
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:271
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:431
Option * add_result(std::string s)
Puts a result at the end.
Definition Option_inl.hpp:468
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:179
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional)
Definition Option.hpp:681
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:148
Option * needs(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:497
CLI11_NODISCARD const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition Option.hpp:598
CLI11_NODISCARD bool has_description() const
True if option has description.
Definition Option.hpp:638
results_t results_
complete Results of parsing
Definition Option.hpp:346
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:765
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:527
Some validators that are provided.
Definition Validators.hpp:54