• 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

0.0
/libs/core/program_options/include/hpx/program_options/errors.hpp
1
//  Copyright Vladimir Prus 2002-2004.
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
#pragma once
8

9
#include <hpx/program_options/config.hpp>
10

11
#include <map>
12
#include <stdexcept>
13
#include <string>
14
#include <utility>
15
#include <vector>
16

17
#include <hpx/config/warnings_prefix.hpp>
18

19
namespace hpx::program_options {
20

21
    /** Base class for all errors in the library. */
22
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT error : public std::logic_error
×
23
    {
24
    public:
25
        explicit error(std::string const& xwhat);
26
    };
×
27

28
    /** Class thrown when there are too many positional options.
29
        This is a programming error.
30
    */
31
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT too_many_positional_options_error
32
      : public error
33
    {
34
    public:
35
        too_many_positional_options_error();
36
    };
37

×
38
    /** Class thrown when there are programming error related to style */
×
39
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT invalid_command_line_style
×
40
      : public error
41
    {
×
42
    public:
43
        explicit invalid_command_line_style(std::string const& msg);
44
    };
45

46
    /** Class thrown if config file can not be read */
47
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT reading_file : public error
48
    {
49
    public:
×
50
        explicit reading_file(char const* filename);
51
    };
52

53
    /**
54
     * Base class for most exceptions in the library.
55
     *
56
     *  Substitutes the values for the parameter name
57
     *      placeholders in the template to create the human-readable error
58
     *      message
×
59
     *
×
60
     *  Placeholders are surrounded by % signs: %example%
×
61
     *      Poor man's version of boost::format
×
62
     *
63
     *  If a parameter name is absent, perform default substitutions
×
64
     *      instead so ugly placeholders are never left in-place.
65
     *
66
     *  Options are displayed in "canonical" form
67
     *      This is the most unambiguous form of the *parsed* option name and
68
     *      would correspond to option_description::format_name() i.e. what is
69
     *      shown by print_usage()
70
     *
71
     *  The "canonical" form depends on whether the option is
72
     *      specified in short or long form, using dashes or slashes or without
73
     *      a prefix (from a configuration file)
74
     */
75
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT error_with_option_name : public error
76
    {
77
    protected:
78
        /** can be
79
         *      0 = no prefix (config file options)
80
         *      allow_long
81
         *      allow_dash_for_short
82
         *      allow_slash_for_short
83
         *      allow_long_disguise */
84
        int m_option_style;
85

86
        /** substitutions
87
         *  from placeholders to values */
88
        std::map<std::string, std::string> m_substitutions;
89
        using string_pair = std::pair<std::string, std::string>;
90
        std::map<std::string, string_pair> m_substitution_defaults;
91

92
    public:
93
        /** template with placeholders */
94
        std::string m_error_template;
95

96
        explicit error_with_option_name(std::string const& template_,
97
            std::string const& option_name = "",
98
            std::string const& original_token = "", int option_style = 0);
99

100
        /** Substitute
101
         *      parameter_name->value to create the error message from
102
         *      the error template */
103
        void set_substitute(
104
            std::string const& parameter_name, std::string const& value)
105
        {
106
            m_substitutions[parameter_name] = value;
107
        }
108

109
        /**
110
         * If the parameter is missing, then make the
111
         *      from->to substitution instead
112
         */
113
        void set_substitute_default(std::string const& parameter_name,
114
            std::string const& from, std::string const& to)
115
        {
116
            m_substitution_defaults[parameter_name] = std::make_pair(from, to);
117
        }
118

119
        /** Add context to an exception */
120
        void add_context(std::string const& option_name,
×
121
            std::string const& original_token, int option_style)
×
122
        {
123
            set_option_name(option_name);
124
            set_original_token(original_token);
125
            set_prefix(option_style);
×
126
        }
127

128
        void set_prefix(int option_style) noexcept
×
129
        {
×
130
            m_option_style = option_style;
131
        }
132

133
        /** Overridden in error_with_no_option_name */
134
        virtual void set_option_name(std::string const& option_name)
135
        {
×
136
            set_substitute("option", option_name);
×
137
        }
138

139
        std::string get_option_name() const
140
        {
141
            return get_canonical_option_name();
142
        }
×
143

144
        void set_original_token(std::string const& original_token)
145
        {
146
            set_substitute("original_token", original_token);
×
147
        }
148

×
149
        /** Creates the error_message on the fly
×
150
         *      Currently a thin wrapper for substitute_placeholders() */
151
        char const* what() const noexcept override;
152

153
    protected:
×
154
        /** Used to hold the error text returned by what() */
155
        mutable std::string m_message;    // For on-demand formatting in 'what'
156

×
157
        /** Makes all substitutions using the template */
158
        virtual void substitute_placeholders(
×
159
            std::string const& error_template) const;
×
160

161
        // helper function for substitute_placeholders
162
        void replace_token(
163
            std::string const& from, std::string const& to) const;
164

165
        /** Construct option name in accordance with the appropriate
166
         *  prefix style: i.e. long dash or short slash etc. */
167
        std::string get_canonical_option_name() const;
168
        std::string get_canonical_option_prefix() const;
169
    };
170

171
    /** Class thrown when there are several option values, but
172
        user called a method which cannot return them all. */
173
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT multiple_values
174
      : public error_with_option_name
175
    {
176
    public:
177
        multiple_values();
178
    };
179

180
    /** Class thrown when there are several occurrences of an
181
        option, but user called a method which cannot return
182
        them all. */
183
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT multiple_occurrences
184
      : public error_with_option_name
185
    {
186
    public:
187
        multiple_occurrences();
188
    };
×
189

×
190
    /** Class thrown when a required/mandatory option is missing */
×
191
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT required_option
192
      : public error_with_option_name
×
193
    {
194
    public:
195
        // option name is constructed by the option_descriptor and never on the fly
196
        explicit required_option(std::string const& option_name);
197
    };
198

199
    /** Base class of un-parsable options,
200
     *  when the desired option cannot be identified.
201
     *
×
202
     *
×
203
     *  It makes no sense to have an option name, when we can't match an option to the
×
204
     *      parameter
205
     *
×
206
     *  Having this a part of the error_with_option_name hierarchy makes error
207
     *      handling a lot easier, even if the name indicates some sort of
208
     *      conceptual dissonance!
209
     *
210
     *   */
211
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT error_with_no_option_name
212
      : public error_with_option_name
213
    {
×
214
    public:
×
215
        explicit error_with_no_option_name(std::string const& template_,
216
            std::string const& original_token = "");
×
217

218
        /** Does NOT set option name, because no option name makes sense */
×
219
        void set_option_name(std::string const&) override {}
220
    };
221

222
    /** Class thrown when option name is not recognized. */
223
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT unknown_option
224
      : public error_with_no_option_name
225
    {
226
    public:
227
        explicit unknown_option(std::string const& original_token = "");
228
    };
229

230
    /** Class thrown when there's ambiguity among several possible options. */
231
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT ambiguous_option
232
      : public error_with_no_option_name
233
    {
234
    public:
235
        explicit ambiguous_option(std::vector<std::string> xalternatives);
236

237
        std::vector<std::string> const& alternatives() const noexcept
×
238
        {
239
            return m_alternatives;
×
240
        }
241

×
242
    protected:
243
        /** Makes all substitutions using the template */
244
        void substitute_placeholders(
×
245
            std::string const& error_template) const override;
246

247
    private:
248
        std::vector<std::string> m_alternatives;
249
    };
250

251
    /**
×
252
     * Class thrown when there's syntax error either for command
×
253
     *  line or config file options. See derived children for concrete classes.
×
254
     */
255
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT invalid_syntax
×
256
      : public error_with_option_name
257
    {
258
    public:
259
        enum kind_t
260
        {
261
            long_not_allowed = 30,
262
            long_adjacent_not_allowed,
×
263
            short_adjacent_not_allowed,
×
264
            empty_adjacent_parameter,
265
            missing_parameter,
×
266
            extra_parameter,
267
            unrecognized_line
×
268
        };
269

270
        explicit invalid_syntax(kind_t kind,
271
            std::string const& option_name = "",
272
            std::string const& original_token = "", int option_style = 0);
273

274
        constexpr kind_t kind() const noexcept
275
        {
276
            return m_kind;
277
        }
278

279
        /** Convenience functions for backwards compatibility */
280
        virtual std::string tokens() const
281
        {
282
            return get_option_name();
283
        }
284

285
    protected:
286
        /** Used to convert kind_t to a related error text */
287
        static std::string get_template(kind_t kind);
288
        kind_t m_kind;
×
289
    };
290

291
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT invalid_config_file_syntax
292
      : public invalid_syntax
293
    {
294
    public:
295
        invalid_config_file_syntax(
296
            std::string const& invalid_line, kind_t kind);
297

298
        /** Convenience functions for backwards compatibility */
299
        [[nodiscard]] std::string tokens() const override;
300
    };
301

302
    /** Class thrown when there are syntax errors in given command line */
×
303
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT invalid_command_line_syntax
304
      : public invalid_syntax
305
    {
×
306
    public:
×
307
        explicit invalid_command_line_syntax(kind_t kind,
×
308
            std::string const& option_name = "",
309
            std::string const& original_token = "", int option_style = 0);
×
310
    };
311

312
    /** Class thrown when value of option is incorrect. */
313
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT validation_error
314
      : public error_with_option_name
315
    {
316
    public:
317
        enum kind_t
×
318
        {
319
            multiple_values_not_allowed = 30,
×
320
            at_least_one_value_required,
321
            invalid_bool_value,
322
            invalid_option_value,
323
            invalid_option
324
        };
325

326
    public:
327
        explicit validation_error(kind_t kind,
328
            std::string const& option_name = "",
329
            std::string const& original_token = "", int option_style = 0);
330

331
        constexpr kind_t kind() const noexcept
×
332
        {
×
333
            return m_kind;
334
        }
×
335

×
336
    protected:
337
        /** Used to convert kind_t to a related error text */
338
        static std::string get_template(kind_t kind);
×
339
        kind_t m_kind;
340
    };
×
341

342
    /** Class thrown if there is an invalid option value given */
343
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT invalid_option_value
344
      : public validation_error
345
    {
×
346
    public:
347
        explicit invalid_option_value(std::string const& value);
348
        explicit invalid_option_value(std::wstring const& value);
349
    };
350

351
    /** Class thrown if there is an invalid bool value given */
352
    HPX_CXX_EXPORT class HPX_ALWAYS_EXPORT invalid_bool_value
353
      : public validation_error
354
    {
355
    public:
356
        explicit invalid_bool_value(std::string const& value);
×
357
    };
358
}    // namespace hpx::program_options
359

360
#include <hpx/config/warnings_suffix.hpp>
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