CLI11
2.7.2
C++11 Command Line Interface Parser
Toggle main menu visibility
Loading...
Searching...
No Matches
TypeTools_inl.hpp
1
// Copyright (c) 2017-2026, University of Cincinnati, developed by Henry Schreiner
2
// under NSF AWARD 1414736 and by the respective contributors.
3
// All rights reserved.
4
//
5
// SPDX-License-Identifier: BSD-3-Clause
6
7
#pragma once
8
9
// IWYU pragma: private, include "CLI/CLI.hpp"
10
11
// This include is only needed for IDEs to discover symbols
12
#include "../TypeTools.hpp"
13
14
#include "../StringTools.hpp"
15
16
// [CLI11:public_includes:set]
17
#include <cerrno>
18
#include <cstdint>
19
#include <cstdlib>
20
#include <limits>
21
#include <string>
22
#include <utility>
23
#include <vector>
24
// [CLI11:public_includes:end]
25
26
namespace
CLI {
27
// [CLI11:type_tools_inl_hpp:verbatim]
28
29
namespace
detail {
30
31
CLI11_INLINE std::int64_t to_flag_value(std::string val)
noexcept
{
32
static
const
std::string trueString(
"true"
);
33
static
const
std::string falseString(
"false"
);
34
if
(val == trueString) {
35
return
1;
36
}
37
if
(val == falseString) {
38
return
-1;
39
}
40
val = detail::to_lower(std::move(val));
41
std::int64_t ret = 0;
42
if
(val.size() == 1) {
43
if
(val[0] >=
'1'
&& val[0] <=
'9'
) {
44
return
(
static_cast<
std::int64_t
>
(val[0]) -
'0'
);
45
}
46
switch
(val[0]) {
47
case
'0'
:
48
case
'f'
:
49
case
'n'
:
50
case
'-'
:
51
ret = -1;
52
break
;
53
case
't'
:
54
case
'y'
:
55
case
'+'
:
56
ret = 1;
57
break
;
58
default
:
59
errno = EINVAL;
60
return
-1;
61
}
62
return
ret;
63
}
64
if
(val == trueString || val ==
"on"
|| val ==
"yes"
|| val ==
"enable"
) {
65
ret = 1;
66
}
else
if
(val == falseString || val ==
"off"
|| val ==
"no"
|| val ==
"disable"
) {
67
ret = -1;
68
}
else
{
69
char
*loc_ptr{
nullptr
};
70
ret = std::strtoll(val.c_str(), &loc_ptr, 0);
71
if
(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
72
errno = EINVAL;
73
}
74
}
75
return
ret;
76
}
77
78
CLI11_INLINE std::string sum_string_vector(
const
std::vector<std::string> &values) {
79
// First try a pure integer sum so that large integral totals (>= 1e16, or beyond the 2^53 mantissa of
80
// double) are preserved exactly and rendered as a plain integer rather than scientific notation
81
std::int64_t ival{0};
82
bool
int_sum{
true
};
83
for
(
const
auto
&arg : values) {
84
std::int64_t tv{0};
85
if
(!integral_conversion(arg, tv)) {
86
int_sum =
false
;
87
break
;
88
}
89
// detect signed overflow before performing the addition since signed overflow is undefined behavior
90
if
((tv > 0 && ival > (std::numeric_limits<std::int64_t>::max)() - tv) ||
91
(tv < 0 && ival < (std::numeric_limits<std::int64_t>::min)() - tv)) {
92
int_sum =
false
;
93
break
;
94
}
95
ival += tv;
96
}
97
if
(int_sum) {
98
return
std::to_string(ival);
99
}
100
101
double
val{0.0};
102
bool
fail{
false
};
103
std::string output;
104
for
(
const
auto
&arg : values) {
105
double
tv{0.0};
106
auto
comp = lexical_cast(arg, tv);
107
if
(!comp) {
108
errno = 0;
109
auto
fv = detail::to_flag_value(arg);
110
fail = (errno != 0);
111
if
(fail) {
112
break
;
113
}
114
tv =
static_cast<
double
>
(fv);
115
}
116
val += tv;
117
}
118
if
(fail) {
119
for
(
const
auto
&arg : values) {
120
output.append(arg);
121
}
122
}
else
{
123
output = float_to_string(val, 16);
124
}
125
return
output;
126
}
127
128
}
// namespace detail
129
// [CLI11:type_tools_inl_hpp:end]
130
}
// namespace CLI
include
CLI
impl
TypeTools_inl.hpp
Generated by
1.17.0