CLI11 2.4.1
Loading...
Searching...
No Matches
Option.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// [CLI11:public_includes:set]
10#include <algorithm>
11#include <functional>
12#include <memory>
13#include <set>
14#include <string>
15#include <tuple>
16#include <utility>
17#include <vector>
18// [CLI11:public_includes:end]
19
20#include "Error.hpp"
21#include "Macros.hpp"
22#include "Split.hpp"
23#include "StringTools.hpp"
24#include "Validators.hpp"
25
26namespace CLI {
27// [CLI11:option_hpp:verbatim]
28
29using results_t = std::vector<std::string>;
31using callback_t = std::function<bool(const results_t &)>;
32
33class Option;
34class App;
35
36using Option_p = std::unique_ptr<Option>;
38enum class MultiOptionPolicy : char {
39 Throw,
40 TakeLast,
41 TakeFirst,
42 Join,
43 TakeAll,
44 Sum,
45 Reverse,
46};
47
50template <typename CRTP> class OptionBase {
51 friend App;
52
53 protected:
55 std::string group_ = std::string("Options");
56
58 bool required_{false};
59
61 bool ignore_case_{false};
62
64 bool ignore_underscore_{false};
65
67 bool configurable_{true};
68
71
73 char delimiter_{'\0'};
74
77
80
82 template <typename T> void copy_to(T *other) const;
83
84 public:
85 // setters
86
88 CRTP *group(const std::string &name) {
90 throw IncorrectConstruction("Group names may not contain newlines or null characters");
91 }
92 group_ = name;
93 return static_cast<CRTP *>(this);
94 }
95
97 CRTP *required(bool value = true) {
98 required_ = value;
99 return static_cast<CRTP *>(this);
100 }
101
103 CRTP *mandatory(bool value = true) { return required(value); }
104
105 CRTP *always_capture_default(bool value = true) {
107 return static_cast<CRTP *>(this);
108 }
109
110 // Getters
111
113 CLI11_NODISCARD const std::string &get_group() const { return group_; }
114
116 CLI11_NODISCARD bool get_required() const { return required_; }
117
120
123
126
129
131 CLI11_NODISCARD char get_delimiter() const { return delimiter_; }
132
135
138
139 // Shortcuts for multi option policy
140
142 CRTP *take_last() {
143 auto *self = static_cast<CRTP *>(this);
144 self->multi_option_policy(MultiOptionPolicy::TakeLast);
145 return self;
146 }
147
149 CRTP *take_first() {
150 auto *self = static_cast<CRTP *>(this);
151 self->multi_option_policy(MultiOptionPolicy::TakeFirst);
152 return self;
153 }
154
156 CRTP *take_all() {
157 auto self = static_cast<CRTP *>(this);
158 self->multi_option_policy(MultiOptionPolicy::TakeAll);
159 return self;
160 }
161
163 CRTP *join() {
164 auto *self = static_cast<CRTP *>(this);
165 self->multi_option_policy(MultiOptionPolicy::Join);
166 return self;
167 }
168
170 CRTP *join(char delim) {
171 auto self = static_cast<CRTP *>(this);
172 self->delimiter_ = delim;
173 self->multi_option_policy(MultiOptionPolicy::Join);
174 return self;
175 }
176
178 CRTP *configurable(bool value = true) {
179 configurable_ = value;
180 return static_cast<CRTP *>(this);
181 }
182
184 CRTP *delimiter(char value = '\0') {
185 delimiter_ = value;
186 return static_cast<CRTP *>(this);
187 }
188};
189
192class OptionDefaults : public OptionBase<OptionDefaults> {
193 public:
194 OptionDefaults() = default;
195
196 // Methods here need a different implementation if they are Option vs. OptionDefault
197
203
205 OptionDefaults *ignore_case(bool value = true) {
206 ignore_case_ = value;
207 return this;
208 }
209
211 OptionDefaults *ignore_underscore(bool value = true) {
212 ignore_underscore_ = value;
213 return this;
214 }
215
219 return this;
220 }
221
223 OptionDefaults *delimiter(char value = '\0') {
224 delimiter_ = value;
225 return this;
226 }
227};
228
229class Option : public OptionBase<Option> {
230 friend App;
231
232 protected:
235
237 std::vector<std::string> snames_{};
238
240 std::vector<std::string> lnames_{};
241
244 std::vector<std::pair<std::string, std::string>> default_flag_values_{};
245
247 std::vector<std::string> fnames_{};
248
250 std::string pname_{};
251
253 std::string envname_{};
254
258
260 std::string description_{};
261
263 std::string default_str_{};
264
266 std::string option_text_{};
267
271 std::function<std::string()> type_name_{[]() { return std::string(); }};
272
274 std::function<std::string()> default_function_{};
275
279
285
290
292 std::vector<Validator> validators_{};
293
295 std::set<Option *> needs_{};
296
298 std::set<Option *> excludes_{};
299
303
305 App *parent_{nullptr};
306
309
313
319 enum class option_state : char {
320 parsing = 0,
321 validated = 2,
322 reduced = 4,
323 callback_run = 6,
324 };
328 bool allow_extra_args_{false};
330 bool flag_like_{false};
334 bool inject_separator_{false};
338 bool force_callback_{false};
340
342 Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
343 : description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
344 std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name));
345 }
346
347 public:
350
351 Option(const Option &) = delete;
352 Option &operator=(const Option &) = delete;
353
355 CLI11_NODISCARD std::size_t count() const { return results_.size(); }
356
358 CLI11_NODISCARD bool empty() const { return results_.empty(); }
359
361 explicit operator bool() const { return !empty() || force_callback_; }
362
364 void clear() {
365 results_.clear();
367 }
368
372
374 Option *expected(int value);
375
377 Option *expected(int value_min, int value_max);
378
381 Option *allow_extra_args(bool value = true) {
382 allow_extra_args_ = value;
383 return this;
384 }
388 Option *trigger_on_parse(bool value = true) {
389 trigger_on_result_ = value;
390 return this;
391 }
394
396 Option *force_callback(bool value = true) {
397 force_callback_ = value;
398 return this;
399 }
402
405 Option *run_callback_for_default(bool value = true) {
407 return this;
408 }
411
413 Option *check(Validator validator, const std::string &validator_name = "");
414
416 Option *check(std::function<std::string(const std::string &)> Validator,
417 std::string Validator_description = "",
418 std::string Validator_name = "");
419
421 Option *transform(Validator Validator, const std::string &Validator_name = "");
422
424 Option *transform(const std::function<std::string(std::string)> &func,
425 std::string transform_description = "",
426 std::string transform_name = "");
427
429 Option *each(const std::function<void(std::string)> &func);
430
432 Validator *get_validator(const std::string &Validator_name = "");
433
436
439 if(opt != this) {
440 needs_.insert(opt);
441 }
442 return this;
443 }
444
446 template <typename T = App> Option *needs(std::string opt_name) {
447 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
448 if(opt == nullptr) {
449 throw IncorrectConstruction::MissingOption(opt_name);
450 }
451 return needs(opt);
452 }
453
455 template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
456 needs(opt);
457 return needs(opt1, args...); // NOLINT(readability-suspicious-call-argument)
458 }
459
462
465
467 template <typename T = App> Option *excludes(std::string opt_name) {
468 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
469 if(opt == nullptr) {
470 throw IncorrectConstruction::MissingOption(opt_name);
471 }
472 return excludes(opt);
473 }
474
476 template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
477 excludes(opt);
478 return excludes(opt1, args...);
479 }
480
483
485 Option *envname(std::string name) {
486 envname_ = std::move(name);
487 return this;
488 }
489
494 template <typename T = App> Option *ignore_case(bool value = true);
495
500 template <typename T = App> Option *ignore_underscore(bool value = true);
501
504
506 Option *disable_flag_override(bool value = true) {
508 return this;
509 }
513
516
521
524
526 CLI11_NODISCARD std::string get_envname() const { return envname_; }
527
529 CLI11_NODISCARD std::set<Option *> get_needs() const { return needs_; }
530
532 CLI11_NODISCARD std::set<Option *> get_excludes() const { return excludes_; }
533
535 CLI11_NODISCARD std::string get_default_str() const { return default_str_; }
536
539
541 CLI11_NODISCARD const std::vector<std::string> &get_lnames() const { return lnames_; }
542
544 CLI11_NODISCARD const std::vector<std::string> &get_snames() const { return snames_; }
545
547 CLI11_NODISCARD const std::vector<std::string> &get_fnames() const { return fnames_; }
549 CLI11_NODISCARD const std::string &get_single_name() const {
550 if(!lnames_.empty()) {
551 return lnames_[0];
552 }
553 if(!snames_.empty()) {
554 return snames_[0];
555 }
556 if(!pname_.empty()) {
557 return pname_;
558 }
559 return envname_;
560 }
563
568
571
579
581 CLI11_NODISCARD bool get_positional() const { return !pname_.empty(); }
582
584 CLI11_NODISCARD bool nonpositional() const { return (!lnames_.empty() || !snames_.empty()); }
585
587 CLI11_NODISCARD bool has_description() const { return !description_.empty(); }
588
590 CLI11_NODISCARD const std::string &get_description() const { return description_; }
591
593 Option *description(std::string option_description) {
594 description_ = std::move(option_description);
595 return this;
596 }
597
598 Option *option_text(std::string text) {
599 option_text_ = std::move(text);
600 return this;
601 }
602
603 CLI11_NODISCARD const std::string &get_option_text() const { return option_text_; }
604
608
613 CLI11_NODISCARD std::string get_name(bool positional = false,
614 bool all_options = false
615 ) const;
616
620
623
625 CLI11_NODISCARD const std::string &matching_name(const Option &other) const;
626
628 bool operator==(const Option &other) const { return !matching_name(other).empty(); }
629
631 CLI11_NODISCARD bool check_name(const std::string &name) const;
632
634 CLI11_NODISCARD bool check_sname(std::string name) const {
635 return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
636 }
637
639 CLI11_NODISCARD bool check_lname(std::string name) const {
640 return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
641 }
642
644 CLI11_NODISCARD bool check_fname(std::string name) const {
645 if(fnames_.empty()) {
646 return false;
647 }
648 return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
649 }
650
653 CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const;
654
656 Option *add_result(std::string s);
657
659 Option *add_result(std::string s, int &results_added);
660
662 Option *add_result(std::vector<std::string> s);
663
665 CLI11_NODISCARD const results_t &results() const { return results_; }
666
669
671 template <typename T> void results(T &output) const {
672 bool retval = false;
673 if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
674 const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
675 retval = detail::lexical_conversion<T, T>(res, output);
676 } else {
677 results_t res;
678 if(results_.empty()) {
679 if(!default_str_.empty()) {
680 // _add_results takes an rvalue only
681 _add_result(std::string(default_str_), res);
682 _validate_results(res);
683 results_t extra;
684 _reduce_results(extra, res);
685 if(!extra.empty()) {
686 res = std::move(extra);
687 }
688 } else {
689 res.emplace_back();
690 }
691 } else {
692 res = reduced_results();
693 }
694 retval = detail::lexical_conversion<T, T>(res, output);
695 }
696 if(!retval) {
698 }
699 }
700
702 template <typename T> CLI11_NODISCARD T as() const {
703 T output;
704 results(output);
705 return output;
706 }
707
710
714
716 Option *type_name_fn(std::function<std::string()> typefun) {
717 type_name_ = std::move(typefun);
718 return this;
719 }
720
722 Option *type_name(std::string typeval) {
723 type_name_fn([typeval]() { return typeval; });
724 return this;
725 }
726
728 Option *type_size(int option_type_size);
729
731 Option *type_size(int option_type_size_min, int option_type_size_max);
732
734 void inject_separator(bool value = true) { inject_separator_ = value; }
735
737 Option *default_function(const std::function<std::string()> &func) {
738 default_function_ = func;
739 return this;
740 }
741
746 }
747 return this;
748 }
749
751 Option *default_str(std::string val) {
752 default_str_ = std::move(val);
753 return this;
754 }
755
758 template <typename X> Option *default_val(const X &val) {
759 std::string val_str = detail::to_string(val);
760 auto old_option_state = current_option_state_;
761 results_t old_results{std::move(results_)};
762 results_.clear();
763 try {
764 add_result(val_str);
765 // if trigger_on_result_ is set the callback already ran
767 run_callback(); // run callback sets the state, we need to reset it again
769 } else {
770 _validate_results(results_);
771 current_option_state_ = old_option_state;
772 }
773 } catch(const CLI::Error &) {
774 // this should be done
775 results_ = std::move(old_results);
776 current_option_state_ = old_option_state;
777 throw;
778 }
779 results_ = std::move(old_results);
780 default_str_ = std::move(val_str);
781 return this;
782 }
783
785 CLI11_NODISCARD std::string get_type_name() const;
786
787 private:
789 void _validate_results(results_t &res) const;
790
794 void _reduce_results(results_t &out, const results_t &original) const;
795
796 // Run a result through the Validators
797 std::string _validate(std::string &result, int index) const;
798
800 int _add_result(std::string &&result, std::vector<std::string> &res) const;
801};
802
803// [CLI11:option_hpp:end]
804} // namespace CLI
805
806#ifndef CLI11_COMPILE
807#include "impl/Option_inl.hpp"
808#endif
#define CLI11_NODISCARD
Definition Macros.hpp:47
Creates a command line program, with very few defaults.
Definition App.hpp:88
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition Error.hpp:203
All errors derive from this one.
Definition Error.hpp:71
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition Error.hpp:94
Definition Option.hpp:50
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition Option.hpp:103
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition Option.hpp:156
CRTP * group(const std::string &name)
Changes the group membership.
Definition Option.hpp:88
CRTP * join()
Set the multi option policy to join.
Definition Option.hpp:163
bool always_capture_default_
Automatically capture default value.
Definition Option.hpp:76
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:79
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition Option.hpp:170
CLI11_NODISCARD bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition Option.hpp:134
CLI11_NODISCARD char get_delimiter() const
Get the current delimiter char.
Definition Option.hpp:131
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:116
CRTP * take_first()
Set the multi option policy to take last.
Definition Option.hpp:149
CLI11_NODISCARD bool get_ignore_case() const
The status of ignore case.
Definition Option.hpp:119
bool ignore_case_
Ignore the case when matching (option, not value)
Definition Option.hpp:61
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:178
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition Option.hpp:184
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:137
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:125
bool configurable_
Allow this option to be given in a configuration file.
Definition Option.hpp:67
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:70
bool required_
True if this is a required option.
Definition Option.hpp:58
CRTP * take_last()
Set the multi option policy to take last.
Definition Option.hpp:142
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:73
CRTP * always_capture_default(bool value=true)
Definition Option.hpp:105
std::string group_
The group membership.
Definition Option.hpp:55
CLI11_NODISCARD bool get_ignore_underscore() const
The status of ignore_underscore.
Definition Option.hpp:122
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition Option.hpp:64
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:128
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:113
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
CRTP * required(bool value=true)
Set the option as required.
Definition Option.hpp:97
Definition Option.hpp:192
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition Option.hpp:199
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition Option.hpp:205
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition Option.hpp:211
OptionDefaults()=default
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition Option.hpp:223
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition Option.hpp:217
Definition Option.hpp:229
CLI11_NODISCARD const std::string & get_option_text() const
Definition Option.hpp:603
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:581
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:263
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition Option.hpp:332
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:526
Option * excludes(Option *opt)
Sets excluded options.
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:722
std::function< std::string()> type_name_
Definition Option.hpp:271
option_state
enumeration for the option state machine
Definition Option.hpp:319
@ 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:326
std::string option_text_
If given, replace the text that describes the option type and usage in the help text.
Definition Option.hpp:266
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:284
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:644
std::string pname_
A positional name.
Definition Option.hpp:250
int expected_min_
The minimum number of expected values.
Definition Option.hpp:287
Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
Making an option by hand is not defined, it must be made by the App class.
Definition Option.hpp:342
Option * expected(int value_min, int value_max)
Set the range of expected arguments.
Option * ignore_case(bool value=true)
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:295
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition Option.hpp:737
CLI11_NODISCARD int get_type_size_min() const
The minimum number of arguments the option expects.
Definition Option.hpp:518
bool remove_excludes(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
CLI11_NODISCARD bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:634
bool trigger_on_result_
flag indicating that the option should trigger the validation and callback chain on each result when ...
Definition Option.hpp:336
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:330
Option * add_result(std::string s)
Puts a result at the end.
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:298
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:338
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:709
Option & operator=(const Option &)=delete
CLI11_NODISCARD bool get_force_callback() const
The status of force_callback.
Definition Option.hpp:401
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:247
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
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)
Validator * get_validator(const std::string &Validator_name="")
Get a named Validator.
Option * option_text(std::string text)
Definition Option.hpp:598
CLI11_NODISCARD bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition Option.hpp:410
Option * check(std::function< std::string(const std::string &)> Validator, std::string Validator_description="", std::string Validator_name="")
Adds a Validator. Takes a const string& and returns an error message (empty if conversion/check is ok...
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing)
Definition Option.hpp:535
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:584
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:570
Option * expected(int value)
Set the number of expected arguments.
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:639
Option(const Option &)=delete
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:665
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition Option.hpp:506
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:573
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:237
results_t proc_results_
results after reduction
Definition Option.hpp:317
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:355
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:405
Option * type_size(int option_type_size_min, int option_type_size_max)
Set a custom option type size range.
void inject_separator(bool value=true)
Set the value of the separator injection flag.
Definition Option.hpp:734
Option * allow_extra_args(bool value=true)
Definition Option.hpp:381
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:388
CLI11_NODISCARD callback_t get_callback() const
Get the callback function.
Definition Option.hpp:538
CLI11_NODISCARD bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:523
Option * type_size(int option_type_size)
Set a custom option size.
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:305
CLI11_NODISCARD bool get_trigger_on_parse() const
The status of trigger on parse.
Definition Option.hpp:393
CLI11_NODISCARD const std::vector< std::string > & get_lnames() const
Get the long names.
Definition Option.hpp:541
CLI11_NODISCARD bool check_name(const std::string &name) const
Check a name. Requires "-" or "--" for short / long, supports positional name.
int expected_max_
The maximum number of expected values.
Definition Option.hpp:289
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:532
Option * transform(const std::function< std::string(std::string)> &func, std::string transform_description="", std::string transform_name="")
Adds a Validator-like function that can change result.
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:467
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:578
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:529
std::string description_
The description for help strings.
Definition Option.hpp:260
CLI11_NODISCARD const std::vector< std::string > & get_snames() const
Get the short names.
Definition Option.hpp:544
CLI11_NODISCARD int get_type_size_max() const
The maximum number of arguments the option expects.
Definition Option.hpp:520
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:334
bool remove_needs(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:590
void run_callback()
Process the callback.
CLI11_NODISCARD const std::string & get_single_name() const
Get a single name for the option, first of lname, pname, sname, envname.
Definition Option.hpp:549
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:562
Option * transform(Validator Validator, const std::string &Validator_name="")
Adds a transforming Validator with a built in type name.
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:308
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:358
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:565
void clear()
Clear the parsed results (mostly for testing)
Definition Option.hpp:364
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:567
Option * add_result(std::vector< std::string > s)
Puts a result at the end.
CLI11_NODISCARD const std::string & matching_name(const Option &other) const
If options share any of the same names, find it.
Option * capture_default_str()
Capture the default value from the original value (if it can be captured)
Definition Option.hpp:743
void results(T &output) const
Get the results as a specified type.
Definition Option.hpp:671
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:751
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:515
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:253
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty)
Definition Option.hpp:274
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:386
Option * ignore_underscore(bool value=true)
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:244
std::vector< Validator > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:292
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition Option.hpp:485
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition Option.hpp:716
Option * default_val(const X &val)
Definition Option.hpp:758
Option * add_result(std::string s, int &results_added)
Puts a result at the end and get a count of the number of arguments actually added.
Option * needs(Option *opt)
Sets required options.
Definition Option.hpp:438
int type_size_max_
Definition Option.hpp:282
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:455
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:328
Option * description(std::string option_description)
Set the description.
Definition Option.hpp:593
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:240
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:396
Validator * get_validator(int index)
Get a Validator by index NOTE: this may not be the order of definition.
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional)
Definition Option.hpp:628
Option * check(Validator validator, const std::string &validator_name="")
Adds a Validator with a built in type name.
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...
Option * needs(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:446
CLI11_NODISCARD const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition Option.hpp:547
CLI11_NODISCARD bool has_description() const
True if option has description.
Definition Option.hpp:587
results_t results_
complete Results of parsing
Definition Option.hpp:315
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:702
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:476
Some validators that are provided.
Definition Validators.hpp:53
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition TypeTools.hpp:293
CLI11_INLINE std::tuple< std::vector< std::string >, std::vector< std::string >, std::string > get_names(const std::vector< std::string > &input)
Get a vector of short names, one of long names, and a single name.
std::enable_if< std::is_integral< T >::value, bool >::type checked_multiply(T &a, T b)
Performs a *= b; if it doesn't cause integer overflow. Returns false otherwise.
Definition Validators.hpp:459
constexpr int expected_max_vector_size
Definition StringTools.hpp:45
bool valid_alias_name_string(const std::string &str)
Verify an app name.
Definition StringTools.hpp:162
CLI11_INLINE std::vector< std::string > split_names(std::string current)
CLI11_INLINE std::ptrdiff_t find_member(std::string name, const std::vector< std::string > names, bool ignore_case=false, bool ignore_underscore=false)
Check if a string is a member of a list of strings and optionally ignore case or ignore underscores.
Definition App.hpp:34
std::unique_ptr< Option > Option_p
Definition Option.hpp:36
std::vector< std::string > results_t
Definition Option.hpp:29
MultiOptionPolicy
Enumeration of the multiOption Policy selection.
Definition Option.hpp:38
@ TakeAll
just get all the passed argument regardless
@ TakeFirst
take only the first Expected number of arguments
@ Reverse
take only the last Expected number of arguments in reverse order
@ Throw
Throw an error if any extra arguments were given.
@ TakeLast
take only the last Expected number of arguments
@ Sum
sum all the arguments together if numerical or concatenate directly without delimiter
@ Join
merge all the arguments together into a single string via the delimiter character default(' ')
std::function< bool(const results_t &)> callback_t
callback function definition
Definition Option.hpp:31