CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Option.hpp
1// Copyright (c) 2017-2025, 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
55template <typename CRTP> class OptionBase {
56 friend App;
57 friend ConfigBase;
58
59 protected:
61 std::string group_ = std::string("OPTIONS");
62
64 bool required_{false};
65
67 bool ignore_case_{false};
68
70 bool ignore_underscore_{false};
71
73 bool configurable_{true};
74
77
79 char delimiter_{'\0'};
80
83
85 MultiOptionPolicy multi_option_policy_{MultiOptionPolicy::Throw};
86
88 template <typename T> void copy_to(T *other) const;
89
90 public:
91 // setters
92
94 CRTP *group(const std::string &name) {
95 if(!detail::valid_alias_name_string(name)) {
96 throw IncorrectConstruction("Group names may not contain newlines or null characters");
97 }
98 group_ = name;
99 return static_cast<CRTP *>(this);
100 }
101
103 CRTP *required(bool value = true) {
104 required_ = value;
105 return static_cast<CRTP *>(this);
106 }
107
109 CRTP *mandatory(bool value = true) { return required(value); }
110
111 CRTP *always_capture_default(bool value = true) {
113 return static_cast<CRTP *>(this);
114 }
115
116 // Getters
117
119 CLI11_NODISCARD const std::string &get_group() const { return group_; }
120
122 CLI11_NODISCARD bool get_required() const { return required_; }
123
125 CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
126
128 CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
129
131 CLI11_NODISCARD bool get_configurable() const { return configurable_; }
132
134 CLI11_NODISCARD bool get_disable_flag_override() const { return disable_flag_override_; }
135
137 CLI11_NODISCARD char get_delimiter() const { return delimiter_; }
138
140 CLI11_NODISCARD bool get_always_capture_default() const { return always_capture_default_; }
141
143 CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const { return multi_option_policy_; }
144
145 // Shortcuts for multi option policy
146
148 CRTP *take_last() {
149 auto *self = static_cast<CRTP *>(this);
150 self->multi_option_policy(MultiOptionPolicy::TakeLast);
151 return self;
152 }
153
155 CRTP *take_first() {
156 auto *self = static_cast<CRTP *>(this);
157 self->multi_option_policy(MultiOptionPolicy::TakeFirst);
158 return self;
159 }
160
162 CRTP *take_all() {
163 auto self = static_cast<CRTP *>(this);
164 self->multi_option_policy(MultiOptionPolicy::TakeAll);
165 return self;
166 }
167
169 CRTP *join() {
170 auto *self = static_cast<CRTP *>(this);
171 self->multi_option_policy(MultiOptionPolicy::Join);
172 return self;
173 }
174
176 CRTP *join(char delim) {
177 auto self = static_cast<CRTP *>(this);
178 self->delimiter_ = delim;
179 self->multi_option_policy(MultiOptionPolicy::Join);
180 return self;
181 }
182
184 CRTP *configurable(bool value = true) {
185 configurable_ = value;
186 return static_cast<CRTP *>(this);
187 }
188
190 CRTP *delimiter(char value = '\0') {
191 delimiter_ = value;
192 return static_cast<CRTP *>(this);
193 }
194};
195
198class OptionDefaults : public OptionBase<OptionDefaults> {
199 public:
200 OptionDefaults() = default;
201
202 // Methods here need a different implementation if they are Option vs. OptionDefault
203
205 OptionDefaults *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw) {
206 multi_option_policy_ = value;
207 return this;
208 }
209
211 OptionDefaults *ignore_case(bool value = true) {
212 ignore_case_ = value;
213 return this;
214 }
215
217 OptionDefaults *ignore_underscore(bool value = true) {
218 ignore_underscore_ = value;
219 return this;
220 }
221
225 return this;
226 }
227
229 OptionDefaults *delimiter(char value = '\0') {
230 delimiter_ = value;
231 return this;
232 }
233};
234
235class Option : public OptionBase<Option> {
236 friend App;
237 friend ConfigBase;
238
239 protected:
242
244 std::vector<std::string> snames_{};
245
247 std::vector<std::string> lnames_{};
248
251 std::vector<std::pair<std::string, std::string>> default_flag_values_{};
252
254 std::vector<std::string> fnames_{};
255
257 std::string pname_{};
258
260 std::string envname_{};
261
265
267 std::string description_{};
268
270 std::string default_str_{};
271
273 std::string option_text_{};
274
278 std::function<std::string()> type_name_{[]() { return std::string(); }};
279
281 std::function<std::string()> default_function_{};
282
286
292
297
299 std::vector<Validator_p> validators_{};
300
302 std::set<Option *> needs_{};
303
305 std::set<Option *> excludes_{};
306
310
312 App *parent_{nullptr};
313
315 callback_t callback_{};
316
320
322 results_t results_{};
324 mutable results_t proc_results_{};
326 enum class option_state : char {
327 parsing = 0,
328 validated = 2,
329 reduced = 4,
330 callback_run = 6,
331 };
335 bool allow_extra_args_{false};
337 bool flag_like_{false};
341 bool inject_separator_{false};
345 bool force_callback_{false};
347
349 Option(std::string option_name,
350 std::string option_description,
351 callback_t callback,
352 App *parent,
353 bool allow_non_standard = false)
354 : description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
355 std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name), allow_non_standard);
356 }
357
358 public:
361
362 Option(const Option &) = delete;
363 Option &operator=(const Option &) = delete;
364
366 CLI11_NODISCARD std::size_t count() const { return results_.size(); }
367
369 CLI11_NODISCARD bool empty() const { return results_.empty(); }
370
372 explicit operator bool() const { return !empty() || force_callback_; }
373
375 void clear() {
376 results_.clear();
378 }
379
383
385 Option *expected(int value);
386
388 Option *expected(int value_min, int value_max);
389
392 Option *allow_extra_args(bool value = true) {
393 allow_extra_args_ = value;
394 return this;
395 }
397 CLI11_NODISCARD bool get_allow_extra_args() const { return allow_extra_args_; }
399 Option *trigger_on_parse(bool value = true) {
400 trigger_on_result_ = value;
401 return this;
402 }
404 CLI11_NODISCARD bool get_trigger_on_parse() const { return trigger_on_result_; }
405
407 Option *force_callback(bool value = true) {
408 force_callback_ = value;
409 return this;
410 }
412 CLI11_NODISCARD bool get_force_callback() const { return force_callback_; }
413
416 Option *run_callback_for_default(bool value = true) {
418 return this;
419 }
421 CLI11_NODISCARD bool get_run_callback_for_default() const { return run_callback_for_default_; }
422
424 Option *check(Validator_p validator);
425
427 Option *check(Validator validator, const std::string &validator_name = "");
428
430 Option *check(std::function<std::string(const std::string &)> validator_func,
431 std::string validator_description = "",
432 std::string validator_name = "");
433
435 Option *transform(Validator_p validator);
436
438 Option *transform(Validator validator, const std::string &transform_name = "");
439
441 Option *transform(const std::function<std::string(std::string)> &transform_func,
442 std::string transform_description = "",
443 std::string transform_name = "");
444
446 Option *each(const std::function<void(std::string)> &func);
447
449 Validator *get_validator(const std::string &validator_name = "");
450
452 Validator *get_validator(int index);
453
456 if(opt != this) {
457 needs_.insert(opt);
458 }
459 return this;
460 }
461
463 template <typename T = App> Option *needs(std::string opt_name) {
464 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
465 if(opt == nullptr) {
466 throw IncorrectConstruction::MissingOption(opt_name);
467 }
468 return needs(opt);
469 }
470
472 template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
473 needs(opt);
474 return needs(opt1, args...); // NOLINT(readability-suspicious-call-argument)
475 }
476
478 bool remove_needs(Option *opt);
479
481 Option *excludes(Option *opt);
482
484 template <typename T = App> Option *excludes(std::string opt_name) {
485 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
486 if(opt == nullptr) {
487 throw IncorrectConstruction::MissingOption(opt_name);
488 }
489 return excludes(opt);
490 }
491
493 template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
494 excludes(opt);
495 return excludes(opt1, args...);
496 }
497
499 bool remove_excludes(Option *opt);
500
502 Option *envname(std::string name) {
503 envname_ = std::move(name);
504 return this;
505 }
506
511 template <typename T = App> Option *ignore_case(bool value = true);
512
517 template <typename T = App> Option *ignore_underscore(bool value = true);
518
520 Option *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw);
521
523 Option *disable_flag_override(bool value = true) {
525 return this;
526 }
530
532 CLI11_NODISCARD int get_type_size() const { return type_size_min_; }
533
535 CLI11_NODISCARD int get_type_size_min() const { return type_size_min_; }
537 CLI11_NODISCARD int get_type_size_max() const { return type_size_max_; }
538
540 CLI11_NODISCARD bool get_inject_separator() const { return inject_separator_; }
541
543 CLI11_NODISCARD std::string get_envname() const { return envname_; }
544
546 CLI11_NODISCARD std::set<Option *> get_needs() const { return needs_; }
547
549 CLI11_NODISCARD std::set<Option *> get_excludes() const { return excludes_; }
550
552 CLI11_NODISCARD std::string get_default_str() const { return default_str_; }
553
555 CLI11_NODISCARD callback_t get_callback() const { return callback_; }
556
558 CLI11_NODISCARD const std::vector<std::string> &get_lnames() const { return lnames_; }
559
561 CLI11_NODISCARD const std::vector<std::string> &get_snames() const { return snames_; }
562
564 CLI11_NODISCARD const std::vector<std::string> &get_fnames() const { return fnames_; }
566 CLI11_NODISCARD const std::string &get_single_name() const {
567 if(!lnames_.empty()) {
568 return lnames_[0];
569 }
570 if(!snames_.empty()) {
571 return snames_[0];
572 }
573 if(!pname_.empty()) {
574 return pname_;
575 }
576 return envname_;
577 }
579 CLI11_NODISCARD int get_expected() const { return expected_min_; }
580
582 CLI11_NODISCARD int get_expected_min() const { return expected_min_; }
584 CLI11_NODISCARD int get_expected_max() const { return expected_max_; }
585
587 CLI11_NODISCARD int get_items_expected_min() const { return type_size_min_ * expected_min_; }
588
590 CLI11_NODISCARD int get_items_expected_max() const {
591 int t = type_size_max_;
592 return detail::checked_multiply(t, expected_max_) ? t : detail::expected_max_vector_size;
593 }
595 CLI11_NODISCARD int get_items_expected() const { return get_items_expected_min(); }
596
598 CLI11_NODISCARD bool get_positional() const { return !pname_.empty(); }
599
601 CLI11_NODISCARD bool nonpositional() const { return (!lnames_.empty() || !snames_.empty()); }
602
604 CLI11_NODISCARD bool has_description() const { return !description_.empty(); }
605
607 CLI11_NODISCARD const std::string &get_description() const { return description_; }
608
610 Option *description(std::string option_description) {
611 description_ = std::move(option_description);
612 return this;
613 }
614
615 Option *option_text(std::string text) {
616 option_text_ = std::move(text);
617 return this;
618 }
619
620 CLI11_NODISCARD const std::string &get_option_text() const { return option_text_; }
621
625
630 CLI11_NODISCARD std::string get_name(bool positional = false,
631 bool all_options = false
632 ) const;
633
637
639 void run_callback();
640
642 CLI11_NODISCARD const std::string &matching_name(const Option &other) const;
643
645 bool operator==(const Option &other) const { return !matching_name(other).empty(); }
646
648 CLI11_NODISCARD bool check_name(const std::string &name) const;
649
651 CLI11_NODISCARD bool check_sname(std::string name) const {
652 return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
653 }
654
656 CLI11_NODISCARD bool check_lname(std::string name) const {
657 return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
658 }
659
661 CLI11_NODISCARD bool check_fname(std::string name) const {
662 if(fnames_.empty()) {
663 return false;
664 }
665 return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
666 }
667
670 CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const;
671
673 Option *add_result(std::string s);
674
676 Option *add_result(std::string s, int &results_added);
677
679 Option *add_result(std::vector<std::string> s);
680
682 CLI11_NODISCARD const results_t &results() const { return results_; }
683
685 CLI11_NODISCARD results_t reduced_results() const;
686
688 template <typename T> void results(T &output) const {
689 bool retval = false;
690 if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
691 const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
692 if(!res.empty()) {
693 retval = detail::lexical_conversion<T, T>(res, output);
694 } else {
695 results_t res2;
696 res2.emplace_back();
697 proc_results_ = std::move(res2);
698 retval = detail::lexical_conversion<T, T>(proc_results_, output);
699 }
700
701 } else {
702 results_t res;
703 if(results_.empty()) {
704 if(!default_str_.empty()) {
705 // _add_results takes an rvalue only
706 _add_result(std::string(default_str_), res);
707 _validate_results(res);
708 results_t extra;
709 _reduce_results(extra, res);
710 if(!extra.empty()) {
711 res = std::move(extra);
712 }
713 } else {
714 res.emplace_back();
715 }
716 } else {
717 res = reduced_results();
718 }
719 // store the results in a stable location if the output is a view
720 proc_results_ = std::move(res);
721 retval = detail::lexical_conversion<T, T>(proc_results_, output);
722 }
723 if(!retval) {
725 }
726 }
727
729 template <typename T> CLI11_NODISCARD T as() const {
730 T output;
731 results(output);
732 return output;
733 }
734
736 CLI11_NODISCARD bool get_callback_run() const { return (current_option_state_ == option_state::callback_run); }
737
741
743 Option *type_name_fn(std::function<std::string()> typefun) {
744 type_name_ = std::move(typefun);
745 return this;
746 }
747
749 Option *type_name(std::string typeval) {
750 type_name_fn([typeval]() { return typeval; });
751 return this;
752 }
753
755 Option *type_size(int option_type_size);
756
758 Option *type_size(int option_type_size_min, int option_type_size_max);
759
761 void inject_separator(bool value = true) { inject_separator_ = value; }
762
764 Option *default_function(const std::function<std::string()> &func) {
765 default_function_ = func;
766 return this;
767 }
768
773 }
774 return this;
775 }
776
778 Option *default_str(std::string val) {
779 default_str_ = std::move(val);
780 return this;
781 }
782
785 template <typename X> Option *default_val(const X &val) {
786 std::string val_str = detail::to_string(val);
787 auto old_option_state = current_option_state_;
788 results_t old_results{std::move(results_)};
789 results_.clear();
790 try {
791 add_result(val_str);
792 // if trigger_on_result_ is set the callback already ran
794 run_callback(); // run callback sets the state, we need to reset it again
796 } else {
797 _validate_results(results_);
798 current_option_state_ = old_option_state;
799 }
800 } catch(const ConversionError &err) {
801 // this should be done
802 results_ = std::move(old_results);
803 current_option_state_ = old_option_state;
804
805 throw ConversionError(
806 get_name(), std::string("given default value(\"") + val_str + "\") produces an error : " + err.what());
807 } catch(const CLI::Error &) {
808 results_ = std::move(old_results);
809 current_option_state_ = old_option_state;
810 throw;
811 }
812 results_ = std::move(old_results);
813 default_str_ = std::move(val_str);
814 return this;
815 }
816
818 CLI11_NODISCARD std::string get_type_name() const;
819
820 private:
822 void _validate_results(results_t &res) const;
823
827 void _reduce_results(results_t &out, const results_t &original) const;
828
829 // Run a result through the Validators
830 std::string _validate(std::string &result, int index) const;
831
833 int _add_result(std::string &&result, std::vector<std::string> &res) const;
834};
835
836// [CLI11:option_hpp:end]
837} // namespace CLI
838
839#ifndef CLI11_COMPILE
840#include "impl/Option_inl.hpp" // IWYU pragma: export
841#endif
Creates a command line program, with very few defaults.
Definition App.hpp:98
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:85
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:55
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition Option.hpp:109
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition Option.hpp:162
CRTP * group(const std::string &name)
Changes the group membership.
Definition Option.hpp:94
CRTP * join()
Set the multi option policy to join.
Definition Option.hpp:169
bool always_capture_default_
Automatically capture default value.
Definition Option.hpp:82
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:85
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition Option.hpp:176
CLI11_NODISCARD bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition Option.hpp:140
CLI11_NODISCARD char get_delimiter() const
Get the current delimiter char.
Definition Option.hpp:137
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:122
CRTP * take_first()
Set the multi option policy to take last.
Definition Option.hpp:155
CLI11_NODISCARD bool get_ignore_case() const
The status of ignore case.
Definition Option.hpp:125
bool ignore_case_
Ignore the case when matching (option, not value)
Definition Option.hpp:67
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:184
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition Option.hpp:190
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:143
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:131
bool configurable_
Allow this option to be given in a configuration file.
Definition Option.hpp:73
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:76
bool required_
True if this is a required option.
Definition Option.hpp:64
CRTP * take_last()
Set the multi option policy to take last.
Definition Option.hpp:148
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:79
std::string group_
The group membership.
Definition Option.hpp:61
CLI11_NODISCARD bool get_ignore_underscore() const
The status of ignore_underscore.
Definition Option.hpp:128
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition Option.hpp:70
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:134
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:119
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:103
Definition Option.hpp:198
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition Option.hpp:205
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition Option.hpp:211
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition Option.hpp:217
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition Option.hpp:229
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition Option.hpp:223
Definition Option.hpp:235
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:490
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:37
Option * check(Validator_p validator)
Adds a shared validator.
Definition Option_inl.hpp:77
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:598
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:270
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition Option.hpp:339
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:543
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:377
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:749
std::function< std::string()> type_name_
Definition Option.hpp:278
option_state
enumeration for the option state machine
Definition Option.hpp:326
@ 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:333
std::string option_text_
If given, replace the text that describes the option type and usage in the help text.
Definition Option.hpp:273
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:291
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
Definition Option_inl.hpp:472
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:533
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:661
std::string pname_
A positional name.
Definition Option.hpp:257
int expected_min_
The minimum number of expected values.
Definition Option.hpp:294
Option * transform(Validator_p validator)
Adds a shared Validator.
Definition Option_inl.hpp:106
Option * ignore_case(bool value=true)
Definition Option_inl.hpp:202
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:302
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition Option.hpp:764
CLI11_NODISCARD int get_type_size_min() const
The minimum number of arguments the option expects.
Definition Option.hpp:535
void run_callback()
Process the callback.
Definition Option_inl.hpp:309
CLI11_NODISCARD bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:651
bool trigger_on_result_
flag indicating that the option should trigger the validation and callback chain on each result when ...
Definition Option.hpp:343
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:337
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:256
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:305
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:345
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:736
CLI11_NODISCARD bool get_force_callback() const
The status of force_callback.
Definition Option.hpp:412
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:254
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:349
CLI11_NODISCARD bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition Option.hpp:421
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing)
Definition Option.hpp:552
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:601
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:587
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:339
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:656
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:682
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition Option.hpp:523
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:590
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:244
results_t proc_results_
results after reduction
Definition Option.hpp:324
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:192
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:366
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:416
void inject_separator(bool value=true)
Set the value of the separator injection flag.
Definition Option.hpp:761
Option * allow_extra_args(bool value=true)
Definition Option.hpp:392
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition Option_inl.hpp:243
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:399
CLI11_NODISCARD callback_t get_callback() const
Get the callback function.
Definition Option.hpp:555
CLI11_NODISCARD bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:540
std::vector< Validator_p > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:299
Option * excludes(Option *opt)
Sets excluded options.
Definition Option_inl.hpp:177
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:312
CLI11_NODISCARD bool get_trigger_on_parse() const
The status of trigger on parse.
Definition Option.hpp:404
CLI11_NODISCARD const std::vector< std::string > & get_lnames() const
Get the long names.
Definition Option.hpp:558
int expected_max_
The maximum number of expected values.
Definition Option.hpp:296
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:549
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
Definition Option_inl.hpp:406
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:484
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:595
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:546
std::string description_
The description for help strings.
Definition Option.hpp:267
CLI11_NODISCARD const std::vector< std::string > & get_snames() const
Get the short names.
Definition Option.hpp:561
CLI11_NODISCARD int get_type_size_max() const
The maximum number of arguments the option expects.
Definition Option.hpp:537
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:341
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:607
Validator * get_validator(const std::string &validator_name="")
Get a named Validator.
Definition Option_inl.hpp:147
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:566
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:579
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:315
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:369
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:582
void clear()
Clear the parsed results (mostly for testing)
Definition Option.hpp:375
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:584
Option * capture_default_str()
Capture the default value from the original value (if it can be captured)
Definition Option.hpp:770
void results(T &output) const
Get the results as a specified type.
Definition Option.hpp:688
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:778
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:532
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:260
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty)
Definition Option.hpp:281
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:397
Option * ignore_underscore(bool value=true)
Definition Option_inl.hpp:222
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:251
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition Option.hpp:502
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition Option.hpp:743
Option * default_val(const X &val)
Definition Option.hpp:785
Option * needs(Option *opt)
Sets required options.
Definition Option.hpp:455
int type_size_max_
Definition Option.hpp:289
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:472
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:335
Option * description(std::string option_description)
Set the description.
Definition Option.hpp:610
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:247
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:407
Option * add_result(std::string s)
Puts a result at the end.
Definition Option_inl.hpp:452
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:167
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional)
Definition Option.hpp:645
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:136
Option * needs(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:463
CLI11_NODISCARD const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition Option.hpp:564
CLI11_NODISCARD bool has_description() const
True if option has description.
Definition Option.hpp:604
results_t results_
complete Results of parsing
Definition Option.hpp:322
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:729
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:493
Some validators that are provided.
Definition Validators.hpp:54