CLI11 2.3.2
Loading...
Searching...
No Matches
Option.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2023, 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};
46
49template <typename CRTP> class OptionBase {
50 friend App;
51
52 protected:
54 std::string group_ = std::string("Options");
55
57 bool required_{false};
58
60 bool ignore_case_{false};
61
63 bool ignore_underscore_{false};
64
66 bool configurable_{true};
67
70
72 char delimiter_{'\0'};
73
76
79
81 template <typename T> void copy_to(T *other) const;
82
83 public:
84 // setters
85
87 CRTP *group(const std::string &name) {
89 throw IncorrectConstruction("Group names may not contain newlines or null characters");
90 }
91 group_ = name;
92 return static_cast<CRTP *>(this);
93 }
94
96 CRTP *required(bool value = true) {
97 required_ = value;
98 return static_cast<CRTP *>(this);
99 }
100
102 CRTP *mandatory(bool value = true) { return required(value); }
103
104 CRTP *always_capture_default(bool value = true) {
106 return static_cast<CRTP *>(this);
107 }
108
109 // Getters
110
112 CLI11_NODISCARD const std::string &get_group() const { return group_; }
113
115 CLI11_NODISCARD bool get_required() const { return required_; }
116
119
122
125
128
130 CLI11_NODISCARD char get_delimiter() const { return delimiter_; }
131
134
137
138 // Shortcuts for multi option policy
139
141 CRTP *take_last() {
142 auto *self = static_cast<CRTP *>(this);
143 self->multi_option_policy(MultiOptionPolicy::TakeLast);
144 return self;
145 }
146
148 CRTP *take_first() {
149 auto *self = static_cast<CRTP *>(this);
150 self->multi_option_policy(MultiOptionPolicy::TakeFirst);
151 return self;
152 }
153
155 CRTP *take_all() {
156 auto self = static_cast<CRTP *>(this);
157 self->multi_option_policy(MultiOptionPolicy::TakeAll);
158 return self;
159 }
160
162 CRTP *join() {
163 auto *self = static_cast<CRTP *>(this);
164 self->multi_option_policy(MultiOptionPolicy::Join);
165 return self;
166 }
167
169 CRTP *join(char delim) {
170 auto self = static_cast<CRTP *>(this);
171 self->delimiter_ = delim;
172 self->multi_option_policy(MultiOptionPolicy::Join);
173 return self;
174 }
175
177 CRTP *configurable(bool value = true) {
178 configurable_ = value;
179 return static_cast<CRTP *>(this);
180 }
181
183 CRTP *delimiter(char value = '\0') {
184 delimiter_ = value;
185 return static_cast<CRTP *>(this);
186 }
187};
188
191class OptionDefaults : public OptionBase<OptionDefaults> {
192 public:
193 OptionDefaults() = default;
194
195 // Methods here need a different implementation if they are Option vs. OptionDefault
196
202
204 OptionDefaults *ignore_case(bool value = true) {
205 ignore_case_ = value;
206 return this;
207 }
208
210 OptionDefaults *ignore_underscore(bool value = true) {
211 ignore_underscore_ = value;
212 return this;
213 }
214
218 return this;
219 }
220
222 OptionDefaults *delimiter(char value = '\0') {
223 delimiter_ = value;
224 return this;
225 }
226};
227
228class Option : public OptionBase<Option> {
229 friend App;
230
231 protected:
234
236 std::vector<std::string> snames_{};
237
239 std::vector<std::string> lnames_{};
240
243 std::vector<std::pair<std::string, std::string>> default_flag_values_{};
244
246 std::vector<std::string> fnames_{};
247
249 std::string pname_{};
250
252 std::string envname_{};
253
257
259 std::string description_{};
260
262 std::string default_str_{};
263
265 std::string option_text_{};
266
270 std::function<std::string()> type_name_{[]() { return std::string(); }};
271
273 std::function<std::string()> default_function_{};
274
278
284
289
291 std::vector<Validator> validators_{};
292
294 std::set<Option *> needs_{};
295
297 std::set<Option *> excludes_{};
298
302
304 App *parent_{nullptr};
305
308
312
318 enum class option_state : char {
319 parsing = 0,
320 validated = 2,
321 reduced = 4,
322 callback_run = 6,
323 };
327 bool allow_extra_args_{false};
329 bool flag_like_{false};
333 bool inject_separator_{false};
337 bool force_callback_{false};
339
341 Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
342 : description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
343 std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name));
344 }
345
346 public:
349
350 Option(const Option &) = delete;
351 Option &operator=(const Option &) = delete;
352
354 CLI11_NODISCARD std::size_t count() const { return results_.size(); }
355
357 CLI11_NODISCARD bool empty() const { return results_.empty(); }
358
360 explicit operator bool() const { return !empty() || force_callback_; }
361
363 void clear() {
364 results_.clear();
366 }
367
371
373 Option *expected(int value);
374
376 Option *expected(int value_min, int value_max);
377
380 Option *allow_extra_args(bool value = true) {
381 allow_extra_args_ = value;
382 return this;
383 }
387 Option *trigger_on_parse(bool value = true) {
388 trigger_on_result_ = value;
389 return this;
390 }
393
395 Option *force_callback(bool value = true) {
396 force_callback_ = value;
397 return this;
398 }
401
404 Option *run_callback_for_default(bool value = true) {
406 return this;
407 }
410
412 Option *check(Validator validator, const std::string &validator_name = "");
413
415 Option *check(std::function<std::string(const std::string &)> Validator,
416 std::string Validator_description = "",
417 std::string Validator_name = "");
418
420 Option *transform(Validator Validator, const std::string &Validator_name = "");
421
423 Option *transform(const std::function<std::string(std::string)> &func,
424 std::string transform_description = "",
425 std::string transform_name = "");
426
428 Option *each(const std::function<void(std::string)> &func);
429
431 Validator *get_validator(const std::string &Validator_name = "");
432
435
438 if(opt != this) {
439 needs_.insert(opt);
440 }
441 return this;
442 }
443
445 template <typename T = App> Option *needs(std::string opt_name) {
446 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
447 if(opt == nullptr) {
448 throw IncorrectConstruction::MissingOption(opt_name);
449 }
450 return needs(opt);
451 }
452
454 template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
455 needs(opt);
456 return needs(opt1, args...); // NOLINT(readability-suspicious-call-argument)
457 }
458
461
464
466 template <typename T = App> Option *excludes(std::string opt_name) {
467 auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
468 if(opt == nullptr) {
469 throw IncorrectConstruction::MissingOption(opt_name);
470 }
471 return excludes(opt);
472 }
473
475 template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
476 excludes(opt);
477 return excludes(opt1, args...);
478 }
479
482
484 Option *envname(std::string name) {
485 envname_ = std::move(name);
486 return this;
487 }
488
493 template <typename T = App> Option *ignore_case(bool value = true);
494
499 template <typename T = App> Option *ignore_underscore(bool value = true);
500
503
505 Option *disable_flag_override(bool value = true) {
507 return this;
508 }
512
515
520
523
525 CLI11_NODISCARD std::string get_envname() const { return envname_; }
526
528 CLI11_NODISCARD std::set<Option *> get_needs() const { return needs_; }
529
531 CLI11_NODISCARD std::set<Option *> get_excludes() const { return excludes_; }
532
534 CLI11_NODISCARD std::string get_default_str() const { return default_str_; }
535
538
540 CLI11_NODISCARD const std::vector<std::string> &get_lnames() const { return lnames_; }
541
543 CLI11_NODISCARD const std::vector<std::string> &get_snames() const { return snames_; }
544
546 CLI11_NODISCARD const std::vector<std::string> &get_fnames() const { return fnames_; }
548 CLI11_NODISCARD const std::string &get_single_name() const {
549 if(!lnames_.empty()) {
550 return lnames_[0];
551 }
552 if(!pname_.empty()) {
553 return pname_;
554 }
555 if(!snames_.empty()) {
556 return snames_[0];
557 }
558 return envname_;
559 }
562
567
570
578
580 CLI11_NODISCARD bool get_positional() const { return pname_.length() > 0; }
581
583 CLI11_NODISCARD bool nonpositional() const { return (snames_.size() + lnames_.size()) > 0; }
584
586 CLI11_NODISCARD bool has_description() const { return description_.length() > 0; }
587
589 CLI11_NODISCARD const std::string &get_description() const { return description_; }
590
592 Option *description(std::string option_description) {
593 description_ = std::move(option_description);
594 return this;
595 }
596
597 Option *option_text(std::string text) {
598 option_text_ = std::move(text);
599 return this;
600 }
601
602 CLI11_NODISCARD const std::string &get_option_text() const { return option_text_; }
603
607
612 CLI11_NODISCARD std::string get_name(bool positional = false,
613 bool all_options = false
614 ) const;
615
619
622
624 CLI11_NODISCARD const std::string &matching_name(const Option &other) const;
625
627 bool operator==(const Option &other) const { return !matching_name(other).empty(); }
628
630 CLI11_NODISCARD bool check_name(const std::string &name) const;
631
633 CLI11_NODISCARD bool check_sname(std::string name) const {
634 return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
635 }
636
638 CLI11_NODISCARD bool check_lname(std::string name) const {
639 return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
640 }
641
643 CLI11_NODISCARD bool check_fname(std::string name) const {
644 if(fnames_.empty()) {
645 return false;
646 }
647 return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
648 }
649
652 CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const;
653
655 Option *add_result(std::string s);
656
658 Option *add_result(std::string s, int &results_added);
659
661 Option *add_result(std::vector<std::string> s);
662
664 CLI11_NODISCARD const results_t &results() const { return results_; }
665
668
670 template <typename T> void results(T &output) const {
671 bool retval = false;
672 if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
673 const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
674 retval = detail::lexical_conversion<T, T>(res, output);
675 } else {
676 results_t res;
677 if(results_.empty()) {
678 if(!default_str_.empty()) {
679 // _add_results takes an rvalue only
680 _add_result(std::string(default_str_), res);
681 _validate_results(res);
682 results_t extra;
683 _reduce_results(extra, res);
684 if(!extra.empty()) {
685 res = std::move(extra);
686 }
687 } else {
688 res.emplace_back();
689 }
690 } else {
691 res = reduced_results();
692 }
693 retval = detail::lexical_conversion<T, T>(res, output);
694 }
695 if(!retval) {
697 }
698 }
699
701 template <typename T> CLI11_NODISCARD T as() const {
702 T output;
703 results(output);
704 return output;
705 }
706
709
713
715 Option *type_name_fn(std::function<std::string()> typefun) {
716 type_name_ = std::move(typefun);
717 return this;
718 }
719
721 Option *type_name(std::string typeval) {
722 type_name_fn([typeval]() { return typeval; });
723 return this;
724 }
725
727 Option *type_size(int option_type_size);
728
730 Option *type_size(int option_type_size_min, int option_type_size_max);
731
733 void inject_separator(bool value = true) { inject_separator_ = value; }
734
736 Option *default_function(const std::function<std::string()> &func) {
737 default_function_ = func;
738 return this;
739 }
740
745 }
746 return this;
747 }
748
750 Option *default_str(std::string val) {
751 default_str_ = std::move(val);
752 return this;
753 }
754
757 template <typename X> Option *default_val(const X &val) {
758 std::string val_str = detail::to_string(val);
759 auto old_option_state = current_option_state_;
760 results_t old_results{std::move(results_)};
761 results_.clear();
762 try {
763 add_result(val_str);
764 // if trigger_on_result_ is set the callback already ran
766 run_callback(); // run callback sets the state, we need to reset it again
768 } else {
769 _validate_results(results_);
770 current_option_state_ = old_option_state;
771 }
772 } catch(const CLI::Error &) {
773 // this should be done
774 results_ = std::move(old_results);
775 current_option_state_ = old_option_state;
776 throw;
777 }
778 results_ = std::move(old_results);
779 default_str_ = std::move(val_str);
780 return this;
781 }
782
784 CLI11_NODISCARD std::string get_type_name() const;
785
786 private:
788 void _validate_results(results_t &res) const;
789
793 void _reduce_results(results_t &out, const results_t &original) const;
794
795 // Run a result through the Validators
796 std::string _validate(std::string &result, int index) const;
797
799 int _add_result(std::string &&result, std::vector<std::string> &res) const;
800};
801
802// [CLI11:option_hpp:end]
803} // namespace CLI
804
805#ifndef CLI11_COMPILE
806#include "impl/Option_inl.hpp"
807#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:197
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:49
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition Option.hpp:102
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition Option.hpp:155
CRTP * group(const std::string &name)
Changes the group membership.
Definition Option.hpp:87
CRTP * join()
Set the multi option policy to join.
Definition Option.hpp:162
bool always_capture_default_
Automatically capture default value.
Definition Option.hpp:75
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:78
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition Option.hpp:169
CLI11_NODISCARD bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition Option.hpp:133
CLI11_NODISCARD char get_delimiter() const
Get the current delimiter char.
Definition Option.hpp:130
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:115
CRTP * take_first()
Set the multi option policy to take last.
Definition Option.hpp:148
CLI11_NODISCARD bool get_ignore_case() const
The status of ignore case.
Definition Option.hpp:118
bool ignore_case_
Ignore the case when matching (option, not value)
Definition Option.hpp:60
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:177
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition Option.hpp:183
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:136
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:124
bool configurable_
Allow this option to be given in a configuration file.
Definition Option.hpp:66
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:69
bool required_
True if this is a required option.
Definition Option.hpp:57
CRTP * take_last()
Set the multi option policy to take last.
Definition Option.hpp:141
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:72
CRTP * always_capture_default(bool value=true)
Definition Option.hpp:104
std::string group_
The group membership.
Definition Option.hpp:54
CLI11_NODISCARD bool get_ignore_underscore() const
The status of ignore_underscore.
Definition Option.hpp:121
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition Option.hpp:63
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:127
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:112
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:96
Definition Option.hpp:191
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition Option.hpp:198
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition Option.hpp:204
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition Option.hpp:210
OptionDefaults()=default
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition Option.hpp:222
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition Option.hpp:216
Definition Option.hpp:228
CLI11_NODISCARD const std::string & get_option_text() const
Definition Option.hpp:602
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:580
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:262
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition Option.hpp:331
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:525
Option * excludes(Option *opt)
Sets excluded options.
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:721
std::function< std::string()> type_name_
Definition Option.hpp:270
option_state
enumeration for the option state machine
Definition Option.hpp:318
@ 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:325
std::string option_text_
If given, replace the text that describes the option type and usage in the help text.
Definition Option.hpp:265
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:283
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:643
std::string pname_
A positional name.
Definition Option.hpp:249
int expected_min_
The minimum number of expected values.
Definition Option.hpp:286
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:341
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:294
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition Option.hpp:736
CLI11_NODISCARD int get_type_size_min() const
The minimum number of arguments the option expects.
Definition Option.hpp:517
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:633
bool trigger_on_result_
flag indicating that the option should trigger the validation and callback chain on each result when ...
Definition Option.hpp:335
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:329
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:297
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:337
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:708
Option & operator=(const Option &)=delete
CLI11_NODISCARD bool get_force_callback() const
The status of force_callback.
Definition Option.hpp:400
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:246
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:597
CLI11_NODISCARD bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition Option.hpp:409
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:534
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:583
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:569
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:638
Option(const Option &)=delete
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:664
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition Option.hpp:505
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:572
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:236
results_t proc_results_
results after reduction
Definition Option.hpp:316
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:354
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:404
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:733
Option * allow_extra_args(bool value=true)
Definition Option.hpp:380
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:387
CLI11_NODISCARD callback_t get_callback() const
Get the callback function.
Definition Option.hpp:537
CLI11_NODISCARD bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:522
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:304
CLI11_NODISCARD bool get_trigger_on_parse() const
The status of trigger on parse.
Definition Option.hpp:392
CLI11_NODISCARD const std::vector< std::string > & get_lnames() const
Get the long names.
Definition Option.hpp:540
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:288
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:531
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:466
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:577
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:528
std::string description_
The description for help strings.
Definition Option.hpp:259
CLI11_NODISCARD const std::vector< std::string > & get_snames() const
Get the short names.
Definition Option.hpp:543
CLI11_NODISCARD int get_type_size_max() const
The maximum number of arguments the option expects.
Definition Option.hpp:519
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:333
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:589
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:548
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:561
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:307
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:357
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:564
void clear()
Clear the parsed results (mostly for testing)
Definition Option.hpp:363
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:566
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:742
void results(T &output) const
Get the results as a specified type.
Definition Option.hpp:670
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:750
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:514
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:252
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty)
Definition Option.hpp:273
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:385
Option * ignore_underscore(bool value=true)
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:243
std::vector< Validator > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:291
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition Option.hpp:484
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition Option.hpp:715
Option * default_val(const X &val)
Definition Option.hpp:757
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:437
int type_size_max_
Definition Option.hpp:281
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:454
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:327
Option * description(std::string option_description)
Set the description.
Definition Option.hpp:592
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:239
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:395
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:627
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:445
CLI11_NODISCARD const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition Option.hpp:546
CLI11_NODISCARD bool has_description() const
True if option has description.
Definition Option.hpp:586
results_t results_
complete Results of parsing
Definition Option.hpp:314
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:701
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:475
Some validators that are provided.
Definition Validators.hpp:81
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:281
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:479
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:157
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
@ 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