Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to parsed options #30

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion application_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class application_impl {
options_description _app_options;
options_description _cfg_options;
variables_map _options;
std::vector<bpo::basic_option<char>> _parsed_options;

std::filesystem::path _data_dir{"data-dir"};
std::filesystem::path _config_dir{"config-dir"};
Expand Down Expand Up @@ -259,6 +260,7 @@ bool application_base::initialize_impl(int argc, char** argv, vector<abstract_pl
bpo::variables_map& options = my->_options;
try {
bpo::parsed_options parsed = bpo::command_line_parser(argc, argv).options(my->_app_options).run();
my->_parsed_options = parsed.options;
bpo::store(parsed, options);
vector<string> positionals = bpo::collect_unrecognized(parsed.options, bpo::include_positional);
if(!positionals.empty())
Expand Down Expand Up @@ -335,6 +337,8 @@ bool application_base::initialize_impl(int argc, char** argv, vector<abstract_pl
std::vector< bpo::basic_option<char> > opts_from_config;
try {
bpo::parsed_options parsed_opts_from_config = bpo::parse_config_file<char>(my->_config_file_name.make_preferred().string().c_str(), my->_cfg_options, false);
my->_parsed_options.reserve(my->_parsed_options.size() + parsed_opts_from_config.options.size());
my->_parsed_options.insert(my->_parsed_options.end(), parsed_opts_from_config.options.begin(), parsed_opts_from_config.options.end());
bpo::store(parsed_opts_from_config, options);
opts_from_config = parsed_opts_from_config.options;
} catch( const boost::program_options::unknown_option& e ) {
Expand Down Expand Up @@ -589,7 +593,11 @@ void application_base::set_sighup_callback(std::function<void()> callback) {
}

const bpo::variables_map& application_base::get_options() const{
return my->_options;
return my->_options;
}

const std::vector<bpo::basic_option<char>>& application_base::get_parsed_options() const {
return my->_parsed_options;
}

} /// namespace appbase
Expand Down
2 changes: 2 additions & 0 deletions include/appbase/application_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <appbase/channel.hpp>
#include <appbase/method.hpp>
#include <boost/core/demangle.hpp>
#include <boost/program_options/option.hpp>
#include <typeindex>
#include <exception>
#include <string_view>
Expand Down Expand Up @@ -254,6 +255,7 @@ class application_base {
}

const bpo::variables_map& get_options() const;
const std::vector<bpo::basic_option<char>>& get_parsed_options() const;

/**
* Set the current thread schedule priority to maximum.
Expand Down