Skip to content

Commit 8003570

Browse files
committed
[MISC] Add poison_config
1 parent 5c99a11 commit 8003570

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// --------------------------------------------------------------------------------------------------------
2+
// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3+
// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik
4+
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5+
// shipped with this file and also available at: https://github.com/seqan/sharg-parser/blob/main/LICENSE.md
6+
// --------------------------------------------------------------------------------------------------------
7+
8+
/*!\file
9+
* \author Enrico Seiler <enrico.seiler AT fu-berlin.de>
10+
* \brief Provides sharg::detail::poison_config.
11+
*/
12+
13+
#pragma once
14+
15+
#include <sharg/config.hpp>
16+
17+
namespace sharg::detail
18+
{
19+
20+
/*!\brief This struct is used to prevent the user from calling sharg::parser::add_option, sharg::parser::add_flag,
21+
* and sharg::parser::add_positional_option without a sharg::config.
22+
* \ingroup parser
23+
* \details
24+
* This prevents (and produces a legible error message) for calls like:
25+
* ```
26+
* parser.add_option(value, {.short_id = 'i', .long_id = "int", .description = "Desc."});
27+
* ```
28+
* instead of
29+
* ```
30+
* parser.add_option(value, sharg::config{.short_id = 'i', .long_id = "int", .description = "Desc."});
31+
* ```
32+
*/
33+
struct poison_config
34+
{
35+
char short_id{'\0'}; //!< Same as sharg::config::short_id.
36+
std::string long_id{}; //!< Same as sharg::config::long_id.
37+
std::string description{}; //!< Same as sharg::config::description.
38+
std::string default_message{}; //!< Same as sharg::config::default_message.
39+
bool advanced{false}; //!< Same as sharg::config::advanced.
40+
bool hidden{false}; //!< Same as sharg::config::hidden.
41+
bool required{false}; //!< Same as sharg::config::required.
42+
std::any validator{}; //!< Prevents CTAD inside a function call, which would cause a compiler error.
43+
};
44+
45+
/*!\brief A validator used for comparing the size of sharg::config and sharg::poison_config.
46+
* \ingroup parser
47+
* \details
48+
* The `sizeof(std::any)` is typically `16`, while `sizeof(sharg::detail::default_validator)` is `1`.
49+
* `poison_config_size_comp_validator` provides a validator whose size is the same as the size of `std::any`.
50+
*/
51+
struct poison_config_size_comp_validator : public detail::default_validator
52+
{
53+
std::any validator{}; //!< A member such that the sizes of sharg::config and sharg::poison_config are the same.
54+
};
55+
56+
/*!\concept sharg::detail::poison_config_valid
57+
* \ingroup parser
58+
* \brief Concept that checks that sharg::poison_config has the same members as sharg::config.
59+
* \tparam validator_t The validator to use. Defaults to sharg::detail::poison_config_size_comp_validator.
60+
* \details
61+
* * Sizes of sharg::config and sharg::detail::poison_config are the same.
62+
* * sharg::detail::poison_config can be constructed with designated initializers from sharg::config's members.
63+
* * sharg::config can be constructed with designated initializers from sharg::detail::poison_config's members.
64+
*/
65+
template <typename validator_t = poison_config_size_comp_validator>
66+
concept poison_config_valid = (sizeof(poison_config) == sizeof(config<validator_t>))
67+
&& requires (config<validator_t> cfg, poison_config poison_cfg) {
68+
{
69+
poison_config{.short_id = cfg.short_id,
70+
.long_id = cfg.long_id,
71+
.description = cfg.description,
72+
.default_message = cfg.default_message,
73+
.advanced = cfg.advanced,
74+
.hidden = cfg.hidden,
75+
.required = cfg.required,
76+
.validator = cfg.validator}
77+
};
78+
{
79+
config<validator_t>{.short_id = poison_cfg.short_id,
80+
.long_id = poison_cfg.long_id,
81+
.description = poison_cfg.description,
82+
.default_message = poison_cfg.default_message,
83+
.advanced = poison_cfg.advanced,
84+
.hidden = poison_cfg.hidden,
85+
.required = poison_cfg.required,
86+
.validator = std::any_cast<validator_t>(poison_cfg.validator)}
87+
};
88+
};
89+
90+
static_assert(poison_config_valid<>, "sharg::detail::poison_config must have the same members as sharg::config!");
91+
92+
} // namespace sharg::detail

include/sharg/parser.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <sharg/detail/format_man.hpp>
2222
#include <sharg/detail/format_parse.hpp>
2323
#include <sharg/detail/format_tdl.hpp>
24+
#include <sharg/detail/poison_config.hpp>
2425
#include <sharg/detail/version_check.hpp>
2526

2627
namespace sharg
@@ -257,6 +258,15 @@ class parser
257258
format);
258259
}
259260

261+
//!\cond DEV
262+
//!\brief A poison overload that catches calls to add_option without explicitly passing a sharg::config.
263+
template <typename option_type>
264+
void add_option(option_type &, detail::poison_config const &)
265+
{
266+
static_assert(false, "Forgot sharg::config?");
267+
}
268+
//!\endcond
269+
260270
/*!\brief Adds a flag to the sharg::parser.
261271
*
262272
* \param[in, out] value The variable which shows if the flag is turned off (default) or on.
@@ -286,6 +296,15 @@ class parser
286296
format);
287297
}
288298

299+
//!\cond DEV
300+
//!\brief A poison overload that catches calls to add_flag without explicitly passing a sharg::config.
301+
template <typename option_type> // Template needed to prevent instantiation of this function if unused.
302+
void add_flag(option_type &, detail::poison_config const &)
303+
{
304+
static_assert(false, "Forgot sharg::config?");
305+
}
306+
//!\endcond
307+
289308
/*!\brief Adds a positional option to the sharg::parser.
290309
*
291310
* \tparam option_type Must have a formatted input function (stream >> value).
@@ -327,6 +346,16 @@ class parser
327346
},
328347
format);
329348
}
349+
350+
//!\cond DEV
351+
//!\brief A poison overload that catches calls to add_positional_option without explicitly passing a sharg::config.
352+
template <typename option_type>
353+
void add_positional_option(option_type &, detail::poison_config const &)
354+
{
355+
static_assert(false, "Forgot sharg::config?");
356+
}
357+
//!\endcond
358+
330359
//!\}
331360

332361
/*!\brief Initiates the actual command line parsing.

0 commit comments

Comments
 (0)