• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.67
/libs/core/command_line_handling_local/src/late_command_line_handling_local.cpp
1
//  Copyright (c) 2007-2025 Hartmut Kaiser
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/assert.hpp>
8
#include <hpx/command_line_handling_local/late_command_line_handling_local.hpp>
9
#include <hpx/command_line_handling_local/parse_command_line_local.hpp>
10
#include <hpx/modules/format.hpp>
11
#include <hpx/modules/program_options.hpp>
12
#include <hpx/modules/runtime_configuration.hpp>
13

14
#include <cstddef>
15
#include <iostream>
16
#include <string>
17
#include <vector>
18

19
namespace hpx::local::detail {
20

21
    std::string enquote(std::string arg)
275✔
22
    {
23
        if (arg.find_first_of(" \t\"") != std::string::npos)
275✔
24
        {
25
#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 110000
×
26
#pragma GCC diagnostic push
27
#pragma GCC diagnostic ignored "-Wrestrict"
275✔
28
#endif
29
            return std::string("\"") + HPX_MOVE(arg) + "\"";
30
#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 110000
×
31
#pragma GCC diagnostic pop
32
#endif
33
        }
×
34
        return arg;
35
    }
×
36

37
    void decode(std::string& str, char const* s, char const* r)
×
38
    {
39
        std::string::size_type pos = 0;
×
40
        while ((pos = str.find(s, pos)) != std::string::npos)
41
        {
×
42
            str.replace(pos, 2, r);
×
43
        }
44
    }
45

1✔
46
    std::string decode_string(std::string str)
47
    {
48
        decode(str, "\\n", "\n");
49
        return str;
1✔
50
    }
51

×
52
    void set_unknown_commandline_options(util::runtime_configuration& ini,
53
        std::vector<std::string> const& still_unregistered_options)
54
    {
55
        std::string still_unknown_commandline;
×
56
        for (std::size_t i = 1; i < still_unregistered_options.size(); ++i)
57
        {
58
            if (i != 1)
1✔
59
            {
60
                still_unknown_commandline += " ";
×
61
            }
62
            still_unknown_commandline += enquote(still_unregistered_options[i]);
×
63
        }
64

1✔
65
        if (!still_unknown_commandline.empty())
66
        {
64✔
67
            util::section* s = ini.get_section("hpx");
68
            HPX_ASSERT(s != nullptr);
69
            s->add_entry("unknown_cmd_line_option", still_unknown_commandline);
128✔
70
        }
71
    }
72

×
73
    bool handle_full_help(util::runtime_configuration const& ini,
×
74
        hpx::program_options::options_description const& options)
×
75
    {
76
        if (std::string const fullhelp(ini.get_entry("hpx.cmd_line_help", ""));
×
77
            !fullhelp.empty())
×
78
        {
79
            if (std::string const help_option(
80
                    ini.get_entry("hpx.cmd_line_help_option", ""));
81
                0 == std::string("full").find(help_option))
82
            {
×
83
                std::cout << decode_string(fullhelp);
84
                std::cout << options << std::endl;
85
            }
86
            else
64✔
87
            {
88
                throw hpx::detail::command_line_error(
89
                    "unknown help option: " + help_option);
64✔
90
            }
91
            return true;
192✔
92
        }
192✔
93
        return false;
128✔
94
    }
192✔
95

96
    std::string get_full_commandline(util::runtime_configuration const& ini)
97
    {
64✔
98
        return ini.get_entry("hpx.commandline.command", "") + " " +
99
            ini.get_entry("hpx.commandline.prepend_options", "") +
100
            ini.get_entry("hpx.commandline.options", "") +
101
            ini.get_entry("hpx.commandline.config_options", "");
192✔
102
    }
103

×
104
    bool handle_late_options(util::runtime_configuration const& ini,
×
105
        hpx::program_options::variables_map const& vm,
×
106
        void (*handle_print_bind)(std::size_t))
107
    {
108
        if (handle_print_bind != nullptr && vm.count("hpx:print-bind"))
128✔
109
        {
110
            std::size_t const num_threads = hpx::util::from_string<std::size_t>(
×
111
                ini.get_entry("hpx.os_threads", 1));
112
            handle_print_bind(num_threads);
113
        }
114

115
        if (vm.count("hpx:exit"))
116
        {
32✔
117
            return true;
118
        }
119

120
        return false;
121
    }
122

123
    int handle_late_commandline_options(util::runtime_configuration& ini,
32✔
124
        hpx::program_options::options_description const& options,
64✔
125
        void (*handle_print_bind)(std::size_t))
126
    {
127
        // do secondary command line processing, check validity of options only
128
        try
129
        {
×
130
            if (std::string const unknown_cmd_line(
×
131
                    ini.get_entry("hpx.unknown_cmd_line", ""));
132
                !unknown_cmd_line.empty())
133
            {
134
                util::commandline_error_mode mode =
135
                    util::commandline_error_mode::rethrow_on_error;
136
                if (std::string const allow_unknown(
×
137
                        ini.get_entry("hpx.commandline.allow_unknown", "0"));
×
138
                    allow_unknown != "0")
×
139
                {
140
                    mode |= util::commandline_error_mode::allow_unregistered;
141
                }
×
142

143
                hpx::program_options::variables_map vm;
×
144
                std::vector<std::string> still_unregistered_options;
145
                parse_commandline(ini, options, unknown_cmd_line, vm, mode,
32✔
146
                    nullptr, &still_unregistered_options);
147

148
                set_unknown_commandline_options(
149
                    ini, still_unregistered_options);
150
            }
151

152
            if (handle_full_help(ini, options))
32✔
153
            {
154
                return 1;
155
            }
32✔
156

157
            // secondary command line handling, looking for --exit and other
32✔
158
            // options
159
            if (std::string const cmd_line(get_full_commandline(ini));
160
                !cmd_line.empty())
161
            {
162
                hpx::program_options::variables_map vm;
32✔
163

164
                parse_commandline(ini, options, cmd_line, vm,
165
                    util::commandline_error_mode::allow_unregistered |
166
                        util::commandline_error_mode::
32✔
167
                            report_missing_config_file);
168

×
169
                if (handle_late_options(ini, vm, handle_print_bind))
170
                {
171
                    return 1;
172
                }
×
173
            }
174
        }
×
175
        catch (std::exception const& e)
176
        {
32✔
177
            std::cerr
178
                << "handle_late_commandline_options: command line processing: "
179
                << e.what() << std::endl;
180
            return -1;
181
        }
182

183
        return 0;
184
    }
185
}    // namespace hpx::local::detail
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc