24#include <seastar/core/format.hh>
25#include <seastar/core/sstring.hh>
26#include <seastar/util/modules.hh>
29#include <fmt/format.h>
31#include <boost/any.hpp>
32#include <boost/intrusive/list.hpp>
35#include <unordered_map>
59namespace program_options {
74class string_map final :
private std::unordered_map<sstring, sstring> {
76 using base = std::unordered_map<sstring, sstring>;
78 using base::value_type;
80 using base::mapped_type;
88 using base::operator[];
97 return static_cast<const string_map::base&
>(lhs) ==
static_cast<const string_map::base&
>(rhs);
100inline bool operator!=(
const string_map& lhs,
const string_map& rhs) {
101 return !(lhs == rhs);
107sstring get_or_default(
const string_map&,
const sstring& key,
const sstring& def = sstring());
109std::istream& operator>>(std::istream& is, string_map&);
110std::ostream& operator<<(std::ostream& os,
const string_map&);
117void validate(boost::any& out,
const std::vector<std::string>& in, string_map*,
int);
119using list_base_hook = boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
123SEASTAR_MODULE_EXPORT_BEGIN
125enum class logger_timestamp_style;
126enum class logger_ostream_type;
131namespace program_options {
182 virtual void visit_value(
const std::set<unsigned>* default_val) = 0;
186 virtual void visit_value(
const logger_timestamp_style* default_val) = 0;
188 virtual void visit_value(
const logger_ostream_type* default_val) = 0;
192 virtual void visit_value(
const std::unordered_map<sstring, log_level>* default_val) = 0;
195 virtual void visit_selection_value(
const std::vector<std::string>& candidate_names,
const std::size_t* default_candidate) = 0;
250 virtual bool visit_value(std::unordered_map<sstring, log_level>& val) = 0;
253 virtual bool visit_selection_value(
const std::vector<std::string>& candidate_names, std::size_t& selected_candidate) = 0;
297 using value_list_type = boost::intrusive::list<
299 boost::intrusive::base_hook<list_base_hook>,
300 boost::intrusive::constant_time_size<false>>;
302 using option_group_list_type = boost::intrusive::list<
304 boost::intrusive::base_hook<list_base_hook>,
305 boost::intrusive::constant_time_size<false>>;
311 value_list_type _values;
312 option_group_list_type _subgroups;
335 operator bool ()
const {
return !_values.empty(); }
336 bool used()
const {
return _used; }
337 const std::string& name()
const {
return _name; }
338 const value_list_type& values()
const {
return _values; }
339 value_list_type& values() {
return _values; }
390 std::string _description;
405 bool used()
const {
return _used; }
406 const std::string& name()
const {
return _name; }
407 const std::string& description()
const {
return _description; }
416template <
typename T = std::monostate>
418 std::optional<T> _value;
419 bool _defaulted =
true;
423 auto* val = _value ? &*_value :
nullptr;
429 _value = std::move(val);
434 _value = std::move(
value);
445 value(
option_group& group, std::string name, std::optional<T> default_value, std::string description)
447 , _value(
std::move(default_value))
455 operator bool ()
const {
return bool(_value); }
460 T&
get_value() {
return _value.value(); }
461 void set_default_value(T
value) { do_set_value(std::move(
value),
true); }
462 void set_value(T
value) { do_set_value(std::move(
value),
false); }
470 std::optional<bool> _set;
478 if (_set.has_value()) {
502 operator bool ()
const {
return _set ? _set.value() :
false; }
503 void set_value() { _set =
true; }
504 void unset_value() { _set =
false; }
517template <
typename T = std::monostate>
520 using deleter = std::function<void(T*)>;
521 using value_handle = std::unique_ptr<T, deleter>;
525 std::unique_ptr<option_group> opts;
527 using candidates = std::vector<candidate>;
530 static constexpr size_t no_selected_candidate = -1;
533 candidates _candidates;
534 size_t _selected_candidate = no_selected_candidate;
535 bool _defaulted =
true;
538 std::vector<std::string> get_candidate_names()
const {
539 std::vector<std::string> candidate_names;
540 candidate_names.reserve(_candidates.size());
541 for (
const auto& c : _candidates) {
542 candidate_names.push_back(c.name);
544 return candidate_names;
547 virtual void do_describe(options_descriptor& descriptor)
const override {
548 descriptor.visit_selection_value(get_candidate_names(), _selected_candidate == no_selected_candidate ?
nullptr : &_selected_candidate);
549 for (
auto& c : _candidates) {
551 c.opts->describe(descriptor);
555 virtual void do_mutate(options_mutator& mutator)
override {
556 if (mutator.visit_selection_value(get_candidate_names(), _selected_candidate)) {
559 if (_selected_candidate != no_selected_candidate) {
560 auto& c = _candidates.at(_selected_candidate);
562 c.opts->mutate(mutator);
566 size_t find_candidate(
const std::string& candidate_name) {
567 auto it = find_if(_candidates.begin(), _candidates.end(), [&] (
const auto& candidate) {
568 return candidate.name == candidate_name;
570 if (it == _candidates.end()) {
571 throw std::invalid_argument(fmt::format(
"find_candidate(): failed to find candidate {}", candidate_name));
573 return it - _candidates.begin();
576 option_group* do_select_candidate(std::string candidate_name,
bool defaulted) {
577 _selected_candidate = find_candidate(candidate_name);
579 return _candidates.at(_selected_candidate).opts.get();
592 , _candidates(
std::move(candidates))
593 , _selected_candidate(find_candidate(default_candidate))
597 , _candidates(
std::move(candidates))
604 operator bool ()
const {
return _selected_candidate != no_selected_candidate; }
613 T& get_selected_candidate()
const {
return *_candidates.at(_selected_candidate).value; }
627SEASTAR_MODULE_EXPORT_END
Definition: program-options.hh:383
Definition: program-options.hh:293
void mutate(options_mutator &mutator)
option_group(option_group *parent, std::string name, unused)
void describe(options_descriptor &descriptor) const
option_group(option_group *parent, std::string name)
Definition: program-options.hh:143
virtual void visit_value(const std::set< unsigned > *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const logger_ostream_type *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const double *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const std::unordered_map< sstring, log_level > *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_selection_value(const std::vector< std::string > &candidate_names, const std::size_t *default_candidate)=0
Visit a selection value (selection_value), default_candidate is null when there is no default candida...
virtual bool visit_value_metadata(const std::string &name, const std::string &description, bool used)=0
virtual void visit_value(const log_level *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const float *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_group_end()=0
virtual void visit_value(const memory::alloc_failure_kind *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const std::string *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const unsigned *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const int *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual bool visit_group_start(const std::string &name, bool used)=0
virtual void visit_value()=0
Visit a switch (value<std::monostate>).
virtual void visit_value(const logger_timestamp_style *default_val)=0
Visit a value (value), default_val is null when value has no default.
virtual void visit_value(const bool *default_val)=0
Visit a value (value), default_val is null when value has no default.
Definition: program-options.hh:201
virtual bool visit_value(std::set< unsigned > &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_value(std::unordered_map< sstring, log_level > &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_value_metadata(const std::string &name, bool used)=0
virtual bool visit_value(logger_ostream_type &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual void visit_group_end()=0
virtual bool visit_value()=0
Visit a switch (value<std::monostate>), switch is set to returned value.
virtual bool visit_value(double &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_value(float &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_group_start(const std::string &name, bool used)=0
virtual bool visit_value(unsigned &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_selection_value(const std::vector< std::string > &candidate_names, std::size_t &selected_candidate)=0
Visit and optionally mutate a selection value (selection_value), should return true if value was muta...
virtual bool visit_value(memory::alloc_failure_kind &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_value(logger_timestamp_style &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_value(int &val)=0
Visit a value (value), default_val is null when value has no default.
virtual bool visit_value(std::string &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_value(log_level &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
virtual bool visit_value(bool &val)=0
Visit and optionally mutate a value (value), should return true if value was mutated.
Definition: program-options.hh:518
option_group * select_default_candidate(std::string candidate_name)
Definition: program-options.hh:621
selection_value(option_group &group, std::string name, unused)
Construct an unused value.
Definition: program-options.hh:600
option_group * get_selected_candidate_opts()
Get the options of the currently selected candidate (assumes there is one selected,...
Definition: program-options.hh:612
option_group * select_candidate(std::string candidate_name)
Definition: program-options.hh:617
bool defaulted() const
Is the currently selected candidate the default one?
Definition: program-options.hh:606
selection_value(option_group &group, std::string name, candidates candidates, std::string default_candidate, std::string description)
Definition: program-options.hh:590
const option_group * get_selected_candidate_opts() const
Get the options of the currently selected candidate (assumes there is one selected,...
Definition: program-options.hh:610
const std::string & get_selected_candidate_name() const
Get the name of the currently selected candidate (assumes there is one selected, see \operator bool()...
Definition: program-options.hh:608
Definition: program-options.hh:522
Wrapper for command-line options with arbitrary string associations.
Definition: program-options.hh:74
value(option_group &group, std::string name, unused)
Construct an unused value.
Definition: program-options.hh:498
value(option_group &group, std::string name, std::string description)
Definition: program-options.hh:494
Definition: program-options.hh:417
value(option_group &group, std::string name, std::optional< T > default_value, std::string description)
Definition: program-options.hh:445
const T & get_value() const
Return the contained value, assumes there is one, see operator bool().
Definition: program-options.hh:459
bool defaulted() const
Does this value still contain a default-value?
Definition: program-options.hh:457
value(option_group &group, std::string name, unused)
Construct an unused value.
Definition: program-options.hh:450
log_level
log level used with
Definition: log.hh:55
A tag type used to construct unused option_group and basic_value objects.
Definition: program-options.hh:257
alloc_failure_kind
The kind of allocation failures to dump diagnostics report for.
Definition: memory_diagnostics.hh:41
Seastar API namespace.
Definition: abort_on_ebadf.hh:26