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

Add --mir-version command line option. solves MirServer/mir#3058 #3130

Merged
merged 7 commits into from
Nov 17, 2023
21 changes: 15 additions & 6 deletions src/platform/options/default_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ void mo::DefaultConfiguration::parse_arguments(
{
desc.add_options()
("help,h", "this help text");
desc.add_options()
("version,V", "display Mir version and exit");

options.parse_arguments(desc, argc, argv);

Expand All @@ -303,12 +305,19 @@ void mo::DefaultConfiguration::parse_arguments(
for (auto const& token : unparsed_arguments)
tokens.push_back(token.c_str());
if (!tokens.empty()) unparsed_arguments_handler(tokens.size(), tokens.data());

if (options.is_set("help"))
{
std::ostringstream help_text;
help_text << desc;
BOOST_THROW_EXCEPTION(mir::ExitWithOutput(help_text.str()));

// Handle one-off options such as `help` or `version`
std::map<const char *,std::function<std::ostringstream & (std::ostringstream &)>> oneoff_options {
{"version",[&](std::ostringstream & oneoff_output) -> std::ostringstream & {oneoff_output <<MIR_VERSION_MAJOR << "." << MIR_VERSION_MINOR << "." << MIR_VERSION_MICRO << std::endl; return oneoff_output;}},
{"help",[&](std::ostringstream & oneoff_output) -> std::ostringstream & {oneoff_output << desc; return oneoff_output;}}
};
for(auto oopt : oneoff_options) {
if(options.is_set(oopt.first))
{
std::ostringstream oneoff_output;
// Intentionally throw exception for non-error condition as per ExitWithOutput documentation.
BOOST_THROW_EXCEPTION(mir::ExitWithOutput(oopt.second(oneoff_output).str()));
}
pillowtrucker marked this conversation as resolved.
Show resolved Hide resolved
}
}
catch (po::error const& error)
Expand Down