From 72394c1b967be64071eef00afebe9541d45dca24 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Thu, 22 Aug 2024 17:51:31 +0300 Subject: [PATCH] app-template: add ability to specify options_description to be used for --help Hiding options from --help output is useful when deprecating options. When using boost::program_options, the way to hide options from display is to provide an alternative options_description object and print that. So this patch adds a way to provide a help-specific options_description. While this entangles us even more with boost::program_options, I don't see a simple alternative. --- include/seastar/core/app-template.hh | 3 +++ src/core/app-template.cc | 35 +++++++++++++++++++--------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/include/seastar/core/app-template.hh b/include/seastar/core/app-template.hh index 8c5a15c8c28..47aef9c0f43 100644 --- a/include/seastar/core/app-template.hh +++ b/include/seastar/core/app-template.hh @@ -129,6 +129,7 @@ private: std::shared_ptr _smp; seastar_options _opts; boost::program_options::options_description _app_opts; + boost::program_options::options_description* _app_visible_opts_for_help = nullptr; boost::program_options::options_description _seastar_opts; boost::program_options::options_description _opts_conf_file; boost::program_options::positional_options_description _pos_opts; @@ -136,6 +137,7 @@ private: configuration_reader _conf_reader; configuration_reader get_default_configuration_reader(); + void add_extra_options(boost::program_options::options_description& opts); public: struct positional_option { const char* name; @@ -153,6 +155,7 @@ public: boost::program_options::options_description& get_options_description(); boost::program_options::options_description& get_conf_file_options_description(); boost::program_options::options_description_easy_init add_options(); + void set_app_visible_options_for_help(boost::program_options::options_description* opts); void add_positional_options(std::initializer_list options); boost::program_options::variables_map& configuration(); int run_deprecated(int ac, char ** av, std::function&& func) noexcept; diff --git a/src/core/app-template.cc b/src/core/app-template.cc index af76bda3721..5dcba0058b2 100644 --- a/src/core/app-template.cc +++ b/src/core/app-template.cc @@ -85,16 +85,7 @@ app_template::app_template(app_template::seastar_options opts) if (!alien::internal::default_instance) { alien::internal::default_instance = _alien.get(); } - _app_opts.add_options() - ("help,h", "show help message") - ; - _app_opts.add_options() - ("help-seastar", "show help message about seastar options") - ; - _app_opts.add_options() - ("help-loggers", "print a list of logger names and exit") - ; - + add_extra_options(_app_opts); { program_options::options_description_building_visitor visitor; _opts.describe(visitor); @@ -109,12 +100,33 @@ app_template::app_template(app_template::config cfg) { } +void +app_template::add_extra_options(boost::program_options::options_description& opts) { + opts.add_options() + ("help,h", "show help message") + ; + opts.add_options() + ("help-seastar", "show help message about seastar options") + ; + opts.add_options() + ("help-loggers", "print a list of logger names and exit") + ; +} + app_template::~app_template() = default; const app_template::seastar_options& app_template::options() const { return _opts; } +void +app_template::set_app_visible_options_for_help(boost::program_options::options_description* opts) { + _app_visible_opts_for_help = opts; + if (_app_visible_opts_for_help) { + add_extra_options(*_app_visible_opts_for_help); + } +} + app_template::configuration_reader app_template::get_default_configuration_reader() { return [this] (bpo::variables_map& configuration) { auto home = std::getenv("HOME"); @@ -211,7 +223,8 @@ app_template::run_deprecated(int ac, char ** av, std::function&& func) if (!_opts.description.empty()) { std::cout << _opts.description << "\n"; } - std::cout << _app_opts << "\n"; + auto& which_opts = _app_visible_opts_for_help ? *_app_visible_opts_for_help : _app_opts; + std::cout << which_opts << "\n"; return 0; } if (configuration.count("help-seastar")) {