Skip to content

Commit

Permalink
Making sure --hpx:help does not throw for required (but missing) argu…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
hkaiser committed Nov 6, 2017
1 parent f7b4663 commit 1d65967
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/util/command_line_handling.cpp
Expand Up @@ -1163,6 +1163,13 @@ namespace hpx { namespace util
for (std::string const& e : ini_config_)
rtcfg_.parse("<user supplied config>", e, true, false);

// support re-throwing command line exceptions for testing purposes
int error_mode = util::allow_unregistered;
if (cfgmap.get_value("hpx.commandline.rethrow_errors", 0) != 0)
{
error_mode |= util::rethrow_on_error;
}

// Initial analysis of the command line options. This is
// preliminary as it will not take into account any aliases as
// defined in any of the runtime configuration files.
Expand All @@ -1173,7 +1180,7 @@ namespace hpx { namespace util
// command line handling.
boost::program_options::variables_map prevm;
if (!util::parse_commandline(rtcfg_, desc_cmdline, argc,
argv, prevm, std::size_t(-1), util::allow_unregistered,
argv, prevm, std::size_t(-1), error_mode,
rtcfg_.mode_))
{
return -1;
Expand Down Expand Up @@ -1224,7 +1231,7 @@ namespace hpx { namespace util
std::vector<std::string> unregistered_options;

if (!util::parse_commandline(rtcfg_, desc_cmdline,
argc, argv, vm_, node_, util::allow_unregistered, rtcfg_.mode_,
argc, argv, vm_, node_, error_mode, rtcfg_.mode_,
&help, &unregistered_options))
{
return -1;
Expand Down
24 changes: 14 additions & 10 deletions src/util/parse_command_line.cpp
Expand Up @@ -649,24 +649,28 @@ namespace hpx { namespace util
store(opts, vm);
}

if (vm.count("hpx:help"))
{
// collect help information
if (visible) {
(*visible)
.add(app_options).add(cmdline_options)
.add(hpx_options).add(counter_options)
.add(debugging_options).add(config_options)
;
}
return true;
}

notify(vm);

detail::handle_generic_config_options(
argv[0], vm, desc_cfgfile, rtcfg, node, error_mode);
detail::handle_config_options(
vm, desc_cfgfile, rtcfg, node, error_mode);

// collect help information
if (visible && vm.count("hpx:help")) {
(*visible)
.add(app_options).add(cmdline_options)
.add(hpx_options).add(counter_options)
.add(debugging_options).add(config_options)
;
}
}
catch (std::exception const& e) {
if (error_mode == rethrow_on_error)
if (error_mode & rethrow_on_error)
throw;

std::cerr << "hpx::init: exception caught: "
Expand Down
1 change: 1 addition & 0 deletions tests/regressions/util/CMakeLists.txt
Expand Up @@ -6,6 +6,7 @@

set(tests
command_line_arguments_706
command_line_required_arguments_2990
configuration_1572
function_argument
function_serialization_728
Expand Down
50 changes: 50 additions & 0 deletions tests/regressions/util/command_line_required_arguments_2990.cpp
@@ -0,0 +1,50 @@
// Copyright (c) 2017 Igor Krivenko
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <hpx/hpx_init.hpp>
#include <hpx/hpx.hpp>

#include <iostream>
#include <string>
#include <vector>

using namespace boost::program_options;

int hpx_main(variables_map& vm)
{
std::cout << "Value of reqopt1: " << vm["reqopt1"].as<int>() << std::endl;
std::cout << "Value of reqopt2: " << vm["reqopt2"].as<double>()
<< std::endl;
std::cout << "Value of reqopt3: " << vm["reqopt3"].as<std::string>()
<< std::endl;

return hpx::finalize();
}

int main(int argc, char* argv[])
{
std::vector<std::string> cfg = {
"hpx.commandline.rethrow_errors!=1"
};

char help_option[] = "--hpx:help";

std::vector<char*> newargv;
for (int i = 0; i != argc; ++i)
{
newargv.push_back(argv[i]);
}
newargv.push_back(help_option);
newargv.push_back(nullptr);

options_description op("Issue #2990\n\nUsage: issue2990 [options]");

op.add_options()
("reqopt1", value<int>()->required(), "Required option 1")
("reqopt2", value<double>()->required(), "Required option 2")
("reqopt3", value<std::string>()->required(), "Required option 3");

return hpx::init(op, argc + 1, newargv.data(), cfg);
}

0 comments on commit 1d65967

Please sign in to comment.