Coverage Report

Created: 2026-11-11 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/args/args.hxx
Line
Count
Source
1
/* A simple header-only C++ argument parser library.
2
 *
3
 * https://github.com/Taywee/args
4
 *
5
 * Copyright (c) 2016-2024 Taylor Richberger <taylor@axfive.net> and Pavel
6
 * Belikov
7
 * 
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 * 
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 * 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 */
26
27
/** \file args.hxx
28
 * \brief this single-header lets you use all of the args functionality
29
 *
30
 * The important stuff is done inside the args namespace
31
 */
32
33
#ifndef ARGS_HXX
34
#define ARGS_HXX
35
#pragma push_macro("min")
36
#pragma push_macro("max")
37
#undef min
38
#undef max
39
40
#define ARGS_VERSION "6.6.1"
41
#define ARGS_VERSION_MAJOR 6
42
#define ARGS_VERSION_MINOR 6
43
#define ARGS_VERSION_PATCH 1
44
45
#include <algorithm>
46
#include <iterator>
47
#include <exception>
48
#include <functional>
49
#include <sstream>
50
#include <string>
51
#include <tuple>
52
#include <vector>
53
#include <unordered_map>
54
#include <unordered_set>
55
#include <type_traits>
56
#include <cstddef>
57
#include <cctype>
58
#include <cerrno>
59
#include <cstdlib>
60
#include <limits>
61
#include <iostream>
62
63
#if defined(_MSC_VER) && _MSC_VER <= 1800
64
#define noexcept
65
#endif
66
67
/** \namespace args
68
 * \brief contains all the functionality of the args library
69
 */
70
namespace args
71
{
72
    /** Getter to grab the value from the argument type.
73
     *
74
     * If the Get() function of the type returns a reference, so does this, and
75
     * the value will be modifiable.
76
     */
77
    template <typename Option>
78
    auto get(Option &option_) -> decltype(option_.Get())
79
    {
80
        return option_.Get();
81
    }
82
83
    /** (INTERNAL) Count UTF-8 glyphs
84
     *
85
     * This is not reliable, and will fail for combinatory glyphs, but it's
86
     * good enough here for now.
87
     *
88
     * \param string The string to count glyphs from
89
     * \return The UTF-8 glyphs in the string
90
     */
91
    inline std::string::size_type Glyphs(const std::string &string_)
92
1.03M
    {
93
1.03M
        std::string::size_type length = 0;
94
1.03M
        for (const char c: string_)
95
6.96M
        {
96
6.96M
            if ((c & 0xc0) != 0x80)
97
6.89M
            {
98
6.89M
                ++length;
99
6.89M
            }
100
6.96M
        }
101
1.03M
        return length;
102
1.03M
    }
103
104
    /** Safe addition to prevent integer overflow.
105
     * Returns true if the addition is successful, false if it would overflow.
106
     */
107
    template<typename T>
108
    bool SafeAdd(T a, T b, T& out) noexcept
109
599k
    {
110
599k
        static_assert(std::is_integral<T>::value, "SafeAdd requires integral types.");
111
599k
        if (std::is_unsigned<T>::value)
112
599k
        {
113
599k
            using U = typename std::make_unsigned<T>::type;
114
599k
            const U ua = static_cast<U>(a);
115
599k
            const U ub = static_cast<U>(b);
116
599k
            const U maxv = std::numeric_limits<U>::max();
117
599k
            if (ua > maxv - ub)
118
0
            {
119
0
                return false;
120
0
            }
121
599k
            out = static_cast<T>(ua + ub);
122
599k
            return true;
123
599k
        }
124
0
        else
125
0
        {
126
0
#if defined(__clang__) || defined(__GNUC__)
127
0
            return !__builtin_add_overflow(a, b, &out);
128
#else
129
            // Fallback bounds check
130
            if (b > 0 && a > std::numeric_limits<T>::max() - b)
131
            {
132
                return false;
133
            }
134
            if (b < 0 && a < std::numeric_limits<T>::min() - b)
135
            {
136
                return false;
137
            }
138
            out = a + b;
139
            return true;
140
#endif
141
0
        }
142
599k
    }
143
144
    /** Safe multiplication to prevent integer overflow.
145
     * Returns true if the multiplication is successful, false if it would overflow.
146
     */
147
    template<typename T>
148
    bool SafeMultiply(T a, T b, T& out) noexcept
149
27.0k
    {
150
27.0k
        static_assert(std::is_integral<T>::value, "SafeMultiply requires integral types.");
151
152
27.0k
        if (a == 0 || b == 0)
153
0
        {
154
0
            out = 0;
155
0
            return true;
156
0
        }
157
158
27.0k
        if (std::is_unsigned<T>::value)
159
27.0k
        {
160
27.0k
            using U = typename std::make_unsigned<T>::type;
161
27.0k
            const U ua = static_cast<U>(a);
162
27.0k
            const U ub = static_cast<U>(b);
163
27.0k
            const U maxv = std::numeric_limits<U>::max();
164
27.0k
            if (ub > maxv / ua)
165
0
            {
166
0
                return false;
167
0
            }
168
27.0k
            out = static_cast<T>(ua * ub);
169
27.0k
            return true;
170
27.0k
        }
171
0
        else
172
0
        {
173
0
#if defined(__clang__) || defined(__GNUC__)
174
0
            return !__builtin_mul_overflow(a, b, &out);
175
#else
176
            // Fallback bounds check
177
            if (a == -1 && b == std::numeric_limits<T>::min())
178
            {
179
                return false;
180
            }
181
            if (b == -1 && a == std::numeric_limits<T>::min())
182
            {
183
                return false;
184
            }
185
            if ((a > 0 && b > 0 && a > std::numeric_limits<T>::max() / b) ||
186
                (a > 0 && b < 0 && b < std::numeric_limits<T>::min() / a) ||
187
                (a < 0 && b > 0 && a < std::numeric_limits<T>::min() / b) ||
188
                (a < 0 && b < 0 && a < std::numeric_limits<T>::max() / b))
189
            {
190
                return false;
191
            }
192
            out = a * b;
193
            return true;
194
#endif
195
0
        }
196
27.0k
    }
197
198
    /** Safe subtraction to prevent integer underflow.
199
     * Returns true if the subtraction is successful, false if it would underflow.
200
     */
201
    template<typename T>
202
    bool SafeSub(T a, T b, T& out) noexcept
203
    {
204
        static_assert(std::is_integral<T>::value, "SafeSub requires integral types.");
205
        if (std::is_unsigned<T>::value)
206
        {
207
            if (a < b)
208
            {
209
                return false;
210
            }
211
            out = a - b;
212
            return true;
213
        }
214
        else
215
        {
216
#if defined(__clang__) || defined(__GNUC__)
217
            return !__builtin_sub_overflow(a, b, &out);
218
#else
219
            // Fallback bounds check
220
            if (b > 0 && a < std::numeric_limits<T>::min() + b)
221
            {
222
                return false;
223
            }
224
            if (b < 0 && a > std::numeric_limits<T>::max() + b)
225
            {
226
                return false;
227
            }
228
            out = a - b;
229
            return true;
230
#endif
231
        }
232
    }
233
234
    /** Safe negation to prevent integer overflow.
235
     * Returns true if the negation is successful, false if it would overflow.
236
     */
237
    // Unsigned overload
238
    template<typename T>
239
    typename std::enable_if<std::is_unsigned<T>::value, bool>::type
240
    SafeNeg(T a, T& out) noexcept
241
    {
242
        static_assert(std::is_integral<T>::value, "SafeNeg requires integral types.");
243
        if (a != 0)
244
        {
245
            return false;
246
        }
247
        out = 0;
248
        return true;
249
    }
250
251
    // Signed overload
252
    template<typename T>
253
    typename std::enable_if<std::is_signed<T>::value, bool>::type
254
    SafeNeg(T a, T& out) noexcept
255
    {
256
        static_assert(std::is_integral<T>::value, "SafeNeg requires integral types.");
257
        if (a == std::numeric_limits<T>::min())
258
        {
259
            return false;
260
        }
261
        out = -a;
262
        return true;
263
    }
264
265
    /** (INTERNAL) Wrap a vector of words into a vector of lines
266
     *
267
     * Empty words are skipped. Word "\n" forces wrapping.
268
     *
269
     * \param begin The begin iterator
270
     * \param end The end iterator
271
     * \param width The width of the body
272
     * \param firstlinewidth the width of the first line, defaults to the width of the body
273
     * \param firstlineindent the indent of the first line, defaults to 0
274
     * \return the vector of lines
275
     */
276
    template <typename It>
277
    inline std::vector<std::string> Wrap(It begin,
278
                                         It end,
279
                                         const std::string::size_type width,
280
                                         std::string::size_type firstlinewidth = 0,
281
                                         std::string::size_type firstlineindent = 0)
282
536k
    {
283
536k
        std::vector<std::string> output;
284
536k
        std::string line(firstlineindent, ' ');
285
536k
        bool empty = true;
286
287
536k
        if (firstlinewidth == 0)
288
521k
        {
289
521k
            firstlinewidth = width;
290
521k
        }
291
292
536k
        auto currentwidth = firstlinewidth;
293
294
1.32M
        for (auto it = begin; it != end; ++it)
295
789k
        {
296
789k
            if (it->empty())
297
40.0k
            {
298
40.0k
                continue;
299
40.0k
            }
300
301
749k
            if (*it == "\n")
302
156
            {
303
156
                if (!empty)
304
9
                {
305
9
                    output.push_back(line);
306
9
                    line.clear();
307
9
                    empty = true;
308
9
                    currentwidth = width;
309
9
                }
310
311
156
                continue;
312
156
            }
313
314
749k
            auto itemsize = Glyphs(*it);
315
            
316
            // Refactored to prevent integer overflow
317
749k
            bool needsWrap = false;
318
749k
            if (itemsize >= currentwidth)
319
258k
            {
320
258k
                needsWrap = true;
321
258k
            }
322
491k
            else
323
491k
            {
324
491k
                size_t remainingWidth = (currentwidth > itemsize) ? (currentwidth - itemsize) : 0;
325
491k
                size_t nextLength = 0;
326
491k
                if (!SafeAdd<std::string::size_type>(line.length(), static_cast<std::string::size_type>(1), nextLength) || nextLength > remainingWidth)
327
15.0k
                {
328
15.0k
                    needsWrap = true;
329
15.0k
                }
330
491k
            }
331
            
332
749k
            if (needsWrap)
333
273k
            {
334
273k
                if (!empty)
335
145k
                {
336
145k
                    output.push_back(line);
337
145k
                    line.clear();
338
145k
                    empty = true;
339
145k
                    currentwidth = width;
340
145k
                }
341
273k
            }
342
343
749k
            if (itemsize > 0)
344
747k
            {
345
747k
                if (!empty)
346
288k
                {
347
288k
                    line += ' ';
348
288k
                }
349
350
747k
                line += *it;
351
747k
                empty = false;
352
747k
            }
353
749k
        }
354
355
536k
        if (!empty)
356
313k
        {
357
313k
            output.push_back(line);
358
313k
        }
359
360
536k
        return output;
361
536k
    }
std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > args::Wrap<std::__1::istream_iterator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char, std::__1::char_traits<char>, long> >(std::__1::istream_iterator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char, std::__1::char_traits<char>, long>, std::__1::istream_iterator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char, std::__1::char_traits<char>, long>, unsigned long, unsigned long, unsigned long)
Line
Count
Source
282
516k
    {
283
516k
        std::vector<std::string> output;
284
516k
        std::string line(firstlineindent, ' ');
285
516k
        bool empty = true;
286
287
516k
        if (firstlinewidth == 0)
288
516k
        {
289
516k
            firstlinewidth = width;
290
516k
        }
291
292
516k
        auto currentwidth = firstlinewidth;
293
294
1.16M
        for (auto it = begin; it != end; ++it)
295
650k
        {
296
650k
            if (it->empty())
297
0
            {
298
0
                continue;
299
0
            }
300
301
650k
            if (*it == "\n")
302
0
            {
303
0
                if (!empty)
304
0
                {
305
0
                    output.push_back(line);
306
0
                    line.clear();
307
0
                    empty = true;
308
0
                    currentwidth = width;
309
0
                }
310
311
0
                continue;
312
0
            }
313
314
650k
            auto itemsize = Glyphs(*it);
315
            
316
            // Refactored to prevent integer overflow
317
650k
            bool needsWrap = false;
318
650k
            if (itemsize >= currentwidth)
319
235k
            {
320
235k
                needsWrap = true;
321
235k
            }
322
415k
            else
323
415k
            {
324
415k
                size_t remainingWidth = (currentwidth > itemsize) ? (currentwidth - itemsize) : 0;
325
415k
                size_t nextLength = 0;
326
415k
                if (!SafeAdd<std::string::size_type>(line.length(), static_cast<std::string::size_type>(1), nextLength) || nextLength > remainingWidth)
327
10.7k
                {
328
10.7k
                    needsWrap = true;
329
10.7k
                }
330
415k
            }
331
            
332
650k
            if (needsWrap)
333
246k
            {
334
246k
                if (!empty)
335
124k
                {
336
124k
                    output.push_back(line);
337
124k
                    line.clear();
338
124k
                    empty = true;
339
124k
                    currentwidth = width;
340
124k
                }
341
246k
            }
342
343
650k
            if (itemsize > 0)
344
648k
            {
345
648k
                if (!empty)
346
230k
                {
347
230k
                    line += ' ';
348
230k
                }
349
350
648k
                line += *it;
351
648k
                empty = false;
352
648k
            }
353
650k
        }
354
355
516k
        if (!empty)
356
293k
        {
357
293k
            output.push_back(line);
358
293k
        }
359
360
516k
        return output;
361
516k
    }
std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > args::Wrap<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, unsigned long, unsigned long, unsigned long)
Line
Count
Source
282
20.1k
    {
283
20.1k
        std::vector<std::string> output;
284
20.1k
        std::string line(firstlineindent, ' ');
285
20.1k
        bool empty = true;
286
287
20.1k
        if (firstlinewidth == 0)
288
5.53k
        {
289
5.53k
            firstlinewidth = width;
290
5.53k
        }
291
292
20.1k
        auto currentwidth = firstlinewidth;
293
294
159k
        for (auto it = begin; it != end; ++it)
295
138k
        {
296
138k
            if (it->empty())
297
40.0k
            {
298
40.0k
                continue;
299
40.0k
            }
300
301
98.8k
            if (*it == "\n")
302
156
            {
303
156
                if (!empty)
304
9
                {
305
9
                    output.push_back(line);
306
9
                    line.clear();
307
9
                    empty = true;
308
9
                    currentwidth = width;
309
9
                }
310
311
156
                continue;
312
156
            }
313
314
98.6k
            auto itemsize = Glyphs(*it);
315
            
316
            // Refactored to prevent integer overflow
317
98.6k
            bool needsWrap = false;
318
98.6k
            if (itemsize >= currentwidth)
319
22.8k
            {
320
22.8k
                needsWrap = true;
321
22.8k
            }
322
75.7k
            else
323
75.7k
            {
324
75.7k
                size_t remainingWidth = (currentwidth > itemsize) ? (currentwidth - itemsize) : 0;
325
75.7k
                size_t nextLength = 0;
326
75.7k
                if (!SafeAdd<std::string::size_type>(line.length(), static_cast<std::string::size_type>(1), nextLength) || nextLength > remainingWidth)
327
4.31k
                {
328
4.31k
                    needsWrap = true;
329
4.31k
                }
330
75.7k
            }
331
            
332
98.6k
            if (needsWrap)
333
27.2k
            {
334
27.2k
                if (!empty)
335
21.0k
                {
336
21.0k
                    output.push_back(line);
337
21.0k
                    line.clear();
338
21.0k
                    empty = true;
339
21.0k
                    currentwidth = width;
340
21.0k
                }
341
27.2k
            }
342
343
98.6k
            if (itemsize > 0)
344
98.4k
            {
345
98.4k
                if (!empty)
346
57.1k
                {
347
57.1k
                    line += ' ';
348
57.1k
                }
349
350
98.4k
                line += *it;
351
98.4k
                empty = false;
352
98.4k
            }
353
98.6k
        }
354
355
20.1k
        if (!empty)
356
20.1k
        {
357
20.1k
            output.push_back(line);
358
20.1k
        }
359
360
20.1k
        return output;
361
20.1k
    }
362
363
    namespace detail
364
    {
365
        template <typename T>
366
        std::string Join(const T& array, const std::string &delimiter)
367
556k
        {
368
556k
            std::string res;
369
            
370
            // Safely compute reservation size to avoid unbounded reallocations
371
556k
            using size_type = std::string::size_type;
372
556k
            size_type total = 0;
373
556k
            size_type count = 0;
374
556k
            const size_type delim_size = static_cast<size_type>(delimiter.size());
375
556k
            bool can_reserve = true;
376
            
377
556k
            for (const auto &element : array)
378
81.1k
            {
379
81.1k
                const size_type elem_size = static_cast<size_type>(element.size());
380
81.1k
                if (!SafeAdd<size_type>(total, elem_size, total))
381
0
                {
382
0
                    can_reserve = false;
383
0
                    break;
384
0
                }
385
81.1k
                ++count;
386
81.1k
            }
387
            
388
556k
            if (can_reserve && count > 1)
389
27.0k
            {
390
27.0k
                size_type delim_count = count - 1;
391
27.0k
                size_type delim_total = 0;
392
27.0k
                if (!SafeMultiply<size_type>(delim_count, delim_size, delim_total) ||
393
27.0k
                    !SafeAdd<size_type>(total, delim_total, total))
394
0
                {
395
0
                    can_reserve = false;
396
0
                }
397
27.0k
            }
398
            
399
556k
            if (can_reserve && total > 0)
400
27.1k
            {
401
27.1k
                res.reserve(total);
402
27.1k
            }
403
            
404
556k
            bool first = true;
405
556k
            for (const auto &element : array)
406
81.1k
            {
407
81.1k
                if (!first)
408
54.0k
                {
409
54.0k
                    res += delimiter;
410
54.0k
                }
411
81.1k
                res += element;
412
81.1k
                first = false;
413
81.1k
            }
414
            
415
556k
            return res;
416
556k
        }
417
    }
418
419
    /** (INTERNAL) Wrap a string into a vector of lines
420
     *
421
     * This is quick and hacky, but works well enough.  You can specify a
422
     * different width for the first line
423
     *
424
     * \param width The width of the body
425
     * \param firstlinewid the width of the first line, defaults to the width of the body
426
     * \return the vector of lines
427
     */
428
    inline std::vector<std::string> Wrap(const std::string &in, const std::string::size_type width, std::string::size_type firstlinewidth = 0)
429
0
    {
430
0
        // Preserve existing line breaks
431
0
        const auto newlineloc = in.find('\n');
432
0
        if (newlineloc != in.npos)
433
0
        {
434
0
            auto first = Wrap(std::string(in, 0, newlineloc), width);
435
0
            auto second = Wrap(std::string(in, newlineloc + 1), width);
436
0
            first.insert(
437
0
                std::end(first),
438
0
                std::make_move_iterator(std::begin(second)),
439
0
                std::make_move_iterator(std::end(second)));
440
0
            return first;
441
0
        }
442
0
443
0
        std::istringstream stream(in);
444
0
        std::string::size_type indent = 0;
445
0
446
0
        for (auto c : in)
447
0
        {
448
0
            if (!std::isspace(static_cast<unsigned char>(c)))
449
0
            {
450
0
                break;
451
0
            }
452
0
            ++indent;
453
0
        }
454
0
455
0
        return Wrap(std::istream_iterator<std::string>(stream), std::istream_iterator<std::string>(),
456
0
                    width, firstlinewidth, indent);
457
0
    }
458
459
#ifdef ARGS_NOEXCEPT
460
    /// Error class, for when ARGS_NOEXCEPT is defined
461
    enum class Error
462
    {
463
        None,
464
        Usage,
465
        Parse,
466
        Validation,
467
        Required,
468
        Map,
469
        Extra,
470
        Help,
471
        Subparser,
472
        Completion,
473
    };
474
#else
475
    /** Base error class
476
     */
477
    class Error : public std::runtime_error
478
    {
479
        public:
480
21.5k
            Error(const std::string &problem) : std::runtime_error(problem) {}
481
0
            virtual ~Error() {}
482
    };
483
484
    /** Errors that occur during usage
485
     */
486
    class UsageError : public Error
487
    {
488
        public:
489
0
            UsageError(const std::string &problem) : Error(problem) {}
490
0
            virtual ~UsageError() {}
491
    };
492
493
    /** Errors that occur during regular parsing
494
     */
495
    class ParseError : public Error
496
    {
497
        public:
498
15.6k
            ParseError(const std::string &problem) : Error(problem) {}
499
0
            virtual ~ParseError() {}
500
    };
501
502
    /** Errors that are detected from group validation after parsing finishes
503
     */
504
    class ValidationError : public Error
505
    {
506
        public:
507
3.60k
            ValidationError(const std::string &problem) : Error(problem) {}
508
0
            virtual ~ValidationError() {}
509
    };
510
511
    /** Errors that when a required flag is omitted
512
     */
513
    class RequiredError : public ValidationError
514
    {
515
        public:
516
1.96k
            RequiredError(const std::string &problem) : ValidationError(problem) {}
517
0
            virtual ~RequiredError() {}
518
    };
519
520
    /** Errors in map lookups
521
     */
522
    class MapError : public ParseError
523
    {
524
        public:
525
1.41k
            MapError(const std::string &problem) : ParseError(problem) {}
526
0
            virtual ~MapError() {}
527
    };
528
529
    /** Error that occurs when a singular flag is specified multiple times
530
     */
531
    class ExtraError : public ParseError
532
    {
533
        public:
534
1
            ExtraError(const std::string &problem) : ParseError(problem) {}
535
0
            virtual ~ExtraError() {}
536
    };
537
538
    /** An exception that indicates that the user has requested help
539
     */
540
    class Help : public Error
541
    {
542
        public:
543
223
            Help(const std::string &flag) : Error(flag) {}
544
0
            virtual ~Help() {}
545
    };
546
547
    /** (INTERNAL) An exception that emulates coroutine-like control flow for subparsers.
548
     */
549
    class SubparserError : public Error
550
    {
551
        public:
552
51
            SubparserError() : Error("") {}
553
0
            virtual ~SubparserError() {}
554
    };
555
556
    /** An exception that contains autocompletion reply
557
     */
558
    class Completion : public Error
559
    {
560
        public:
561
2.09k
            Completion(const std::string &flag) : Error(flag) {}
562
0
            virtual ~Completion() {}
563
    };
564
#endif
565
566
    /** A simple unified option type for unified initializer lists for the Matcher class.
567
     */
568
    struct EitherFlag
569
    {
570
        const bool isShort;
571
        const char shortFlag;
572
        const std::string longFlag;
573
730k
        EitherFlag(const std::string &flag) : isShort(false), shortFlag(), longFlag(flag) {}
574
95.0k
        EitherFlag(const char *flag) : isShort(false), shortFlag(), longFlag(flag) {}
575
794k
        EitherFlag(const char flag) : isShort(true), shortFlag(flag), longFlag() {}
576
577
        /** Get just the long flags from an initializer list of EitherFlags
578
         */
579
        static std::unordered_set<std::string> GetLong(std::initializer_list<EitherFlag> flags)
580
103k
        {
581
103k
            std::unordered_set<std::string>  longFlags;
582
103k
            for (const EitherFlag &flag: flags)
583
180k
            {
584
180k
                if (!flag.isShort)
585
95.0k
                {
586
95.0k
                    longFlags.insert(flag.longFlag);
587
95.0k
                }
588
180k
            }
589
103k
            return longFlags;
590
103k
        }
591
592
        /** Get just the short flags from an initializer list of EitherFlags
593
         */
594
        static std::unordered_set<char> GetShort(std::initializer_list<EitherFlag> flags)
595
103k
        {
596
103k
            std::unordered_set<char>  shortFlags;
597
103k
            for (const EitherFlag &flag: flags)
598
180k
            {
599
180k
                if (flag.isShort)
600
85.2k
                {
601
85.2k
                    shortFlags.insert(flag.shortFlag);
602
85.2k
                }
603
180k
            }
604
103k
            return shortFlags;
605
103k
        }
606
607
        std::string str() const
608
393k
        {
609
393k
            return isShort ? std::string(1, shortFlag) : longFlag;
610
393k
        }
611
612
        std::string str(const std::string &shortPrefix, const std::string &longPrefix) const
613
80.2k
        {
614
80.2k
            return isShort ? shortPrefix + std::string(1, shortFlag) : longPrefix + longFlag;
615
80.2k
        }
616
    };
617
618
619
620
    /** A class of "matchers", specifying short and flags that can possibly be
621
     * matched.
622
     *
623
     * This is supposed to be constructed and then passed in, not used directly
624
     * from user code.
625
     */
626
    class Matcher
627
    {
628
        private:
629
            const std::unordered_set<char> shortFlags;
630
            const std::unordered_set<std::string> longFlags;
631
632
        public:
633
            /** Specify short and long flags separately as iterators
634
             *
635
             * ex: `args::Matcher(shortFlags.begin(), shortFlags.end(), longFlags.begin(), longFlags.end())`
636
             */
637
            template <typename ShortIt, typename LongIt>
638
            Matcher(ShortIt shortFlagsStart, ShortIt shortFlagsEnd, LongIt longFlagsStart, LongIt longFlagsEnd) :
639
103k
                shortFlags(shortFlagsStart, shortFlagsEnd),
640
103k
                longFlags(longFlagsStart, longFlagsEnd)
641
103k
            {
642
103k
                if (shortFlags.empty() && longFlags.empty())
643
0
                {
644
0
#ifndef ARGS_NOEXCEPT
645
0
                    throw UsageError("empty Matcher");
646
0
#endif
647
0
                }
648
103k
            }
649
650
#ifdef ARGS_NOEXCEPT
651
            /// Only for ARGS_NOEXCEPT
652
            Error GetError() const noexcept
653
            {
654
                return shortFlags.empty() && longFlags.empty() ? Error::Usage : Error::None;
655
            }
656
#endif
657
658
            /** Specify short and long flags separately as iterables
659
             *
660
             * ex: `args::Matcher(shortFlags, longFlags)`
661
             */
662
            template <typename Short, typename Long>
663
            Matcher(Short &&shortIn, Long &&longIn) :
664
103k
                Matcher(std::begin(shortIn), std::end(shortIn), std::begin(longIn), std::end(longIn))
665
103k
            {}
666
667
            /** Specify a mixed single initializer-list of both short and long flags
668
             *
669
             * This is the fancy one.  It takes a single initializer list of
670
             * any number of any mixed kinds of flags.  Chars are
671
             * automatically interpreted as short flags, and strings are
672
             * automatically interpreted as long flags:
673
             *
674
             *     args::Matcher{'a'}
675
             *     args::Matcher{"foo"}
676
             *     args::Matcher{'h', "help"}
677
             *     args::Matcher{"foo", 'f', 'F', "FoO"}
678
             */
679
            Matcher(std::initializer_list<EitherFlag> in) :
680
103k
                Matcher(EitherFlag::GetShort(in), EitherFlag::GetLong(in)) {}
681
682
103k
            Matcher(Matcher &&other) noexcept : shortFlags(std::move(other.shortFlags)), longFlags(std::move(other.longFlags))
683
103k
            {}
684
685
207k
            ~Matcher() {}
686
687
            /** (INTERNAL) Check if there is a match of a short flag
688
             */
689
            bool Match(const char flag) const
690
83.8k
            {
691
83.8k
                return shortFlags.find(flag) != shortFlags.end();
692
83.8k
            }
693
694
            /** (INTERNAL) Check if there is a match of a long flag
695
             */
696
            bool Match(const std::string &flag) const
697
45.5k
            {
698
45.5k
                return longFlags.find(flag) != longFlags.end();
699
45.5k
            }
700
701
            /** (INTERNAL) Check if there is a match of a flag
702
             */
703
            bool Match(const EitherFlag &flag) const
704
129k
            {
705
129k
                return flag.isShort ? Match(flag.shortFlag) : Match(flag.longFlag);
706
129k
            }
707
708
            /** (INTERNAL) Get all flag strings as a vector, with the prefixes embedded
709
             */
710
            std::vector<EitherFlag> GetFlagStrings() const
711
741k
            {
712
741k
                std::vector<EitherFlag> flagStrings;
713
741k
                flagStrings.reserve(shortFlags.size() + longFlags.size());
714
741k
                for (const char flag: shortFlags)
715
642k
                {
716
642k
                    flagStrings.emplace_back(flag);
717
642k
                }
718
741k
                for (const std::string &flag: longFlags)
719
687k
                {
720
687k
                    flagStrings.emplace_back(flag);
721
687k
                }
722
741k
                return flagStrings;
723
741k
            }
724
725
            /** (INTERNAL) Get long flag if it exists or any short flag
726
             */
727
            EitherFlag GetLongOrAny() const
728
30.0k
            {
729
30.0k
                if (!longFlags.empty())
730
26.6k
                {
731
26.6k
                    return *longFlags.begin();
732
26.6k
                }
733
734
3.40k
                if (!shortFlags.empty())
735
3.40k
                {
736
3.40k
                    return *shortFlags.begin();
737
3.40k
                }
738
739
                // should be unreachable
740
0
                return ' ';
741
3.40k
            }
742
743
            /** (INTERNAL) Get short flag if it exists or any long flag
744
             */
745
            EitherFlag GetShortOrAny() const
746
47.4k
            {
747
47.4k
                if (!shortFlags.empty())
748
41.9k
                {
749
41.9k
                    return *shortFlags.begin();
750
41.9k
                }
751
752
5.57k
                if (!longFlags.empty())
753
5.57k
                {
754
5.57k
                    return *longFlags.begin();
755
5.57k
                }
756
757
                // should be unreachable
758
0
                return ' ';
759
5.57k
            }
760
    };
761
762
    /** Attributes for flags.
763
     */
764
    enum class Options
765
    {
766
        /** Default options.
767
         */
768
        None = 0x0,
769
770
        /** Flag can't be passed multiple times.
771
         */
772
        Single = 0x01,
773
774
        /** Flag can't be omitted.
775
         */
776
        Required = 0x02,
777
778
        /** Flag is excluded from usage line.
779
         */
780
        HiddenFromUsage = 0x04,
781
782
        /** Flag is excluded from options help.
783
         */
784
        HiddenFromDescription = 0x08,
785
786
        /** Flag is global and can be used in any subcommand.
787
         */
788
        Global = 0x10,
789
790
        /** Flag stops a parser.
791
         */
792
        KickOut = 0x20,
793
794
        /** Flag is excluded from auto completion.
795
         */
796
        HiddenFromCompletion = 0x40,
797
798
        /** Flag is excluded from options help and usage line
799
         */
800
        Hidden = HiddenFromUsage | HiddenFromDescription | HiddenFromCompletion,
801
    };
802
803
    inline Options operator | (Options lhs, Options rhs)
804
238
    {
805
238
        return static_cast<Options>(static_cast<int>(lhs) | static_cast<int>(rhs));
806
238
    }
807
808
    inline Options operator & (Options lhs, Options rhs)
809
950k
    {
810
950k
        return static_cast<Options>(static_cast<int>(lhs) & static_cast<int>(rhs));
811
950k
    }
812
813
    class FlagBase;
814
    class PositionalBase;
815
    class Command;
816
    class ArgumentParser;
817
818
    /** A simple structure of parameters for easy user-modifyable help menus
819
     */
820
    struct HelpParams
821
    {
822
        /** The width of the help menu
823
         */
824
        unsigned int width = 80;
825
        /** The indent of the program line
826
         */
827
        unsigned int progindent = 2;
828
        /** The indent of the program trailing lines for long parameters
829
         */
830
        unsigned int progtailindent = 4;
831
        /** The indent of the description and epilogs
832
         */
833
        unsigned int descriptionindent = 4;
834
        /** The indent of the flags
835
         */
836
        unsigned int flagindent = 6;
837
        /** The indent of the flag descriptions
838
         */
839
        unsigned int helpindent = 40;
840
        /** The additional indent each group adds
841
         */
842
        unsigned int eachgroupindent = 2;
843
844
        /** The minimum gutter between each flag and its help
845
         */
846
        unsigned int gutter = 1;
847
848
        /** Show the terminator when both options and positional parameters are present
849
         */
850
        bool showTerminator = true;
851
852
        /** Show the {OPTIONS} on the prog line when this is true
853
         */
854
        bool showProglineOptions = true;
855
856
        /** Show the positionals on the prog line when this is true
857
         */
858
        bool showProglinePositionals = true;
859
860
        /** The prefix for short flags
861
         */
862
        std::string shortPrefix;
863
864
        /** The prefix for long flags
865
         */
866
        std::string longPrefix;
867
868
        /** The separator for short flags
869
         */
870
        std::string shortSeparator;
871
872
        /** The separator for long flags
873
         */
874
        std::string longSeparator;
875
876
        /** The program name for help generation
877
         */
878
        std::string programName;
879
880
        /** Show command's flags
881
         */
882
        bool showCommandChildren = false;
883
884
        /** Show command's descriptions and epilog
885
         */
886
        bool showCommandFullHelp = false;
887
888
        /** The postfix for progline when showProglineOptions is true and command has any flags
889
         */
890
        std::string proglineOptions = "{OPTIONS}";
891
892
        /** The prefix for progline when command has any subcommands
893
         */
894
        std::string proglineCommand = "COMMAND";
895
896
        /** The prefix for progline value
897
         */
898
        std::string proglineValueOpen = " <";
899
900
        /** The postfix for progline value
901
         */
902
        std::string proglineValueClose = ">";
903
904
        /** The prefix for progline required argument
905
         */
906
        std::string proglineRequiredOpen = "";
907
908
        /** The postfix for progline required argument
909
         */
910
        std::string proglineRequiredClose = "";
911
912
        /** The prefix for progline non-required argument
913
         */
914
        std::string proglineNonrequiredOpen = "[";
915
916
        /** The postfix for progline non-required argument
917
         */
918
        std::string proglineNonrequiredClose = "]";
919
920
        /** Show flags in program line
921
         */
922
        bool proglineShowFlags = false;
923
924
        /** Use short flags in program lines when possible
925
         */
926
        bool proglinePreferShortFlags = false;
927
928
        /** Program line prefix
929
         */
930
        std::string usageString;
931
932
        /** String shown in help before flags descriptions
933
         */
934
        std::string optionsString = "OPTIONS:";
935
936
        /** Display value name after all the long and short flags
937
         */
938
        bool useValueNameOnce = false;
939
940
        /** Show value name
941
         */
942
        bool showValueName = true;
943
944
        /** Add newline before flag description
945
         */
946
        bool addNewlineBeforeDescription = false;
947
948
        /** The prefix for option value
949
         */
950
        std::string valueOpen = "[";
951
952
        /** The postfix for option value
953
         */
954
        std::string valueClose = "]";
955
956
        /** Add choices to argument description
957
         */
958
        bool addChoices = false;
959
960
        /** The prefix for choices
961
         */
962
        std::string choiceString = "\nOne of: ";
963
964
        /** Add default values to argument description
965
         */
966
        bool addDefault = false;
967
968
        /** The prefix for default values
969
         */
970
        std::string defaultString = "\nDefault: ";
971
    };
972
973
    /** A number of arguments which can be consumed by an option.
974
     *
975
     * Represents a closed interval [min, max].
976
     */
977
    struct Nargs
978
    {
979
        const size_t min;
980
        const size_t max;
981
982
2.04k
        Nargs(size_t min_, size_t max_) : min{min_}, max{max_}
983
2.04k
        {
984
2.04k
#ifndef ARGS_NOEXCEPT
985
2.04k
            if (max < min)
986
0
            {
987
0
                throw UsageError("Nargs: max < min");
988
0
            }
989
2.04k
#endif
990
2.04k
        }
991
992
324k
        Nargs(size_t num_) : min{num_}, max{num_}
993
324k
        {
994
324k
        }
995
996
        friend bool operator == (const Nargs &lhs, const Nargs &rhs)
997
148k
        {
998
148k
            return lhs.min == rhs.min && lhs.max == rhs.max;
999
148k
        }
1000
1001
        friend bool operator != (const Nargs &lhs, const Nargs &rhs)
1002
0
        {
1003
0
            return !(lhs == rhs);
1004
0
        }
1005
    };
1006
1007
    /** Base class for all match types
1008
     */
1009
    class Base
1010
    {
1011
        private:
1012
            Options options = {};
1013
1014
        protected:
1015
            bool matched = false;
1016
            const std::string help;
1017
#ifdef ARGS_NOEXCEPT
1018
            /// Only for ARGS_NOEXCEPT
1019
            mutable Error error = Error::None;
1020
            mutable std::string errorMsg;
1021
#endif
1022
1023
        public:
1024
164k
            Base(const std::string &help_, Options options_ = {}) : options(options_), help(help_) {}
1025
164k
            virtual ~Base() {}
1026
1027
            Options GetOptions() const noexcept
1028
914k
            {
1029
914k
                return options;
1030
914k
            }
1031
1032
            bool IsRequired() const noexcept
1033
231k
            {
1034
231k
                return (GetOptions() & Options::Required) != Options::None;
1035
231k
            }
1036
1037
            virtual bool Matched() const noexcept
1038
337k
            {
1039
337k
                return matched;
1040
337k
            }
1041
1042
            virtual void Validate(const std::string &, const std::string &) const
1043
0
            {
1044
0
            }
1045
1046
            operator bool() const noexcept
1047
27.4k
            {
1048
27.4k
                return Matched();
1049
27.4k
            }
1050
1051
            virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams &, const unsigned indentLevel) const
1052
0
            {
1053
0
                std::tuple<std::string, std::string, unsigned> description;
1054
0
                std::get<1>(description) = help;
1055
0
                std::get<2>(description) = indentLevel;
1056
0
                return { std::move(description) };
1057
0
            }
1058
1059
            virtual std::vector<Command*> GetCommands()
1060
226k
            {
1061
226k
                return {};
1062
226k
            }
1063
1064
            virtual bool IsGroup() const
1065
98.1k
            {
1066
98.1k
                return false;
1067
98.1k
            }
1068
1069
            virtual bool IsFlag() const
1070
46.8k
            {
1071
46.8k
                return false;
1072
46.8k
            }
1073
1074
            virtual FlagBase *Match(const EitherFlag &)
1075
4.55k
            {
1076
4.55k
                return nullptr;
1077
4.55k
            }
1078
1079
            virtual PositionalBase *GetNextPositional()
1080
123k
            {
1081
123k
                return nullptr;
1082
123k
            }
1083
1084
            virtual std::vector<FlagBase*> GetAllFlags()
1085
696
            {
1086
696
                return {};
1087
696
            }
1088
1089
            virtual bool HasFlag() const
1090
253
            {
1091
253
                return false;
1092
253
            }
1093
1094
            virtual bool HasPositional() const
1095
182k
            {
1096
182k
                return false;
1097
182k
            }
1098
1099
            virtual bool HasCommand() const
1100
144k
            {
1101
144k
                return false;
1102
144k
            }
1103
1104
            virtual std::vector<std::string> GetProgramLine(const HelpParams &) const
1105
0
            {
1106
0
                return {};
1107
0
            }
1108
1109
            /// Sets a kick-out value for building subparsers
1110
            void KickOut(bool kickout_) noexcept
1111
238
            {
1112
238
                if (kickout_)
1113
238
                {
1114
238
                    options = options | Options::KickOut;
1115
238
                }
1116
0
                else
1117
0
                {
1118
0
                    options = static_cast<Options>(static_cast<int>(options) & ~static_cast<int>(Options::KickOut));
1119
0
                }
1120
238
            }
1121
1122
            /// Gets the kick-out value for building subparsers
1123
            bool KickOut() const noexcept
1124
35.8k
            {
1125
35.8k
                return (options & Options::KickOut) != Options::None;
1126
35.8k
            }
1127
1128
            virtual void Reset() noexcept
1129
224k
            {
1130
224k
                matched = false;
1131
#ifdef ARGS_NOEXCEPT
1132
                error = Error::None;
1133
                errorMsg.clear();
1134
#endif
1135
224k
            }
1136
1137
#ifdef ARGS_NOEXCEPT
1138
            /// Only for ARGS_NOEXCEPT
1139
            virtual Error GetError() const
1140
            {
1141
                return error;
1142
            }
1143
1144
            /// Only for ARGS_NOEXCEPT
1145
            virtual std::string GetErrorMsg() const
1146
            {
1147
                return errorMsg;
1148
            }
1149
#endif
1150
    };
1151
1152
    /** Base class for all match types that have a name
1153
     */
1154
    class NamedBase : public Base
1155
    {
1156
        protected:
1157
            const std::string name;
1158
            bool kickout = false;
1159
            std::string defaultString;
1160
            bool defaultStringManual = false;
1161
            std::vector<std::string> choicesStrings;
1162
            bool choicesStringManual = false;
1163
1164
196k
            virtual std::string GetDefaultString(const HelpParams&) const { return {}; }
1165
1166
251k
            virtual std::vector<std::string> GetChoicesStrings(const HelpParams&) const { return {}; }
1167
1168
54.0k
            virtual std::string GetNameString(const HelpParams&) const { return Name(); }
1169
1170
            void AddDescriptionPostfix(std::string &dest, const bool isManual, const std::string &manual, bool isGenerated, const std::string &generated, const std::string &str) const
1171
555k
            {
1172
555k
                if (isManual && !manual.empty())
1173
0
                {
1174
0
                    dest += str;
1175
0
                    dest += manual;
1176
0
                }
1177
555k
                else if (!isManual && isGenerated && !generated.empty())
1178
32.3k
                {
1179
32.3k
                    dest += str;
1180
32.3k
                    dest += generated;
1181
32.3k
                }
1182
555k
            }
1183
1184
        public:
1185
129k
            NamedBase(const std::string &name_, const std::string &help_, Options options_ = {}) : Base(help_, options_), name(name_) {}
1186
129k
            virtual ~NamedBase() {}
1187
1188
            /** Sets default value string that will be added to argument description.
1189
             *  Use empty string to disable it for this argument.
1190
             */
1191
            void HelpDefault(const std::string &str)
1192
0
            {
1193
0
                defaultStringManual = true;
1194
0
                defaultString = str;
1195
0
            }
1196
1197
            /** Gets default value string that will be added to argument description.
1198
             */
1199
            std::string HelpDefault(const HelpParams &params) const
1200
0
            {
1201
0
                return defaultStringManual ? defaultString : GetDefaultString(params);
1202
0
            }
1203
1204
            /** Sets choices strings that will be added to argument description.
1205
             *  Use empty vector to disable it for this argument.
1206
             */
1207
            void HelpChoices(const std::vector<std::string> &array)
1208
0
            {
1209
0
                choicesStringManual = true;
1210
0
                choicesStrings = array;
1211
0
            }
1212
1213
            /** Gets choices strings that will be added to argument description.
1214
             */
1215
            std::vector<std::string> HelpChoices(const HelpParams &params) const
1216
213
            {
1217
213
                return choicesStringManual ? choicesStrings : GetChoicesStrings(params);
1218
213
            }
1219
1220
            virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams &params, const unsigned indentLevel) const override
1221
277k
            {
1222
277k
                std::tuple<std::string, std::string, unsigned> description;
1223
277k
                std::get<0>(description) = GetNameString(params);
1224
277k
                std::get<1>(description) = help;
1225
277k
                std::get<2>(description) = indentLevel;
1226
1227
277k
                AddDescriptionPostfix(std::get<1>(description), choicesStringManual, detail::Join(choicesStrings, ", "), params.addChoices, detail::Join(GetChoicesStrings(params), ", "), params.choiceString);
1228
277k
                AddDescriptionPostfix(std::get<1>(description), defaultStringManual, defaultString, params.addDefault, GetDefaultString(params), params.defaultString);
1229
1230
277k
                return { std::move(description) };
1231
277k
            }
1232
1233
            virtual std::string Name() const
1234
127k
            {
1235
127k
                return name;
1236
127k
            }
1237
    };
1238
1239
    namespace detail
1240
    {
1241
        template<typename T>
1242
        using vector = std::vector<T, std::allocator<T>>;
1243
        
1244
        template<typename K, typename T>
1245
        using unordered_map = std::unordered_map<K, T, std::hash<K>, 
1246
            std::equal_to<K>, std::allocator<std::pair<const K, T> > >;
1247
1248
        template<typename S, typename T>
1249
        class is_streamable
1250
        {
1251
            template<typename SS, typename TT>
1252
            static auto test(int)
1253
            -> decltype( std::declval<SS&>() << std::declval<TT>(), std::true_type() );
1254
1255
            template<typename, typename>
1256
            static auto test(...) -> std::false_type;
1257
1258
        public:
1259
            using type = decltype(test<S,T>(0));
1260
        };
1261
1262
        template <typename T>
1263
        using IsConvertableToString = typename is_streamable<std::ostringstream, T>::type;
1264
1265
        template <typename T>
1266
        typename std::enable_if<IsConvertableToString<T>::value, std::string>::type
1267
        ToString(const T &value)
1268
162k
        {
1269
162k
            std::ostringstream s;
1270
162k
            s << value;
1271
162k
            return s.str();
1272
162k
        }
_ZN4args6detail8ToStringIiEENSt3__19enable_ifIXsr21IsConvertableToStringIT_EE5valueENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEE4typeERKS4_
Line
Count
Source
1268
26.9k
        {
1269
26.9k
            std::ostringstream s;
1270
26.9k
            s << value;
1271
26.9k
            return s.str();
1272
26.9k
        }
_ZN4args6detail8ToStringIdEENSt3__19enable_ifIXsr21IsConvertableToStringIT_EE5valueENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEE4typeERKS4_
Line
Count
Source
1268
26.9k
        {
1269
26.9k
            std::ostringstream s;
1270
26.9k
            s << value;
1271
26.9k
            return s.str();
1272
26.9k
        }
_ZN4args6detail8ToStringINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEENS2_9enable_ifIXsr21IsConvertableToStringIT_EE5valueES8_E4typeERKSA_
Line
Count
Source
1268
108k
        {
1269
108k
            std::ostringstream s;
1270
108k
            s << value;
1271
108k
            return s.str();
1272
108k
        }
1273
1274
        template <typename T>
1275
        typename std::enable_if<!IsConvertableToString<T>::value, std::string>::type
1276
        ToString(const T &)
1277
        {
1278
            return {};
1279
        }
1280
1281
        // Keep emplace_back for sequences: insert can require move assignment
1282
        // even when appending a copyable value at the end of a vector.
1283
        template <typename Container, typename T>
1284
        auto AppendMappedValue(Container &container, const T &value, int)
1285
            -> decltype(container.emplace_back(value), void())
1286
219
        {
1287
219
            container.emplace_back(value);
1288
219
        }
1289
1290
        template <typename Container, typename T>
1291
        auto AppendMappedValue(Container &container, const T &value, long)
1292
            -> decltype(container.insert(std::end(container), value), void())
1293
        {
1294
            container.insert(std::end(container), value);
1295
        }
1296
1297
        template <typename T>
1298
        std::vector<std::string> MapKeysToStrings(const T &map)
1299
27.0k
        {
1300
27.0k
            std::vector<std::string> res;
1301
27.0k
            using K = typename std::decay<decltype(std::begin(map)->first)>::type;
1302
27.0k
            if (IsConvertableToString<K>::value)
1303
27.0k
            {
1304
27.0k
                for (const auto &p : map)
1305
80.9k
                {
1306
80.9k
                    res.push_back(detail::ToString(p.first));
1307
80.9k
                }
1308
1309
27.0k
                std::sort(res.begin(), res.end());
1310
27.0k
            }
1311
27.0k
            return res;
1312
27.0k
        }
1313
    }
1314
1315
    /** Base class for all flag options
1316
     */
1317
    class FlagBase : public NamedBase
1318
    {
1319
        protected:
1320
            const Matcher matcher;
1321
1322
            virtual std::string GetNameString(const HelpParams &params) const override
1323
223k
            {
1324
223k
                const std::string postfix = !params.showValueName || NumberOfArguments() == 0 ? std::string() : Name();
1325
223k
                std::string flags;
1326
223k
                const auto flagStrings = matcher.GetFlagStrings();
1327
223k
                const bool useValueNameOnce = flagStrings.size() == 1 ? false : params.useValueNameOnce;
1328
617k
                for (auto it = flagStrings.begin(); it != flagStrings.end(); ++it)
1329
393k
                {
1330
393k
                    auto &flag = *it;
1331
393k
                    if (it != flagStrings.begin())
1332
170k
                    {
1333
170k
                        flags += ", ";
1334
170k
                    }
1335
1336
393k
                    flags += flag.isShort ? params.shortPrefix : params.longPrefix;
1337
393k
                    flags += flag.str();
1338
1339
393k
                    if (!postfix.empty() && (!useValueNameOnce || it + 1 == flagStrings.end()))
1340
61.4k
                    {
1341
61.4k
                        flags += flag.isShort ? params.shortSeparator : params.longSeparator;
1342
61.4k
                        flags += params.valueOpen + postfix + params.valueClose;
1343
61.4k
                    }
1344
393k
                }
1345
1346
223k
                return flags;
1347
223k
            }
1348
1349
        public:
1350
0
            FlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, const bool extraError_ = false) : NamedBase(name_, help_, extraError_ ? Options::Single : Options()), matcher(std::move(matcher_)) {}
1351
1352
103k
            FlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_) : NamedBase(name_, help_, options_), matcher(std::move(matcher_)) {}
1353
1354
103k
            virtual ~FlagBase() {}
1355
1356
            virtual bool IsFlag() const override
1357
103k
            {
1358
103k
                return true;
1359
103k
            }
1360
1361
            virtual FlagBase *Match(const EitherFlag &flag) override
1362
129k
            {
1363
129k
                if (matcher.Match(flag))
1364
28.6k
                {
1365
28.6k
                    if ((GetOptions() & Options::Single) != Options::None && matched)
1366
1
                    {
1367
1
                        std::ostringstream problem;
1368
1
                        problem << "Flag '" << flag.str() << "' was passed multiple times, but is only allowed to be passed once";
1369
#ifdef ARGS_NOEXCEPT
1370
                        error = Error::Extra;
1371
                        errorMsg = problem.str();
1372
#else
1373
1
                        throw ExtraError(problem.str());
1374
1
#endif
1375
1
                    }
1376
28.6k
                    matched = true;
1377
28.6k
                    return this;
1378
28.6k
                }
1379
100k
                return nullptr;
1380
129k
            }
1381
1382
            virtual std::vector<FlagBase*> GetAllFlags() override
1383
1.80k
            {
1384
1.80k
                return { this };
1385
1.80k
            }
1386
1387
            const Matcher &GetMatcher() const
1388
517k
            {
1389
517k
                return matcher;
1390
517k
            }
1391
1392
            virtual void Validate(const std::string &shortPrefix, const std::string &longPrefix) const override
1393
93.0k
            {
1394
93.0k
                if (!Matched() && IsRequired())
1395
1.96k
                {
1396
1.96k
                        std::ostringstream problem;
1397
1.96k
                        problem << "Flag '" << matcher.GetLongOrAny().str(shortPrefix, longPrefix) << "' is required";
1398
#ifdef ARGS_NOEXCEPT
1399
                        error = Error::Required;
1400
                        errorMsg = problem.str();
1401
#else
1402
1.96k
                        throw RequiredError(problem.str());
1403
1.96k
#endif
1404
1.96k
                }
1405
93.0k
            }
1406
1407
            virtual std::vector<std::string> GetProgramLine(const HelpParams &params) const override
1408
223k
            {
1409
223k
                if (!params.proglineShowFlags)
1410
148k
                {
1411
148k
                    return {};
1412
148k
                }
1413
1414
74.1k
                const std::string postfix = NumberOfArguments() == 0 ? std::string() : Name();
1415
74.1k
                const EitherFlag flag = params.proglinePreferShortFlags ? matcher.GetShortOrAny() : matcher.GetLongOrAny();
1416
74.1k
                std::string res = flag.str(params.shortPrefix, params.longPrefix);
1417
74.1k
                if (!postfix.empty())
1418
44.8k
                {
1419
44.8k
                    res += params.proglineValueOpen + postfix + params.proglineValueClose;
1420
44.8k
                }
1421
1422
74.1k
                return { IsRequired() ? params.proglineRequiredOpen + res + params.proglineRequiredClose
1423
74.1k
                                      : params.proglineNonrequiredOpen + res + params.proglineNonrequiredClose };
1424
223k
            }
1425
1426
            virtual bool HasFlag() const override
1427
47.4k
            {
1428
47.4k
                return true;
1429
47.4k
            }
1430
1431
#ifdef ARGS_NOEXCEPT
1432
            /// Only for ARGS_NOEXCEPT
1433
            bool usageError = false;
1434
            void SetUsageError()
1435
            {
1436
                usageError = true;
1437
            }
1438
            void ClearUsageError()
1439
            {
1440
                usageError = false;
1441
            }
1442
            virtual Error GetError() const override
1443
            {
1444
                if(usageError)
1445
                {
1446
                    return Error::Usage;
1447
                }
1448
                const auto nargs = NumberOfArguments();
1449
                if (nargs.min > nargs.max)
1450
                {
1451
                    return Error::Usage;
1452
                }
1453
1454
                const auto matcherError = matcher.GetError();
1455
                if (matcherError != Error::None)
1456
                {
1457
                    return matcherError;
1458
                }
1459
1460
                return error;
1461
            }
1462
#endif
1463
1464
            /** Defines how many values can be consumed by this option.
1465
             *
1466
             * \return closed interval [min, max]
1467
             */
1468
            virtual Nargs NumberOfArguments() const noexcept = 0;
1469
1470
            /** Parse values of this option.
1471
             *
1472
             * \param value Vector of values. It's size must be in NumberOfArguments() interval.
1473
             */
1474
            virtual void ParseValue(const std::vector<std::string> &value) = 0;
1475
    };
1476
1477
    /** Base class for value-accepting flag options
1478
     */
1479
    class ValueFlagBase : public FlagBase
1480
    {
1481
        public:
1482
0
            ValueFlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, const bool extraError_ = false) : FlagBase(name_, help_, std::move(matcher_), extraError_) {}
1483
60.1k
            ValueFlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_) : FlagBase(name_, help_, std::move(matcher_), options_) {}
1484
0
            virtual ~ValueFlagBase() {}
1485
1486
            virtual Nargs NumberOfArguments() const noexcept override
1487
96.0k
            {
1488
96.0k
                return 1;
1489
96.0k
            }
1490
    };
1491
1492
    class CompletionFlag : public ValueFlagBase
1493
    {
1494
        public:
1495
            std::vector<std::string> reply;
1496
            size_t cword = 0;
1497
            std::string syntax;
1498
1499
            template <typename GroupClass>
1500
2.49k
            CompletionFlag(GroupClass &group_, Matcher &&matcher_): ValueFlagBase("completion", "completion flag", std::move(matcher_), Options::Hidden)
1501
2.49k
            {
1502
2.49k
                group_.AddCompletion(*this);
1503
2.49k
            }
1504
1505
2.49k
            virtual ~CompletionFlag() {}
1506
1507
            virtual Nargs NumberOfArguments() const noexcept override
1508
4.99k
            {
1509
4.99k
                return 2;
1510
4.99k
            }
1511
1512
            virtual void ParseValue(const std::vector<std::string> &value_) override
1513
0
            {
1514
0
                syntax = value_.at(0);
1515
0
                const std::string &raw = value_.at(1);
1516
0
                bool failed = false;
1517
0
1518
0
                const auto firstNonSpace = std::find_if_not(raw.begin(), raw.end(), [](char c)
1519
3.49k
                {
1520
3.49k
                    return std::isspace(static_cast<unsigned char>(c)) != 0;
1521
3.49k
                });
1522
0
1523
0
                // Reject explicit signs: cword must be a plain non-negative
1524
0
                // decimal index. istringstream would otherwise silently
1525
0
                // accept "+1".
1526
0
                if (firstNonSpace != raw.end() && (*firstNonSpace == '-' || *firstNonSpace == '+'))
1527
0
                {
1528
0
                    failed = true;
1529
0
                }
1530
0
1531
0
                size_t parsed = 0;
1532
0
                if (!failed)
1533
0
                {
1534
0
                    std::istringstream ss(raw);
1535
0
                    // Use the C locale so that the cword index parses
1536
0
                    // consistently regardless of any std::locale::global call
1537
0
                    // elsewhere in the process. A locale with a non-empty
1538
0
                    // grouping facet would otherwise reject digit-only inputs
1539
0
                    // like "12" when grouping rules expect separators.
1540
0
                    ss.imbue(std::locale::classic());
1541
0
                    ss >> parsed;
1542
0
                    if (ss.fail())
1543
0
                    {
1544
0
                        failed = true;
1545
0
                    }
1546
0
                    else
1547
0
                    {
1548
0
                        char extra;
1549
0
                        if (ss >> extra)
1550
0
                        {
1551
0
                            failed = true;
1552
0
                        }
1553
0
                        else if (!ss.eof())
1554
0
                        {
1555
0
                            failed = true;
1556
0
                        }
1557
0
                    }
1558
0
                }
1559
0
1560
0
                if (failed)
1561
0
                {
1562
0
#ifdef ARGS_NOEXCEPT
1563
0
                    error = Error::Parse;
1564
0
                    errorMsg = "Argument 'completion' received invalid value type '" + raw + "'";
1565
0
#else
1566
0
                    std::ostringstream problem;
1567
0
                    problem << "Argument 'completion' received invalid value type '" << raw << "'";
1568
0
                    throw ParseError(problem.str());
1569
0
#endif
1570
0
                    return;
1571
0
                }
1572
0
1573
0
                cword = parsed;
1574
0
            }
1575
1576
            /** Get the completion reply
1577
             */
1578
            std::string Get() noexcept
1579
757
            {
1580
757
                return detail::Join(reply, "\n");
1581
757
            }
1582
1583
            virtual void Reset() noexcept override
1584
4.98k
            {
1585
4.98k
                ValueFlagBase::Reset();
1586
4.98k
                cword = 0;
1587
4.98k
                syntax.clear();
1588
4.98k
                reply.clear();
1589
4.98k
            }
1590
    };
1591
1592
1593
    /** Base class for positional options
1594
     */
1595
    class PositionalBase : public NamedBase
1596
    {
1597
        protected:
1598
            bool ready;
1599
1600
        public:
1601
25.9k
            PositionalBase(const std::string &name_, const std::string &help_, Options options_ = {}) : NamedBase(name_, help_, options_), ready(true) {}
1602
0
            virtual ~PositionalBase() {}
1603
1604
            bool Ready()
1605
19.4k
            {
1606
19.4k
                return ready;
1607
19.4k
            }
1608
1609
            virtual void ParseValue(const std::string &value_) = 0;
1610
1611
            virtual void Reset() noexcept override
1612
43.5k
            {
1613
43.5k
                matched = false;
1614
43.5k
                ready = true;
1615
#ifdef ARGS_NOEXCEPT
1616
                error = Error::None;
1617
                errorMsg.clear();
1618
#endif
1619
43.5k
            }
1620
1621
            virtual PositionalBase *GetNextPositional() override
1622
19.4k
            {
1623
19.4k
                return Ready() ? this : nullptr;
1624
19.4k
            }
1625
1626
            virtual bool HasPositional() const override
1627
20.3k
            {
1628
20.3k
                return true;
1629
20.3k
            }
1630
1631
            virtual std::vector<std::string> GetProgramLine(const HelpParams &params) const override
1632
54.0k
            {
1633
54.0k
                return { IsRequired() ? params.proglineRequiredOpen + Name() + params.proglineRequiredClose
1634
54.0k
                                      : params.proglineNonrequiredOpen + Name() + params.proglineNonrequiredClose };
1635
54.0k
            }
1636
1637
            virtual void Validate(const std::string &, const std::string &) const override
1638
13.2k
            {
1639
13.2k
                if (IsRequired() && !Matched())
1640
0
                {
1641
0
                    std::ostringstream problem;
1642
0
                    problem << "Option '" << Name() << "' is required";
1643
#ifdef ARGS_NOEXCEPT
1644
                    error = Error::Required;
1645
                    errorMsg = problem.str();
1646
#else
1647
0
                    throw RequiredError(problem.str());
1648
0
#endif
1649
0
                }
1650
13.2k
            }
1651
    };
1652
1653
    /** Class for all kinds of validating groups, including ArgumentParser
1654
     */
1655
    class Group : public Base
1656
    {
1657
        private:
1658
            Group* parent;
1659
            std::vector<Base*> children;
1660
            std::function<bool(const Group &)> validator;
1661
1662
        public:
1663
            /** Default validators
1664
             */
1665
            struct Validators
1666
            {
1667
                static bool Xor(const Group &group)
1668
542
                {
1669
542
                    return group.MatchedChildren() == 1;
1670
542
                }
1671
1672
                static bool AtLeastOne(const Group &group)
1673
1.05k
                {
1674
1.05k
                    return group.MatchedChildren() >= 1;
1675
1.05k
                }
1676
1677
                static bool AtMostOne(const Group &group)
1678
401
                {
1679
401
                    return group.MatchedChildren() <= 1;
1680
401
                }
1681
1682
                static bool All(const Group &group)
1683
483
                {
1684
483
                    return group.Children().size() == group.MatchedChildren();
1685
483
                }
1686
1687
                static bool AllOrNone(const Group &group)
1688
483
                {
1689
483
                    return (All(group) || None(group));
1690
483
                }
1691
1692
                static bool AllChildGroups(const Group &group)
1693
535
                {
1694
805
                    return std::none_of(std::begin(group.Children()), std::end(group.Children()), [](const Base* child) -> bool {
1695
805
                            return child->IsGroup() && !child->Matched();
1696
805
                            });
1697
535
                }
1698
1699
                static bool DontCare(const Group &)
1700
3.17k
                {
1701
3.17k
                    return true;
1702
3.17k
                }
1703
1704
                static bool CareTooMuch(const Group &)
1705
0
                {
1706
0
                    return false;
1707
0
                }
1708
1709
                static bool None(const Group &group)
1710
482
                {
1711
482
                    return group.MatchedChildren() == 0;
1712
482
                }
1713
            };
1714
            /// If help is empty, this group will not be printed in help output
1715
26.8k
            Group(const std::string &help_ = std::string(), const std::function<bool(const Group &)> &validator_ = Validators::DontCare, Options options_ = {}) : Base(help_, options_), validator(validator_)
1716
26.8k
            {
1717
26.8k
                parent = nullptr;
1718
26.8k
            }
1719
            /// If help is empty, this group will not be printed in help output
1720
8.56k
            Group(Group &group_, const std::string &help_ = std::string(), const std::function<bool(const Group &)> &validator_ = Validators::DontCare, Options options_ = {}) : Base(help_, options_), validator(validator_)
1721
8.56k
            {
1722
8.56k
                group_.Add(*this);
1723
8.56k
                parent = &group_;
1724
8.56k
            }
1725
35.3k
            virtual ~Group() {}
1726
1727
            /** Append a child to this Group.
1728
             */
1729
            void Add(Base &child)
1730
150k
            {
1731
150k
                children.emplace_back(&child);
1732
1733
150k
                if(child.IsFlag()) {
1734
103k
#ifndef ARGS_NOEXCEPT
1735
                    // Detection runs from the child's own constructor, so a
1736
                    // duplicate throws before that constructor completes and the
1737
                    // child's storage is released while the stack unwinds. Undo
1738
                    // the registration first, or a caller that catches the error
1739
                    // leaves this group holding a pointer to a dead object.
1740
103k
                    try
1741
103k
                    {
1742
103k
                        SignalDetectDuplicates();
1743
103k
                    }
1744
103k
                    catch (...)
1745
103k
                    {
1746
0
                        children.pop_back();
1747
0
                        throw;
1748
0
                    }
1749
#else
1750
                    SignalDetectDuplicates();
1751
#endif
1752
103k
                }
1753
150k
            }
1754
1755
            /** Get all this group's children
1756
             */
1757
            const std::vector<Base *> &Children() const
1758
630k
            {
1759
630k
                return children;
1760
630k
            }
1761
1762
            /** Return the first FlagBase that matches flag, or nullptr
1763
             *
1764
             * \param flag The flag with prefixes stripped
1765
             * \return the first matching FlagBase pointer, or nullptr if there is no match
1766
             */
1767
            virtual FlagBase *Match(const EitherFlag &flag) override
1768
36.6k
            {
1769
36.6k
                for (Base *child: Children())
1770
140k
                {
1771
140k
                    if (FlagBase *match = child->Match(flag))
1772
31.2k
                    {
1773
31.2k
                        return match;
1774
31.2k
                    }
1775
140k
                }
1776
5.34k
                return nullptr;
1777
36.6k
            }
1778
1779
            virtual std::vector<FlagBase*> GetAllFlags() override
1780
72
            {
1781
72
                std::vector<FlagBase*> res;
1782
72
                for (Base *child: Children())
1783
60
                {
1784
60
                    auto childRes = child->GetAllFlags();
1785
60
                    res.insert(res.end(), childRes.begin(), childRes.end());
1786
60
                }
1787
72
                return res;
1788
72
            }
1789
1790
            virtual void Validate(const std::string &shortPrefix, const std::string &longPrefix) const override
1791
5.22k
            {
1792
5.22k
                for (Base *child: Children())
1793
9.57k
                {
1794
9.57k
                    child->Validate(shortPrefix, longPrefix);
1795
9.57k
                }
1796
5.22k
            }
1797
1798
            /** Get the next ready positional, or nullptr if there is none
1799
             *
1800
             * \return the first ready PositionalBase pointer, or nullptr if there is no match
1801
             */
1802
            virtual PositionalBase *GetNextPositional() override
1803
21.6k
            {
1804
21.6k
                for (Base *child: Children())
1805
149k
                {
1806
149k
                    if (auto next = child->GetNextPositional())
1807
12.3k
                    {
1808
12.3k
                        return next;
1809
12.3k
                    }
1810
149k
                }
1811
9.30k
                return nullptr;
1812
21.6k
            }
1813
1814
            /** Get whether this has any FlagBase children
1815
             *
1816
             * \return Whether or not there are any FlagBase children
1817
             */
1818
            virtual bool HasFlag() const override
1819
47.7k
            {
1820
47.7k
                return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasFlag(); });
1821
47.7k
            }
1822
1823
            /** Get whether this has any PositionalBase children
1824
             *
1825
             * \return Whether or not there are any PositionalBase children
1826
             */
1827
            virtual bool HasPositional() const override
1828
40.9k
            {
1829
222k
                return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasPositional(); });
1830
40.9k
            }
1831
1832
            /** Get whether this has any Command children
1833
             *
1834
             * \return Whether or not there are any Command children
1835
             */
1836
            virtual bool HasCommand() const override
1837
24.1k
            {
1838
155k
                return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasCommand(); });
1839
24.1k
            }
1840
1841
            /** Count the number of matched children this group has
1842
             */
1843
            std::vector<Base *>::size_type MatchedChildren() const
1844
2.95k
            {
1845
                // Cast to avoid warnings from -Wsign-conversion
1846
2.95k
                return static_cast<std::vector<Base *>::size_type>(
1847
5.91k
                        std::count_if(std::begin(Children()), std::end(Children()), [](const Base *child){return child->Matched();}));
1848
2.95k
            }
1849
1850
            /** Get the list of children which were matched
1851
             */
1852
            std::vector<Base *> GetMatchedChildren() const
1853
0
            {
1854
0
                // Could be replaced by C++ 20 filter, or a custom iterator.
1855
0
                std::vector<Base*> matched_children;
1856
0
                std::copy_if(children.begin(), children.end(), std::back_inserter(matched_children), [](Base* b){
1857
0
                    return b->Matched();
1858
0
                });
1859
0
                return matched_children;
1860
0
            }
1861
1862
            /** Gets the children which are a certain type.
1863
              * \tparam ChildType The type of child to select. 
1864
              * \param matching Return only children of the type which matched (default false).
1865
              * \return Vector of children meeting the criteria.
1866
             */
1867
             template <typename ChildType>
1868
             std::vector<ChildType *> GetFilteredChildren(bool matching = false) const
1869
             {
1870
                std::vector<ChildType *> filtered_children;
1871
                for(Base *child : children) {
1872
                    if(!matching || child->Matched())
1873
                    {
1874
                        ChildType* cast_result = dynamic_cast<ChildType*>(child);
1875
                        if(cast_result != nullptr)
1876
                        {
1877
                            filtered_children.push_back(cast_result);
1878
                        }
1879
1880
                    }
1881
                }
1882
                return filtered_children;
1883
             }
1884
1885
            /** Whether or not this group matches validation
1886
             */
1887
            virtual bool Matched() const noexcept override
1888
6.19k
            {
1889
6.19k
                return validator(*this);
1890
6.19k
            }
1891
1892
            /** Get validation
1893
             */
1894
            bool Get() const
1895
0
            {
1896
0
                return Matched();
1897
0
            }
1898
1899
            /** Get all the child descriptions for help generation
1900
             */
1901
            virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams &params, const unsigned int indent) const override
1902
27.4k
            {
1903
27.4k
                std::vector<std::tuple<std::string, std::string, unsigned int>> descriptions;
1904
1905
                // Push that group description on the back if not empty
1906
27.4k
                unsigned addindent = 0;
1907
27.4k
                if (!help.empty())
1908
1.26k
                {
1909
1.26k
                    descriptions.emplace_back(help, "", indent);
1910
1.26k
                    addindent = 1;
1911
1.26k
                }
1912
1913
27.4k
                for (Base *child: Children())
1914
54.6k
                {
1915
54.6k
                    if ((child->GetOptions() & Options::HiddenFromDescription) != Options::None)
1916
0
                    {
1917
0
                        continue;
1918
0
                    }
1919
1920
54.6k
                    auto groupDescriptions = child->GetDescription(params, indent + addindent);
1921
54.6k
                    descriptions.insert(
1922
54.6k
                        std::end(descriptions),
1923
54.6k
                        std::make_move_iterator(std::begin(groupDescriptions)),
1924
54.6k
                        std::make_move_iterator(std::end(groupDescriptions)));
1925
54.6k
                }
1926
27.4k
                return descriptions;
1927
27.4k
            }
1928
1929
            /** Get the names of positional parameters
1930
             */
1931
            virtual std::vector<std::string> GetProgramLine(const HelpParams &params) const override
1932
54.3k
            {
1933
54.3k
                std::vector <std::string> names;
1934
54.3k
                for (Base *child: Children())
1935
323k
                {
1936
323k
                    if ((child->GetOptions() & Options::HiddenFromUsage) != Options::None)
1937
19.6k
                    {
1938
19.6k
                        continue;
1939
19.6k
                    }
1940
1941
304k
                    auto groupNames = child->GetProgramLine(params);
1942
304k
                    names.insert(
1943
304k
                        std::end(names),
1944
304k
                        std::make_move_iterator(std::begin(groupNames)),
1945
304k
                        std::make_move_iterator(std::end(groupNames)));
1946
304k
                }
1947
54.3k
                return names;
1948
54.3k
            }
1949
1950
            virtual std::vector<Command*> GetCommands() override
1951
38.8k
            {
1952
38.8k
                std::vector<Command*> res;
1953
38.8k
                for (const auto &child : Children())
1954
265k
                {
1955
265k
                    auto subparsers = child->GetCommands();
1956
265k
                    res.insert(std::end(res), std::begin(subparsers), std::end(subparsers));
1957
265k
                }
1958
38.8k
                return res;
1959
38.8k
            }
1960
1961
            virtual bool IsGroup() const override
1962
19.1k
            {
1963
19.1k
                return true;
1964
19.1k
            }
1965
1966
            virtual void Reset() noexcept override
1967
57.5k
            {
1968
57.5k
                Base::Reset();
1969
1970
57.5k
                for (auto &child: Children())
1971
245k
                {
1972
245k
                    child->Reset();
1973
245k
                }
1974
#ifdef ARGS_NOEXCEPT
1975
                error = Error::None;
1976
                errorMsg.clear();
1977
#endif
1978
57.5k
            }
1979
1980
            /** Sends a signal to the root of the tree to begin checking for
1981
              * duplicates. If this is the root, begins checking.
1982
              */
1983
            void SignalDetectDuplicates()
1984
117k
            {
1985
117k
                if(parent != nullptr) parent->SignalDetectDuplicates();
1986
103k
                else DetectDuplicateFlags();
1987
117k
            }
1988
1989
            /** Detect duplicate flags.
1990
              * In a noexcept context, sets an error on the duplicate flag.
1991
              * In a normal context, throws a ParseError.
1992
              */
1993
            void DetectDuplicateFlags()
1994
103k
            {
1995
103k
                std::unordered_set<char> usedShortFlags;
1996
103k
                std::unordered_set<std::string> usedLongFlags;
1997
103k
                DetectDuplicateFlags(usedShortFlags, usedLongFlags);
1998
103k
            }
1999
        
2000
            /** Used by parameterless DetectDuplicateFlags.
2001
              */
2002
            void DetectDuplicateFlags(std::unordered_set<char> &usedShortFlags, std::unordered_set<std::string> &usedLongFlags)
2003
117k
            {
2004
117k
                for (Base *child: Children())
2005
529k
                {
2006
529k
                    if(auto flag = dynamic_cast<FlagBase*>(child))
2007
516k
                    {
2008
                        // Check for duplicate flags, setting a usage error on the
2009
                        // flag if a duplicate is detected.
2010
516k
                        for(EitherFlag flagString: flag->GetMatcher().GetFlagStrings())
2011
934k
                        {
2012
934k
                            if(flagString.isShort)
2013
444k
                            {
2014
444k
                                if(usedShortFlags.count(flagString.shortFlag))
2015
0
                                {
2016
#ifdef ARGS_NOEXCEPT
2017
                                    flag->SetUsageError();
2018
#else
2019
0
                                    throw ParseError("duplicate short flag detected");
2020
0
#endif
2021
0
                                }
2022
444k
                                else
2023
444k
                                {
2024
444k
                                    usedShortFlags.insert(flagString.shortFlag);
2025
444k
                                }
2026
444k
                            }
2027
489k
                            else
2028
489k
                            {
2029
489k
                                if(usedLongFlags.count(flagString.longFlag))
2030
0
                                {
2031
#ifdef ARGS_NOEXCEPT
2032
                                    flag->SetUsageError();
2033
#else
2034
0
                                    throw ParseError("duplicate long flag detected");
2035
0
#endif
2036
0
                                }
2037
489k
                                else
2038
489k
                                {
2039
489k
                                    usedLongFlags.insert(flagString.longFlag);
2040
489k
                                }
2041
489k
                            }
2042
934k
                        }
2043
516k
                    }
2044
13.4k
                    else if(auto group = dynamic_cast<Group*>(child))
2045
13.4k
                    {
2046
                        // A command opens its own flag namespace and runs its
2047
                        // own duplicate detection as a separate root, so a flag
2048
                        // reused either side of a command boundary is not a
2049
                        // genuine duplicate. Only descend into plain groups
2050
                        // here; IsGroup() is false for a Command.
2051
13.4k
                        if(group->IsGroup())
2052
13.4k
                        {
2053
13.4k
                            group->DetectDuplicateFlags(usedShortFlags, usedLongFlags);
2054
13.4k
                        }
2055
13.4k
                    }
2056
529k
                }
2057
117k
            }
2058
2059
#ifdef ARGS_NOEXCEPT
2060
            /// Only for ARGS_NOEXCEPT
2061
            virtual Error GetError() const override
2062
            {
2063
                if (error != Error::None)
2064
                {
2065
                    return error;
2066
                }
2067
2068
                auto it = std::find_if(Children().begin(), Children().end(), [](const Base *child){return child->GetError() != Error::None;});
2069
                if (it == Children().end())
2070
                {
2071
                    return Error::None;
2072
                } else
2073
                {
2074
                    return (*it)->GetError();
2075
                }
2076
            }
2077
2078
            /// Only for ARGS_NOEXCEPT
2079
            virtual std::string GetErrorMsg() const override
2080
            {
2081
                if (error != Error::None)
2082
                {
2083
                    return errorMsg;
2084
                }
2085
2086
                auto it = std::find_if(Children().begin(), Children().end(), [](const Base *child){return child->GetError() != Error::None;});
2087
                if (it == Children().end())
2088
                {
2089
                    return "";
2090
                } else
2091
                {
2092
                    return (*it)->GetErrorMsg();
2093
                }
2094
            }
2095
#endif
2096
2097
    };
2098
2099
    /** Class for using global options in ArgumentParser.
2100
     */
2101
    class GlobalOptions : public Group
2102
    {
2103
        public:
2104
1.83k
            GlobalOptions(Group &base, Base &options_) : Group(base, {}, Group::Validators::DontCare, Options::Global)
2105
1.83k
            {
2106
1.83k
                Add(options_);
2107
1.83k
            }
2108
    };
2109
2110
    /** Utility class for building subparsers with coroutines/callbacks.
2111
     *
2112
     * Brief example:
2113
     * \code
2114
     * Command command(argumentParser, "command", "my command", [](args::Subparser &s)
2115
     * {
2116
     *      // your command flags/positionals
2117
     *      s.Parse(); //required
2118
     *      //your command code
2119
     * });
2120
     * \endcode
2121
     *
2122
     * For ARGS_NOEXCEPT mode don't forget to check `s.GetError()` after `s.Parse()`
2123
     * and return if it isn't equals to args::Error::None.
2124
     *
2125
     * \sa Command
2126
     */
2127
    class Subparser : public Group
2128
    {
2129
        private:
2130
            std::vector<std::string> args;
2131
            std::vector<std::string> kicked;
2132
            ArgumentParser *parser = nullptr;
2133
            const HelpParams &helpParams;
2134
            const Command &command;
2135
            bool isParsed = false;
2136
2137
        public:
2138
            Subparser(std::vector<std::string> args_, ArgumentParser &parser_, const Command &command_, const HelpParams &helpParams_)
2139
548
                : Group({}, Validators::AllChildGroups), args(std::move(args_)), parser(&parser_), helpParams(helpParams_), command(command_)
2140
548
            {
2141
548
            }
2142
2143
51
            Subparser(const Command &command_, const HelpParams &helpParams_) : Group({}, Validators::AllChildGroups), helpParams(helpParams_), command(command_)
2144
51
            {
2145
51
            }
2146
2147
            Subparser(const Subparser&) = delete;
2148
            Subparser(Subparser&&) = delete;
2149
            Subparser &operator = (const Subparser&) = delete;
2150
            Subparser &operator = (Subparser&&) = delete;
2151
2152
            const Command &GetCommand()
2153
0
            {
2154
0
                return command;
2155
0
            }
2156
2157
            /** (INTERNAL) Determines whether Parse was called or not.
2158
             */
2159
            bool IsParsed() const
2160
261
            {
2161
261
                return isParsed;
2162
261
            }
2163
2164
            /** Continue parsing arguments for new command.
2165
             */
2166
            void Parse();
2167
2168
            /** Returns a vector of kicked out arguments.
2169
             *
2170
             * \sa Base::KickOut
2171
             */
2172
            const std::vector<std::string> &KickedOut() const noexcept
2173
0
            {
2174
0
                return kicked;
2175
0
            }
2176
    };
2177
2178
    /** Main class for building subparsers.
2179
     *
2180
     * /sa Subparser
2181
     */
2182
    class Command : public Group
2183
    {
2184
        private:
2185
            friend class Subparser;
2186
2187
            std::string name;
2188
            std::string help;
2189
            std::string description;
2190
            std::string epilog;
2191
            std::string proglinePostfix;
2192
2193
            std::function<void(Subparser&)> parserCoroutine;
2194
            bool commandIsRequired = true;
2195
            Command *selectedCommand = nullptr;
2196
2197
            mutable std::vector<std::tuple<std::string, std::string, unsigned>> subparserDescription;
2198
            mutable std::vector<std::string> subparserProgramLine;
2199
            mutable bool subparserHasFlag = false;
2200
            mutable bool subparserHasPositional = false;
2201
            mutable bool subparserHasCommand = false;
2202
#ifdef ARGS_NOEXCEPT
2203
            mutable Error subparserError = Error::None;
2204
#endif
2205
            mutable Subparser *subparser = nullptr;
2206
2207
        protected:
2208
2209
            class RaiiSubparser
2210
            {
2211
                public:
2212
                    RaiiSubparser(ArgumentParser &parser_, std::vector<std::string> args_);
2213
                    RaiiSubparser(const Command &command_, const HelpParams &params_);
2214
2215
                    ~RaiiSubparser()
2216
599
                    {
2217
599
                        command.subparser = oldSubparser;
2218
599
                    }
2219
2220
                    Subparser &Parser()
2221
860
                    {
2222
860
                        return parser;
2223
860
                    }
2224
2225
                private:
2226
                    const Command &command;
2227
                    Subparser parser;
2228
                    Subparser *oldSubparser;
2229
            };
2230
2231
13.8k
            Command() = default;
2232
2233
            std::function<void(Subparser&)> &GetCoroutine()
2234
1.09k
            {
2235
1.09k
                return selectedCommand != nullptr ? selectedCommand->GetCoroutine() : parserCoroutine;
2236
1.09k
            }
2237
2238
            Command &SelectedCommand()
2239
548
            {
2240
548
                Command *res = this;
2241
1.09k
                while (res->selectedCommand != nullptr)
2242
548
                {
2243
548
                    res = res->selectedCommand;
2244
548
                }
2245
2246
548
                return *res;
2247
548
            }
2248
2249
            const Command &SelectedCommand() const
2250
20.1k
            {
2251
20.1k
                const Command *res = this;
2252
20.1k
                while (res->selectedCommand != nullptr)
2253
0
                {
2254
0
                    res = res->selectedCommand;
2255
0
                }
2256
2257
20.1k
                return *res;
2258
20.1k
            }
2259
2260
            void UpdateSubparserHelp(const HelpParams &params) const
2261
53.8k
            {
2262
53.8k
                if (parserCoroutine)
2263
51
                {
2264
51
                    RaiiSubparser coro(*this, params);
2265
51
#ifndef ARGS_NOEXCEPT
2266
51
                    try
2267
51
                    {
2268
51
                        parserCoroutine(coro.Parser());
2269
51
                    }
2270
51
                    catch (args::SubparserError&)
2271
51
                    {
2272
51
                    }
2273
#else
2274
                    parserCoroutine(coro.Parser());
2275
#endif
2276
51
                }
2277
53.8k
            }
2278
2279
        public:
2280
            Command(Group &base_, std::string name_, std::string help_, std::function<void(Subparser&)> coroutine_ = {})
2281
10.5k
                : name(std::move(name_)), help(std::move(help_)), parserCoroutine(std::move(coroutine_))
2282
10.5k
            {
2283
10.5k
                base_.Add(*this);
2284
10.5k
            }
2285
2286
            /** The description that appears on the prog line after options
2287
             */
2288
            const std::string &ProglinePostfix() const
2289
26.9k
            { return proglinePostfix; }
2290
2291
            /** The description that appears on the prog line after options
2292
             */
2293
            void ProglinePostfix(const std::string &proglinePostfix_)
2294
0
            { this->proglinePostfix = proglinePostfix_; }
2295
2296
            /** The description that appears above options
2297
             */
2298
            const std::string &Description() const
2299
21.9k
            { return description; }
2300
            /** The description that appears above options
2301
             */
2302
2303
            void Description(const std::string &description_)
2304
13.8k
            { this->description = description_; }
2305
2306
            /** The description that appears below options
2307
             */
2308
            const std::string &Epilog() const
2309
20.1k
            { return epilog; }
2310
2311
            /** The description that appears below options
2312
             */
2313
            void Epilog(const std::string &epilog_)
2314
13.8k
            { this->epilog = epilog_; }
2315
2316
            /** The name of command
2317
             */
2318
            const std::string &Name() const
2319
38.1k
            { return name; }
2320
2321
            /** The description of command
2322
             */
2323
            const std::string &Help() const
2324
18.4k
            { return help; }
2325
2326
            /** If value is true, parser will fail if no command was parsed.
2327
             *
2328
             * Default: true.
2329
             */
2330
            void RequireCommand(bool value)
2331
1.83k
            { commandIsRequired = value; }
2332
2333
            virtual bool IsGroup() const override
2334
1.84k
            { return false; }
2335
2336
            virtual bool Matched() const noexcept override
2337
191k
            { return Base::Matched(); }
2338
2339
            operator bool() const noexcept
2340
11.0k
            { return Matched(); }
2341
2342
            void Match() noexcept
2343
548
            { matched = true; }
2344
2345
            void SelectCommand(Command *c) noexcept
2346
548
            {
2347
548
                selectedCommand = c;
2348
2349
548
                if (c != nullptr)
2350
548
                {
2351
548
                    c->Match();
2352
548
                }
2353
548
            }
2354
2355
            virtual FlagBase *Match(const EitherFlag &flag) override
2356
34.5k
            {
2357
34.5k
                if (selectedCommand != nullptr)
2358
786
                {
2359
786
                    if (auto *res = selectedCommand->Match(flag))
2360
453
                    {
2361
453
                        return res;
2362
453
                    }
2363
2364
333
                    for (auto *child: Children())
2365
985
                    {
2366
985
                        if ((child->GetOptions() & Options::Global) != Options::None)
2367
400
                        {
2368
400
                            if (auto *res = child->Match(flag))
2369
202
                            {
2370
202
                                return res;
2371
202
                            }
2372
400
                        }
2373
985
                    }
2374
2375
131
                    return nullptr;
2376
333
                }
2377
2378
33.7k
                if (subparser != nullptr)
2379
786
                {
2380
786
                    return subparser->Match(flag);
2381
786
                }
2382
2383
33.0k
                return Matched() ? Group::Match(flag) : nullptr;
2384
33.7k
            }
2385
2386
            virtual std::vector<FlagBase*> GetAllFlags() override
2387
1.18k
            {
2388
1.18k
                std::vector<FlagBase*> res;
2389
2390
1.18k
                if (!Matched())
2391
696
                {
2392
696
                    return res;
2393
696
                }
2394
2395
492
                for (auto *child: Children())
2396
3.78k
                {
2397
3.78k
                    if (selectedCommand == nullptr || (child->GetOptions() & Options::Global) != Options::None)
2398
3.13k
                    {
2399
3.13k
                        auto childFlags = child->GetAllFlags();
2400
3.13k
                        res.insert(res.end(), childFlags.begin(), childFlags.end());
2401
3.13k
                    }
2402
3.78k
                }
2403
2404
492
                if (selectedCommand != nullptr)
2405
72
                {
2406
72
                    auto childFlags = selectedCommand->GetAllFlags();
2407
72
                    res.insert(res.end(), childFlags.begin(), childFlags.end());
2408
72
                }
2409
2410
492
                if (subparser != nullptr)
2411
72
                {
2412
72
                    auto childFlags = subparser->GetAllFlags();
2413
72
                    res.insert(res.end(), childFlags.begin(), childFlags.end());
2414
72
                }
2415
2416
492
                return res;
2417
1.18k
            }
2418
2419
            virtual PositionalBase *GetNextPositional() override
2420
15.0k
            {
2421
15.0k
                if (selectedCommand != nullptr)
2422
1.37k
                {
2423
1.37k
                    if (auto *res = selectedCommand->GetNextPositional())
2424
545
                    {
2425
545
                        return res;
2426
545
                    }
2427
2428
828
                    for (auto *child: Children())
2429
2.29k
                    {
2430
2.29k
                        if ((child->GetOptions() & Options::Global) != Options::None)
2431
1.47k
                        {
2432
1.47k
                            if (auto *res = child->GetNextPositional())
2433
737
                            {
2434
737
                                return res;
2435
737
                            }
2436
1.47k
                        }
2437
2.29k
                    }
2438
2439
91
                    return nullptr;
2440
828
                }
2441
2442
13.6k
                if (subparser != nullptr)
2443
1.37k
                {
2444
1.37k
                    return subparser->GetNextPositional();
2445
1.37k
                }
2446
2447
12.2k
                return Matched() ? Group::GetNextPositional() : nullptr;
2448
13.6k
            }
2449
2450
            virtual bool HasFlag() const override
2451
20.2k
            {
2452
20.2k
                return subparserHasFlag || Group::HasFlag();
2453
20.2k
            }
2454
2455
            virtual bool HasPositional() const override
2456
20.2k
            {
2457
20.2k
                return subparserHasPositional || Group::HasPositional();
2458
20.2k
            }
2459
2460
            virtual bool HasCommand() const override
2461
87
            {
2462
87
                return true;
2463
87
            }
2464
2465
            std::vector<std::string> GetCommandProgramLine(const HelpParams &params) const
2466
26.9k
            {
2467
26.9k
                UpdateSubparserHelp(params);
2468
2469
26.9k
                std::vector<std::string> res;
2470
2471
26.9k
                if ((subparserHasFlag || Group::HasFlag()) && params.showProglineOptions && !params.proglineShowFlags)
2472
3.37k
                {
2473
3.37k
                    res.push_back(params.proglineOptions);
2474
3.37k
                }
2475
2476
26.9k
                auto group_res = Group::GetProgramLine(params);
2477
26.9k
                std::move(std::move(group_res).begin(), std::move(group_res).end(), std::back_inserter(res));
2478
2479
26.9k
                res.insert(res.end(), subparserProgramLine.begin(), subparserProgramLine.end());
2480
2481
26.9k
                if (!params.proglineCommand.empty() && (Group::HasCommand() || subparserHasCommand))
2482
0
                {
2483
0
                    res.insert(res.begin(), commandIsRequired ? params.proglineCommand : "[" + params.proglineCommand + "]");
2484
0
                }
2485
2486
26.9k
                if (!Name().empty())
2487
0
                {
2488
0
                    res.insert(res.begin(), Name());
2489
0
                }
2490
2491
26.9k
                if (!ProglinePostfix().empty())
2492
0
                {
2493
0
                    std::string line;
2494
0
                    for (auto c : ProglinePostfix())
2495
0
                    {
2496
0
                        if (std::isspace(static_cast<unsigned char>(c)))
2497
0
                        {
2498
0
                            if (!line.empty())
2499
0
                            {
2500
0
                                res.push_back(line);
2501
0
                                line.clear();
2502
0
                            }
2503
2504
0
                            if (c == '\n')
2505
0
                            {
2506
0
                                res.push_back("\n");
2507
0
                            }
2508
0
                        }
2509
0
                        else
2510
0
                        {
2511
0
                            line += c;
2512
0
                        }
2513
0
                    }
2514
2515
0
                    if (!line.empty())
2516
0
                    {
2517
0
                        res.push_back(line);
2518
0
                    }
2519
0
                }
2520
2521
26.9k
                return res;
2522
26.9k
            }
2523
2524
            virtual std::vector<std::string> GetProgramLine(const HelpParams &params) const override
2525
26.9k
            {
2526
26.9k
                if (!Matched())
2527
51
                {
2528
51
                    return {};
2529
51
                }
2530
2531
26.9k
                return GetCommandProgramLine(params);
2532
26.9k
            }
2533
2534
            virtual std::vector<Command*> GetCommands() override
2535
51.1k
            {
2536
51.1k
                if (selectedCommand != nullptr)
2537
621
                {
2538
621
                    return selectedCommand->GetCommands();
2539
621
                }
2540
2541
50.5k
                if (Matched())
2542
24.7k
                {
2543
24.7k
                    return Group::GetCommands();
2544
24.7k
                }
2545
2546
25.7k
                return { this };
2547
50.5k
            }
2548
2549
            virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams &params, const unsigned int indent) const override
2550
26.9k
            {
2551
26.9k
                std::vector<std::tuple<std::string, std::string, unsigned>> descriptions;
2552
26.9k
                unsigned addindent = 0;
2553
2554
26.9k
                UpdateSubparserHelp(params);
2555
2556
26.9k
                if (!Matched())
2557
51
                {
2558
51
                    if (params.showCommandFullHelp)
2559
0
                    {
2560
0
                        std::ostringstream s;
2561
0
                        bool empty = true;
2562
0
                        for (const auto &progline: GetCommandProgramLine(params))
2563
0
                        {
2564
0
                            if (!empty)
2565
0
                            {
2566
0
                                s << ' ';
2567
0
                            }
2568
0
                            else
2569
0
                            {
2570
0
                                empty = false;
2571
0
                            }
2572
2573
0
                            s << progline;
2574
0
                        }
2575
2576
0
                        descriptions.emplace_back(s.str(), "", indent);
2577
0
                    }
2578
51
                    else
2579
51
                    {
2580
51
                        descriptions.emplace_back(Name(), help, indent);
2581
51
                    }
2582
2583
51
                    if (!params.showCommandChildren && !params.showCommandFullHelp)
2584
51
                    {
2585
51
                        return descriptions;
2586
51
                    }
2587
2588
0
                    addindent = 1;
2589
0
                }
2590
2591
26.9k
                if (params.showCommandFullHelp && !Matched())
2592
0
                {
2593
0
                    descriptions.emplace_back("", "", indent + addindent);
2594
0
                    descriptions.emplace_back(Description().empty() ? Help() : Description(), "", indent + addindent);
2595
0
                    descriptions.emplace_back("", "", indent + addindent);
2596
0
                }
2597
2598
26.9k
                for (Base *child: Children())
2599
269k
                {
2600
269k
                    if ((child->GetOptions() & Options::HiddenFromDescription) != Options::None)
2601
18.8k
                    {
2602
18.8k
                        continue;
2603
18.8k
                    }
2604
2605
250k
                    auto groupDescriptions = child->GetDescription(params, indent + addindent);
2606
250k
                    descriptions.insert(
2607
250k
                                        std::end(descriptions),
2608
250k
                                        std::make_move_iterator(std::begin(groupDescriptions)),
2609
250k
                                        std::make_move_iterator(std::end(groupDescriptions)));
2610
250k
                }
2611
2612
26.9k
                for (auto childDescription: subparserDescription)
2613
0
                {
2614
0
                    std::get<2>(childDescription) += indent + addindent;
2615
0
                    descriptions.push_back(std::move(childDescription));
2616
0
                }
2617
2618
26.9k
                if (params.showCommandFullHelp && !Matched())
2619
0
                {
2620
0
                    descriptions.emplace_back("", "", indent + addindent);
2621
0
                    if (!Epilog().empty())
2622
0
                    {
2623
0
                        descriptions.emplace_back(Epilog(), "", indent + addindent);
2624
0
                        descriptions.emplace_back("", "", indent + addindent);
2625
0
                    }
2626
0
                }
2627
2628
26.9k
                return descriptions;
2629
26.9k
            }
2630
2631
            virtual void Validate(const std::string &shortprefix, const std::string &longprefix) const override
2632
11.5k
            {
2633
11.5k
                if (!Matched())
2634
1.31k
                {
2635
1.31k
                    return;
2636
1.31k
                }
2637
2638
10.2k
                auto onValidationError = [&]
2639
10.2k
                {
2640
1.57k
                    std::ostringstream problem;
2641
1.57k
                    problem << "Group validation failed somewhere!";
2642
#ifdef ARGS_NOEXCEPT
2643
                    error = Error::Validation;
2644
                    errorMsg = problem.str();
2645
#else
2646
1.57k
                    throw ValidationError(problem.str());
2647
1.57k
#endif
2648
1.57k
                };
2649
2650
10.2k
                for (Base *child: Children())
2651
104k
                {
2652
104k
                    if (child->IsGroup() && !child->Matched())
2653
1.57k
                    {
2654
1.57k
                        onValidationError();
2655
1.57k
                    }
2656
2657
104k
                    child->Validate(shortprefix, longprefix);
2658
104k
                }
2659
2660
10.2k
                if (subparser != nullptr)
2661
535
                {
2662
535
                    subparser->Validate(shortprefix, longprefix);
2663
535
                    if (!subparser->Matched())
2664
0
                    {
2665
0
                        onValidationError();
2666
0
                    }
2667
535
                }
2668
2669
10.2k
                if (selectedCommand == nullptr && commandIsRequired && (Group::HasCommand() || subparserHasCommand))
2670
73
                {
2671
73
                    std::ostringstream problem;
2672
73
                    problem << "Command is required";
2673
#ifdef ARGS_NOEXCEPT
2674
                    error = Error::Validation;
2675
                    errorMsg = problem.str();
2676
#else
2677
73
                    throw ValidationError(problem.str());
2678
73
#endif
2679
73
                }
2680
10.2k
            }
2681
2682
            virtual void Reset() noexcept override
2683
42.8k
            {
2684
42.8k
                Group::Reset();
2685
42.8k
                selectedCommand = nullptr;
2686
42.8k
                subparserProgramLine.clear();
2687
42.8k
                subparserDescription.clear();
2688
42.8k
                subparserHasFlag = false;
2689
42.8k
                subparserHasPositional = false;
2690
42.8k
                subparserHasCommand = false;
2691
#ifdef ARGS_NOEXCEPT
2692
                subparserError = Error::None;
2693
#endif
2694
42.8k
            }
2695
2696
#ifdef ARGS_NOEXCEPT
2697
            /// Only for ARGS_NOEXCEPT
2698
            virtual Error GetError() const override
2699
            {
2700
                if (!Matched())
2701
                {
2702
                    return Error::None;
2703
                }
2704
2705
                if (error != Error::None)
2706
                {
2707
                    return error;
2708
                }
2709
2710
                if (subparserError != Error::None)
2711
                {
2712
                    return subparserError;
2713
                }
2714
2715
                return Group::GetError();
2716
            }
2717
#endif
2718
    };
2719
2720
    /** The main user facing command line argument parser class
2721
     */
2722
    class ArgumentParser : public Command
2723
    {
2724
        friend class Subparser;
2725
2726
        private:
2727
            std::string longprefix;
2728
            std::string shortprefix;
2729
2730
            std::string longseparator;
2731
2732
            std::string terminator;
2733
2734
            bool allowJoinedShortValue = true;
2735
            bool allowJoinedLongValue = true;
2736
            bool allowSeparateShortValue = true;
2737
            bool allowSeparateLongValue = true;
2738
2739
            bool readCompletion = false;
2740
            CompletionFlag *completion = nullptr;
2741
2742
        protected:
2743
            enum class OptionType
2744
            {
2745
                LongFlag,
2746
                ShortFlag,
2747
                Positional
2748
            };
2749
2750
            OptionType ParseOption(const std::string &s, bool allowEmpty = false)
2751
52.2k
            {
2752
52.2k
                const bool matchesLong = s.find(longprefix) == 0 && (allowEmpty || s.length() > longprefix.length());
2753
52.2k
                const bool matchesShort = s.find(shortprefix) == 0 && (allowEmpty || s.length() > shortprefix.length());
2754
2755
                // A chunk can start with both prefixes when one is a prefix of
2756
                // the other, or when the long prefix is empty (every string
2757
                // starts with it). Resolve to the longer, more specific prefix:
2758
                // this keeps the default "--"/"-" preference for long flags
2759
                // while letting a short flag be recognised under an empty long
2760
                // prefix instead of being swallowed as a nameless long flag.
2761
52.2k
                if (matchesLong && matchesShort)
2762
10.4k
                {
2763
10.4k
                    return longprefix.length() >= shortprefix.length() ? OptionType::LongFlag : OptionType::ShortFlag;
2764
10.4k
                }
2765
2766
41.7k
                if (matchesLong)
2767
193
                {
2768
193
                    return OptionType::LongFlag;
2769
193
                }
2770
2771
41.5k
                if (matchesShort)
2772
10.3k
                {
2773
10.3k
                    return OptionType::ShortFlag;
2774
10.3k
                }
2775
2776
31.1k
                return OptionType::Positional;
2777
41.5k
            }
2778
2779
            template <typename It>
2780
            bool Complete(FlagBase &flag, It it, It end)
2781
13.0k
            {
2782
13.0k
                auto nextIt = it;
2783
13.0k
                if (!readCompletion || (++nextIt != end))
2784
13.0k
                {
2785
13.0k
                    return false;
2786
13.0k
                }
2787
2788
36
                const auto &chunk = *it;
2789
36
                for (auto &choice : flag.HelpChoices(helpParams))
2790
12
                {
2791
12
                    AddCompletionReply(chunk, choice);
2792
12
                }
2793
2794
36
#ifndef ARGS_NOEXCEPT
2795
36
                throw Completion(completion->Get());
2796
#else
2797
                return true;
2798
#endif
2799
13.0k
            }
bool args::ArgumentParser::Complete<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(args::FlagBase&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>)
Line
Count
Source
2781
12.8k
            {
2782
12.8k
                auto nextIt = it;
2783
12.8k
                if (!readCompletion || (++nextIt != end))
2784
12.8k
                {
2785
12.8k
                    return false;
2786
12.8k
                }
2787
2788
0
                const auto &chunk = *it;
2789
0
                for (auto &choice : flag.HelpChoices(helpParams))
2790
0
                {
2791
0
                    AddCompletionReply(chunk, choice);
2792
0
                }
2793
2794
0
#ifndef ARGS_NOEXCEPT
2795
0
                throw Completion(completion->Get());
2796
#else
2797
                return true;
2798
#endif
2799
12.8k
            }
bool args::ArgumentParser::Complete<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(args::FlagBase&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>)
Line
Count
Source
2781
275
            {
2782
275
                auto nextIt = it;
2783
275
                if (!readCompletion || (++nextIt != end))
2784
239
                {
2785
239
                    return false;
2786
239
                }
2787
2788
36
                const auto &chunk = *it;
2789
36
                for (auto &choice : flag.HelpChoices(helpParams))
2790
12
                {
2791
12
                    AddCompletionReply(chunk, choice);
2792
12
                }
2793
2794
36
#ifndef ARGS_NOEXCEPT
2795
36
                throw Completion(completion->Get());
2796
#else
2797
                return true;
2798
#endif
2799
275
            }
2800
2801
            /** (INTERNAL) Parse flag's values
2802
             *
2803
             * \param arg The string to display in error message as a flag name
2804
             * \param[in, out] it The iterator to first value. It will point to the last value
2805
             * \param end The end iterator
2806
             * \param joinedArg Joined value (e.g. bar in --foo=bar)
2807
             * \param canDiscardJoined If true joined value can be parsed as flag not as a value (as in -abcd)
2808
             * \param[out] values The vector to store parsed arg's values
2809
             */
2810
            template <typename It>
2811
            std::string ParseArgsValues(FlagBase &flag, const std::string &arg, It &it, It end,
2812
                                        const bool allowSeparate, const bool allowJoined,
2813
                                        const bool hasJoined, const std::string &joinedArg,
2814
                                        const bool canDiscardJoined, std::vector<std::string> &values)
2815
28.4k
            {
2816
28.4k
                values.clear();
2817
2818
28.4k
                Nargs nargs = flag.NumberOfArguments();
2819
2820
28.4k
                if (hasJoined && !allowJoined && (nargs.min != 0 || !canDiscardJoined))
2821
0
                {
2822
0
                    return "Flag '" + arg + "' was passed a joined argument, but these are disallowed";
2823
0
                }
2824
2825
28.4k
                if (hasJoined)
2826
18.4k
                {
2827
18.4k
                    if (!canDiscardJoined || (allowJoined && nargs.max != 0))
2828
2.20k
                    {
2829
2.20k
                        values.push_back(joinedArg);
2830
2.20k
                    }
2831
18.4k
                } else if (!allowSeparate)
2832
0
                {
2833
0
                    if (nargs.min != 0)
2834
0
                    {
2835
0
                        return "Flag '" + arg + "' was passed a separate argument, but these are disallowed";
2836
0
                    }
2837
10.0k
                } else if (allowSeparate && (!hasJoined || !values.empty())) {
2838
                    // Only gather separate values when they are allowed. A joined
2839
                    // value that was discarded rather than taken (short chunks such
2840
                    // as -nf when joined short values are off) means this flag isn't
2841
                    // taking an argument here, so the rest of the chunk is flags.
2842
10.0k
                    auto valueIt = it;
2843
10.0k
                    ++valueIt;
2844
2845
23.1k
                    while (valueIt != end &&
2846
22.6k
                           *valueIt != terminator &&
2847
22.5k
                           values.size() < nargs.max &&
2848
13.3k
                           (values.size() < nargs.min || ParseOption(*valueIt) == OptionType::Positional))
2849
13.0k
                    {
2850
13.0k
                        if (Complete(flag, valueIt, end))
2851
0
                        {
2852
                            // Park `it` on the completion position rather than
2853
                            // `end`. In ARGS_NOEXCEPT mode Complete returns
2854
                            // true (no throw), so the caller's for-loop will
2855
                            // run its ++it after we return; advancing an
2856
                            // already-end iterator is undefined behavior and
2857
                            // causes a subsequent out-of-bounds read of the
2858
                            // arg vector. Since Complete only fires when
2859
                            // ++nextIt == end, valueIt is the last element,
2860
                            // and ++(it=valueIt) safely lands on end.
2861
0
                            it = valueIt;
2862
0
                            return "";
2863
0
                        }
2864
2865
13.0k
                        values.push_back(*valueIt);
2866
13.0k
                        ++it;
2867
13.0k
                        ++valueIt;
2868
13.0k
                    }
2869
10.0k
                }
2870
2871
28.4k
                if (values.size() > nargs.max)
2872
30
                {
2873
30
                    return "Passed an argument into a non-argument flag: " + arg;
2874
28.4k
                } else if (values.size() < nargs.min)
2875
226
                {
2876
226
                    if (nargs.min == 1 && nargs.max == 1)
2877
208
                    {
2878
208
                        return "Flag '" + arg + "' requires an argument but received none";
2879
208
                    } else if (nargs.min == 1)
2880
0
                    {
2881
0
                        return "Flag '" + arg + "' requires at least one argument but received none";
2882
18
                    } else if (nargs.min != nargs.max)
2883
0
                    {
2884
0
                        return "Flag '" + arg + "' requires at least " + std::to_string(nargs.min) +
2885
0
                               " arguments but received " + std::to_string(values.size());
2886
0
                    } else
2887
18
                    {
2888
18
                        return "Flag '" + arg + "' requires " + std::to_string(nargs.min) +
2889
18
                               " arguments but received " + std::to_string(values.size());
2890
18
                    }
2891
226
                }
2892
2893
28.2k
                return {};
2894
28.4k
            }
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > args::ArgumentParser::ParseArgsValues<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(args::FlagBase&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >&)
Line
Count
Source
2815
15.8k
            {
2816
15.8k
                values.clear();
2817
2818
15.8k
                Nargs nargs = flag.NumberOfArguments();
2819
2820
15.8k
                if (hasJoined && !allowJoined && (nargs.min != 0 || !canDiscardJoined))
2821
0
                {
2822
0
                    return "Flag '" + arg + "' was passed a joined argument, but these are disallowed";
2823
0
                }
2824
2825
15.8k
                if (hasJoined)
2826
6.75k
                {
2827
6.75k
                    if (!canDiscardJoined || (allowJoined && nargs.max != 0))
2828
1.60k
                    {
2829
1.60k
                        values.push_back(joinedArg);
2830
1.60k
                    }
2831
9.14k
                } else if (!allowSeparate)
2832
0
                {
2833
0
                    if (nargs.min != 0)
2834
0
                    {
2835
0
                        return "Flag '" + arg + "' was passed a separate argument, but these are disallowed";
2836
0
                    }
2837
9.14k
                } else if (allowSeparate && (!hasJoined || !values.empty())) {
2838
                    // Only gather separate values when they are allowed. A joined
2839
                    // value that was discarded rather than taken (short chunks such
2840
                    // as -nf when joined short values are off) means this flag isn't
2841
                    // taking an argument here, so the rest of the chunk is flags.
2842
9.14k
                    auto valueIt = it;
2843
9.14k
                    ++valueIt;
2844
2845
21.9k
                    while (valueIt != end &&
2846
21.5k
                           *valueIt != terminator &&
2847
21.4k
                           values.size() < nargs.max &&
2848
13.0k
                           (values.size() < nargs.min || ParseOption(*valueIt) == OptionType::Positional))
2849
12.8k
                    {
2850
12.8k
                        if (Complete(flag, valueIt, end))
2851
0
                        {
2852
                            // Park `it` on the completion position rather than
2853
                            // `end`. In ARGS_NOEXCEPT mode Complete returns
2854
                            // true (no throw), so the caller's for-loop will
2855
                            // run its ++it after we return; advancing an
2856
                            // already-end iterator is undefined behavior and
2857
                            // causes a subsequent out-of-bounds read of the
2858
                            // arg vector. Since Complete only fires when
2859
                            // ++nextIt == end, valueIt is the last element,
2860
                            // and ++(it=valueIt) safely lands on end.
2861
0
                            it = valueIt;
2862
0
                            return "";
2863
0
                        }
2864
2865
12.8k
                        values.push_back(*valueIt);
2866
12.8k
                        ++it;
2867
12.8k
                        ++valueIt;
2868
12.8k
                    }
2869
9.14k
                }
2870
2871
15.8k
                if (values.size() > nargs.max)
2872
2
                {
2873
2
                    return "Passed an argument into a non-argument flag: " + arg;
2874
15.8k
                } else if (values.size() < nargs.min)
2875
192
                {
2876
192
                    if (nargs.min == 1 && nargs.max == 1)
2877
177
                    {
2878
177
                        return "Flag '" + arg + "' requires an argument but received none";
2879
177
                    } else if (nargs.min == 1)
2880
0
                    {
2881
0
                        return "Flag '" + arg + "' requires at least one argument but received none";
2882
15
                    } else if (nargs.min != nargs.max)
2883
0
                    {
2884
0
                        return "Flag '" + arg + "' requires at least " + std::to_string(nargs.min) +
2885
0
                               " arguments but received " + std::to_string(values.size());
2886
0
                    } else
2887
15
                    {
2888
15
                        return "Flag '" + arg + "' requires " + std::to_string(nargs.min) +
2889
15
                               " arguments but received " + std::to_string(values.size());
2890
15
                    }
2891
192
                }
2892
2893
15.6k
                return {};
2894
15.8k
            }
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > args::ArgumentParser::ParseArgsValues<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(args::FlagBase&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >&)
Line
Count
Source
2815
12.5k
            {
2816
12.5k
                values.clear();
2817
2818
12.5k
                Nargs nargs = flag.NumberOfArguments();
2819
2820
12.5k
                if (hasJoined && !allowJoined && (nargs.min != 0 || !canDiscardJoined))
2821
0
                {
2822
0
                    return "Flag '" + arg + "' was passed a joined argument, but these are disallowed";
2823
0
                }
2824
2825
12.5k
                if (hasJoined)
2826
11.6k
                {
2827
11.6k
                    if (!canDiscardJoined || (allowJoined && nargs.max != 0))
2828
606
                    {
2829
606
                        values.push_back(joinedArg);
2830
606
                    }
2831
11.6k
                } else if (!allowSeparate)
2832
0
                {
2833
0
                    if (nargs.min != 0)
2834
0
                    {
2835
0
                        return "Flag '" + arg + "' was passed a separate argument, but these are disallowed";
2836
0
                    }
2837
911
                } else if (allowSeparate && (!hasJoined || !values.empty())) {
2838
                    // Only gather separate values when they are allowed. A joined
2839
                    // value that was discarded rather than taken (short chunks such
2840
                    // as -nf when joined short values are off) means this flag isn't
2841
                    // taking an argument here, so the rest of the chunk is flags.
2842
911
                    auto valueIt = it;
2843
911
                    ++valueIt;
2844
2845
1.18k
                    while (valueIt != end &&
2846
1.07k
                           *valueIt != terminator &&
2847
1.05k
                           values.size() < nargs.max &&
2848
275
                           (values.size() < nargs.min || ParseOption(*valueIt) == OptionType::Positional))
2849
275
                    {
2850
275
                        if (Complete(flag, valueIt, end))
2851
0
                        {
2852
                            // Park `it` on the completion position rather than
2853
                            // `end`. In ARGS_NOEXCEPT mode Complete returns
2854
                            // true (no throw), so the caller's for-loop will
2855
                            // run its ++it after we return; advancing an
2856
                            // already-end iterator is undefined behavior and
2857
                            // causes a subsequent out-of-bounds read of the
2858
                            // arg vector. Since Complete only fires when
2859
                            // ++nextIt == end, valueIt is the last element,
2860
                            // and ++(it=valueIt) safely lands on end.
2861
0
                            it = valueIt;
2862
0
                            return "";
2863
0
                        }
2864
2865
275
                        values.push_back(*valueIt);
2866
275
                        ++it;
2867
275
                        ++valueIt;
2868
275
                    }
2869
911
                }
2870
2871
12.5k
                if (values.size() > nargs.max)
2872
28
                {
2873
28
                    return "Passed an argument into a non-argument flag: " + arg;
2874
12.5k
                } else if (values.size() < nargs.min)
2875
34
                {
2876
34
                    if (nargs.min == 1 && nargs.max == 1)
2877
31
                    {
2878
31
                        return "Flag '" + arg + "' requires an argument but received none";
2879
31
                    } else if (nargs.min == 1)
2880
0
                    {
2881
0
                        return "Flag '" + arg + "' requires at least one argument but received none";
2882
3
                    } else if (nargs.min != nargs.max)
2883
0
                    {
2884
0
                        return "Flag '" + arg + "' requires at least " + std::to_string(nargs.min) +
2885
0
                               " arguments but received " + std::to_string(values.size());
2886
0
                    } else
2887
3
                    {
2888
3
                        return "Flag '" + arg + "' requires " + std::to_string(nargs.min) +
2889
3
                               " arguments but received " + std::to_string(values.size());
2890
3
                    }
2891
34
                }
2892
2893
12.5k
                return {};
2894
12.5k
            }
2895
2896
            template <typename It>
2897
            bool ParseLong(It &it, It end)
2898
10.2k
            {
2899
10.2k
                const auto &chunk = *it;
2900
10.2k
                const auto argchunk = chunk.substr(longprefix.size());
2901
                // Try to separate it, in case of a separator:
2902
10.2k
                const auto separator = longseparator.empty() ? argchunk.npos : argchunk.find(longseparator);
2903
                // If the separator is in the argument, separate it.
2904
10.2k
                const auto arg = (separator != argchunk.npos ?
2905
1.72k
                    std::string(argchunk, 0, separator)
2906
10.2k
                    : argchunk);
2907
10.2k
                const auto joined = (separator != argchunk.npos ?
2908
1.72k
                    argchunk.substr(separator + longseparator.size())
2909
10.2k
                    : std::string());
2910
2911
10.2k
                if (auto flag = Match(arg))
2912
8.82k
                {
2913
#ifdef ARGS_NOEXCEPT
2914
                    // Match() may set the flag's error (e.g. Error::Extra when
2915
                    // Options::Single is violated). In non-noexcept mode that
2916
                    // path throws and parsing stops before the value is read;
2917
                    // in noexcept mode we must mirror that and skip the value
2918
                    // parsing so the previously-stored value is preserved.
2919
                    if (flag->GetError() != Error::None)
2920
                    {
2921
                        return false;
2922
                    }
2923
#endif
2924
8.82k
                    std::vector<std::string> values;
2925
8.82k
                    const std::string errorMessage = ParseArgsValues(*flag, arg, it, end, allowSeparateLongValue, allowJoinedLongValue,
2926
8.82k
                                                                     separator != argchunk.npos, joined, false, values);
2927
8.82k
                    if (!errorMessage.empty())
2928
83
                    {
2929
83
#ifndef ARGS_NOEXCEPT
2930
83
                        throw ParseError(errorMessage);
2931
#else
2932
                        error = Error::Parse;
2933
                        errorMsg = errorMessage;
2934
                        return false;
2935
#endif
2936
83
                    }
2937
2938
8.74k
                    if (!readCompletion)
2939
8.56k
                    {
2940
8.56k
                        flag->ParseValue(values);
2941
#ifdef ARGS_NOEXCEPT
2942
                        // Non-noexcept ParseValue paths throw on Help, reader
2943
                        // failure, or Map miss, which halts parsing. Mirror
2944
                        // that here so a later parser-level error (e.g. an
2945
                        // unknown flag) cannot shadow the flag's error in
2946
                        // ArgumentParser::GetError().
2947
                        if (flag->GetError() != Error::None)
2948
                        {
2949
                            return false;
2950
                        }
2951
#endif
2952
8.56k
                    }
2953
2954
8.74k
                    if (flag->KickOut())
2955
8
                    {
2956
8
                        ++it;
2957
8
                        return false;
2958
8
                    }
2959
8.74k
                } else
2960
1.40k
                {
2961
1.40k
                    const std::string errorMessage("Flag could not be matched: " + arg);
2962
1.40k
#ifndef ARGS_NOEXCEPT
2963
1.40k
                    throw ParseError(errorMessage);
2964
#else
2965
                    error = Error::Parse;
2966
                    errorMsg = errorMessage;
2967
                    return false;
2968
#endif
2969
1.40k
                }
2970
2971
8.73k
                return true;
2972
10.2k
            }
bool args::ArgumentParser::ParseLong<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>)
Line
Count
Source
2898
8.82k
            {
2899
8.82k
                const auto &chunk = *it;
2900
8.82k
                const auto argchunk = chunk.substr(longprefix.size());
2901
                // Try to separate it, in case of a separator:
2902
8.82k
                const auto separator = longseparator.empty() ? argchunk.npos : argchunk.find(longseparator);
2903
                // If the separator is in the argument, separate it.
2904
8.82k
                const auto arg = (separator != argchunk.npos ?
2905
1.04k
                    std::string(argchunk, 0, separator)
2906
8.82k
                    : argchunk);
2907
8.82k
                const auto joined = (separator != argchunk.npos ?
2908
1.04k
                    argchunk.substr(separator + longseparator.size())
2909
8.82k
                    : std::string());
2910
2911
8.82k
                if (auto flag = Match(arg))
2912
8.14k
                {
2913
#ifdef ARGS_NOEXCEPT
2914
                    // Match() may set the flag's error (e.g. Error::Extra when
2915
                    // Options::Single is violated). In non-noexcept mode that
2916
                    // path throws and parsing stops before the value is read;
2917
                    // in noexcept mode we must mirror that and skip the value
2918
                    // parsing so the previously-stored value is preserved.
2919
                    if (flag->GetError() != Error::None)
2920
                    {
2921
                        return false;
2922
                    }
2923
#endif
2924
8.14k
                    std::vector<std::string> values;
2925
8.14k
                    const std::string errorMessage = ParseArgsValues(*flag, arg, it, end, allowSeparateLongValue, allowJoinedLongValue,
2926
8.14k
                                                                     separator != argchunk.npos, joined, false, values);
2927
8.14k
                    if (!errorMessage.empty())
2928
36
                    {
2929
36
#ifndef ARGS_NOEXCEPT
2930
36
                        throw ParseError(errorMessage);
2931
#else
2932
                        error = Error::Parse;
2933
                        errorMsg = errorMessage;
2934
                        return false;
2935
#endif
2936
36
                    }
2937
2938
8.11k
                    if (!readCompletion)
2939
8.11k
                    {
2940
8.11k
                        flag->ParseValue(values);
2941
#ifdef ARGS_NOEXCEPT
2942
                        // Non-noexcept ParseValue paths throw on Help, reader
2943
                        // failure, or Map miss, which halts parsing. Mirror
2944
                        // that here so a later parser-level error (e.g. an
2945
                        // unknown flag) cannot shadow the flag's error in
2946
                        // ArgumentParser::GetError().
2947
                        if (flag->GetError() != Error::None)
2948
                        {
2949
                            return false;
2950
                        }
2951
#endif
2952
8.11k
                    }
2953
2954
8.11k
                    if (flag->KickOut())
2955
0
                    {
2956
0
                        ++it;
2957
0
                        return false;
2958
0
                    }
2959
8.11k
                } else
2960
673
                {
2961
673
                    const std::string errorMessage("Flag could not be matched: " + arg);
2962
673
#ifndef ARGS_NOEXCEPT
2963
673
                    throw ParseError(errorMessage);
2964
#else
2965
                    error = Error::Parse;
2966
                    errorMsg = errorMessage;
2967
                    return false;
2968
#endif
2969
673
                }
2970
2971
8.11k
                return true;
2972
8.82k
            }
bool args::ArgumentParser::ParseLong<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>)
Line
Count
Source
2898
1.41k
            {
2899
1.41k
                const auto &chunk = *it;
2900
1.41k
                const auto argchunk = chunk.substr(longprefix.size());
2901
                // Try to separate it, in case of a separator:
2902
1.41k
                const auto separator = longseparator.empty() ? argchunk.npos : argchunk.find(longseparator);
2903
                // If the separator is in the argument, separate it.
2904
1.41k
                const auto arg = (separator != argchunk.npos ?
2905
681
                    std::string(argchunk, 0, separator)
2906
1.41k
                    : argchunk);
2907
1.41k
                const auto joined = (separator != argchunk.npos ?
2908
681
                    argchunk.substr(separator + longseparator.size())
2909
1.41k
                    : std::string());
2910
2911
1.41k
                if (auto flag = Match(arg))
2912
678
                {
2913
#ifdef ARGS_NOEXCEPT
2914
                    // Match() may set the flag's error (e.g. Error::Extra when
2915
                    // Options::Single is violated). In non-noexcept mode that
2916
                    // path throws and parsing stops before the value is read;
2917
                    // in noexcept mode we must mirror that and skip the value
2918
                    // parsing so the previously-stored value is preserved.
2919
                    if (flag->GetError() != Error::None)
2920
                    {
2921
                        return false;
2922
                    }
2923
#endif
2924
678
                    std::vector<std::string> values;
2925
678
                    const std::string errorMessage = ParseArgsValues(*flag, arg, it, end, allowSeparateLongValue, allowJoinedLongValue,
2926
678
                                                                     separator != argchunk.npos, joined, false, values);
2927
678
                    if (!errorMessage.empty())
2928
47
                    {
2929
47
#ifndef ARGS_NOEXCEPT
2930
47
                        throw ParseError(errorMessage);
2931
#else
2932
                        error = Error::Parse;
2933
                        errorMsg = errorMessage;
2934
                        return false;
2935
#endif
2936
47
                    }
2937
2938
631
                    if (!readCompletion)
2939
454
                    {
2940
454
                        flag->ParseValue(values);
2941
#ifdef ARGS_NOEXCEPT
2942
                        // Non-noexcept ParseValue paths throw on Help, reader
2943
                        // failure, or Map miss, which halts parsing. Mirror
2944
                        // that here so a later parser-level error (e.g. an
2945
                        // unknown flag) cannot shadow the flag's error in
2946
                        // ArgumentParser::GetError().
2947
                        if (flag->GetError() != Error::None)
2948
                        {
2949
                            return false;
2950
                        }
2951
#endif
2952
454
                    }
2953
2954
631
                    if (flag->KickOut())
2955
8
                    {
2956
8
                        ++it;
2957
8
                        return false;
2958
8
                    }
2959
631
                } else
2960
733
                {
2961
733
                    const std::string errorMessage("Flag could not be matched: " + arg);
2962
733
#ifndef ARGS_NOEXCEPT
2963
733
                    throw ParseError(errorMessage);
2964
#else
2965
                    error = Error::Parse;
2966
                    errorMsg = errorMessage;
2967
                    return false;
2968
#endif
2969
733
                }
2970
2971
623
                return true;
2972
1.41k
            }
2973
2974
            template <typename It>
2975
            bool ParseShort(It &it, It end)
2976
4.98k
            {
2977
4.98k
                const auto &chunk = *it;
2978
4.98k
                const auto argchunk = chunk.substr(shortprefix.size());
2979
23.1k
                for (auto argit = std::begin(argchunk); argit != std::end(argchunk); ++argit)
2980
21.1k
                {
2981
21.1k
                    const auto arg = *argit;
2982
2983
21.1k
                    if (auto flag = Match(arg))
2984
19.6k
                    {
2985
#ifdef ARGS_NOEXCEPT
2986
                        // See ParseLong: if Match recorded an error
2987
                        // (e.g. Options::Single violation), bail before the
2988
                        // value is parsed so the prior value is preserved.
2989
                        if (flag->GetError() != Error::None)
2990
                        {
2991
                            return false;
2992
                        }
2993
#endif
2994
19.6k
                        const std::string value(argit + 1, std::end(argchunk));
2995
19.6k
                        std::vector<std::string> values;
2996
19.6k
                        const std::string errorMessage = ParseArgsValues(*flag, std::string(1, arg), it, end,
2997
19.6k
                                                                         allowSeparateShortValue, allowJoinedShortValue,
2998
19.6k
                                                                         !value.empty(), value, !value.empty(), values);
2999
3000
19.6k
                        if (!errorMessage.empty())
3001
173
                        {
3002
173
#ifndef ARGS_NOEXCEPT
3003
173
                            throw ParseError(errorMessage);
3004
#else
3005
                            error = Error::Parse;
3006
                            errorMsg = errorMessage;
3007
                            return false;
3008
#endif
3009
173
                        }
3010
3011
19.4k
                        if (!readCompletion)
3012
18.7k
                        {
3013
18.7k
                            flag->ParseValue(values);
3014
#ifdef ARGS_NOEXCEPT
3015
                            // See ParseLong: ensure a flag-level error from
3016
                            // ParseValue (Help, Parse, Map) halts parsing so
3017
                            // it cannot be shadowed by a later parser error.
3018
                            if (flag->GetError() != Error::None)
3019
                            {
3020
                                return false;
3021
                            }
3022
#endif
3023
18.7k
                        }
3024
3025
19.4k
                        if (flag->KickOut())
3026
16
                        {
3027
16
                            ++it;
3028
16
                            return false;
3029
16
                        }
3030
3031
19.4k
                        if (!values.empty())
3032
1.33k
                        {
3033
1.33k
                            break;
3034
1.33k
                        }
3035
19.4k
                    } else
3036
1.53k
                    {
3037
1.53k
                        const std::string errorMessage("Flag could not be matched: '" + std::string(1, arg) + "'");
3038
1.53k
#ifndef ARGS_NOEXCEPT
3039
1.53k
                        throw ParseError(errorMessage);
3040
#else
3041
                        error = Error::Parse;
3042
                        errorMsg = errorMessage;
3043
                        return false;
3044
#endif
3045
1.53k
                    }
3046
21.1k
                }
3047
3048
3.26k
                return true;
3049
4.98k
            }
bool args::ArgumentParser::ParseShort<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>)
Line
Count
Source
2976
3.73k
            {
2977
3.73k
                const auto &chunk = *it;
2978
3.73k
                const auto argchunk = chunk.substr(shortprefix.size());
2979
10.3k
                for (auto argit = std::begin(argchunk); argit != std::end(argchunk); ++argit)
2980
8.88k
                {
2981
8.88k
                    const auto arg = *argit;
2982
2983
8.88k
                    if (auto flag = Match(arg))
2984
7.74k
                    {
2985
#ifdef ARGS_NOEXCEPT
2986
                        // See ParseLong: if Match recorded an error
2987
                        // (e.g. Options::Single violation), bail before the
2988
                        // value is parsed so the prior value is preserved.
2989
                        if (flag->GetError() != Error::None)
2990
                        {
2991
                            return false;
2992
                        }
2993
#endif
2994
7.74k
                        const std::string value(argit + 1, std::end(argchunk));
2995
7.74k
                        std::vector<std::string> values;
2996
7.74k
                        const std::string errorMessage = ParseArgsValues(*flag, std::string(1, arg), it, end,
2997
7.74k
                                                                         allowSeparateShortValue, allowJoinedShortValue,
2998
7.74k
                                                                         !value.empty(), value, !value.empty(), values);
2999
3000
7.74k
                        if (!errorMessage.empty())
3001
158
                        {
3002
158
#ifndef ARGS_NOEXCEPT
3003
158
                            throw ParseError(errorMessage);
3004
#else
3005
                            error = Error::Parse;
3006
                            errorMsg = errorMessage;
3007
                            return false;
3008
#endif
3009
158
                        }
3010
3011
7.58k
                        if (!readCompletion)
3012
7.58k
                        {
3013
7.58k
                            flag->ParseValue(values);
3014
#ifdef ARGS_NOEXCEPT
3015
                            // See ParseLong: ensure a flag-level error from
3016
                            // ParseValue (Help, Parse, Map) halts parsing so
3017
                            // it cannot be shadowed by a later parser error.
3018
                            if (flag->GetError() != Error::None)
3019
                            {
3020
                                return false;
3021
                            }
3022
#endif
3023
7.58k
                        }
3024
3025
7.58k
                        if (flag->KickOut())
3026
0
                        {
3027
0
                            ++it;
3028
0
                            return false;
3029
0
                        }
3030
3031
7.58k
                        if (!values.empty())
3032
1.00k
                        {
3033
1.00k
                            break;
3034
1.00k
                        }
3035
7.58k
                    } else
3036
1.14k
                    {
3037
1.14k
                        const std::string errorMessage("Flag could not be matched: '" + std::string(1, arg) + "'");
3038
1.14k
#ifndef ARGS_NOEXCEPT
3039
1.14k
                        throw ParseError(errorMessage);
3040
#else
3041
                        error = Error::Parse;
3042
                        errorMsg = errorMessage;
3043
                        return false;
3044
#endif
3045
1.14k
                    }
3046
8.88k
                }
3047
3048
2.43k
                return true;
3049
3.73k
            }
bool args::ArgumentParser::ParseShort<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>&, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>)
Line
Count
Source
2976
1.25k
            {
2977
1.25k
                const auto &chunk = *it;
2978
1.25k
                const auto argchunk = chunk.substr(shortprefix.size());
2979
12.7k
                for (auto argit = std::begin(argchunk); argit != std::end(argchunk); ++argit)
2980
12.3k
                {
2981
12.3k
                    const auto arg = *argit;
2982
2983
12.3k
                    if (auto flag = Match(arg))
2984
11.9k
                    {
2985
#ifdef ARGS_NOEXCEPT
2986
                        // See ParseLong: if Match recorded an error
2987
                        // (e.g. Options::Single violation), bail before the
2988
                        // value is parsed so the prior value is preserved.
2989
                        if (flag->GetError() != Error::None)
2990
                        {
2991
                            return false;
2992
                        }
2993
#endif
2994
11.9k
                        const std::string value(argit + 1, std::end(argchunk));
2995
11.9k
                        std::vector<std::string> values;
2996
11.9k
                        const std::string errorMessage = ParseArgsValues(*flag, std::string(1, arg), it, end,
2997
11.9k
                                                                         allowSeparateShortValue, allowJoinedShortValue,
2998
11.9k
                                                                         !value.empty(), value, !value.empty(), values);
2999
3000
11.9k
                        if (!errorMessage.empty())
3001
15
                        {
3002
15
#ifndef ARGS_NOEXCEPT
3003
15
                            throw ParseError(errorMessage);
3004
#else
3005
                            error = Error::Parse;
3006
                            errorMsg = errorMessage;
3007
                            return false;
3008
#endif
3009
15
                        }
3010
3011
11.8k
                        if (!readCompletion)
3012
11.1k
                        {
3013
11.1k
                            flag->ParseValue(values);
3014
#ifdef ARGS_NOEXCEPT
3015
                            // See ParseLong: ensure a flag-level error from
3016
                            // ParseValue (Help, Parse, Map) halts parsing so
3017
                            // it cannot be shadowed by a later parser error.
3018
                            if (flag->GetError() != Error::None)
3019
                            {
3020
                                return false;
3021
                            }
3022
#endif
3023
11.1k
                        }
3024
3025
11.8k
                        if (flag->KickOut())
3026
16
                        {
3027
16
                            ++it;
3028
16
                            return false;
3029
16
                        }
3030
3031
11.8k
                        if (!values.empty())
3032
338
                        {
3033
338
                            break;
3034
338
                        }
3035
11.8k
                    } else
3036
389
                    {
3037
389
                        const std::string errorMessage("Flag could not be matched: '" + std::string(1, arg) + "'");
3038
389
#ifndef ARGS_NOEXCEPT
3039
389
                        throw ParseError(errorMessage);
3040
#else
3041
                        error = Error::Parse;
3042
                        errorMsg = errorMessage;
3043
                        return false;
3044
#endif
3045
389
                    }
3046
12.3k
                }
3047
3048
830
                return true;
3049
1.25k
            }
3050
3051
            bool AddCompletionReply(const std::string &cur, const std::string &choice)
3052
5.64k
            {
3053
5.64k
                if (cur.empty() || choice.find(cur) == 0)
3054
478
                {
3055
478
                    if (completion->syntax == "bash" && ParseOption(choice) == OptionType::LongFlag && choice.find(longseparator) != std::string::npos)
3056
90
                    {
3057
90
                        completion->reply.push_back(choice.substr(choice.find(longseparator) + longseparator.size()));
3058
90
                    } else
3059
388
                    {
3060
388
                        completion->reply.push_back(choice);
3061
388
                    }
3062
478
                    return true;
3063
478
                }
3064
3065
5.16k
                return false;
3066
5.64k
            }
3067
3068
            template <typename It>
3069
            bool Complete(It it, It end, bool terminated)
3070
32.8k
            {
3071
32.8k
                auto nextIt = it;
3072
32.8k
                if (!readCompletion || (++nextIt != end))
3073
32.1k
                {
3074
32.1k
                    return false;
3075
32.1k
                }
3076
3077
721
                const auto &chunk = *it;
3078
721
                auto pos = GetNextPositional();
3079
721
                std::vector<Command *> commands = GetCommands();
3080
721
                const auto optionType = ParseOption(chunk, true);
3081
3082
                // Once the terminator has been seen the parser treats every
3083
                // following chunk as positional, so only positional choices are
3084
                // valid completions here. Suggesting flags or commands past the
3085
                // terminator offers candidates the parser would then reject.
3086
721
                if (!terminated && !commands.empty() && (chunk.empty() || optionType == OptionType::Positional))
3087
258
                {
3088
258
                    for (auto &cmd : commands)
3089
516
                    {
3090
516
                        if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3091
516
                        {
3092
516
                            AddCompletionReply(chunk, cmd->Name());
3093
516
                        }
3094
516
                    }
3095
258
                } else
3096
463
                {
3097
463
                    bool hasPositionalCompletion = true;
3098
3099
463
                    if (!terminated && !commands.empty())
3100
348
                    {
3101
348
                        for (auto &cmd : commands)
3102
696
                        {
3103
696
                            if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3104
696
                            {
3105
696
                                AddCompletionReply(chunk, cmd->Name());
3106
696
                            }
3107
696
                        }
3108
348
                    } else if (pos)
3109
35
                    {
3110
35
                        if ((pos->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3111
35
                        {
3112
35
                            auto choices = pos->HelpChoices(helpParams);
3113
35
                            hasPositionalCompletion = !choices.empty() || optionType != OptionType::Positional;
3114
35
                            for (auto &choice : choices)
3115
54
                            {
3116
54
                                AddCompletionReply(chunk, choice);
3117
54
                            }
3118
35
                        }
3119
35
                    }
3120
3121
463
                    if (!terminated && hasPositionalCompletion)
3122
420
                    {
3123
420
                        auto flags = GetAllFlags();
3124
420
                        for (auto flag : flags)
3125
1.80k
                        {
3126
1.80k
                            if ((flag->GetOptions() & Options::HiddenFromCompletion) != Options::None)
3127
348
                            {
3128
348
                                continue;
3129
348
                            }
3130
3131
1.45k
                            auto &matcher = flag->GetMatcher();
3132
1.45k
                            if (!AddCompletionReply(chunk, matcher.GetShortOrAny().str(shortprefix, longprefix)))
3133
1.34k
                            {
3134
1.34k
                                for (auto &flagName : matcher.GetFlagStrings())
3135
2.68k
                                {
3136
2.68k
                                    if (AddCompletionReply(chunk, flagName.str(shortprefix, longprefix)))
3137
116
                                    {
3138
116
                                        break;
3139
116
                                    }
3140
2.68k
                                }
3141
1.34k
                            }
3142
1.45k
                        }
3143
3144
420
                        if (optionType == OptionType::LongFlag && allowJoinedLongValue)
3145
260
                        {
3146
260
                            const auto separator = longseparator.empty() ? chunk.npos : chunk.find(longseparator);
3147
                            // Only attempt joined-value completion when the
3148
                            // separator lies at or past the long prefix, so
3149
                            // there is a (possibly empty) flag name between
3150
                            // them. With a custom longseparator that overlaps
3151
                            // the prefix (e.g. LongSeparator("-") under the
3152
                            // default "--" prefix), an attacker-controlled
3153
                            // completion word like "--x" puts the separator
3154
                            // inside the prefix, making `arg` shorter than
3155
                            // longprefix. arg.substr(longprefix.size()) would
3156
                            // then throw std::out_of_range, which escapes the
3157
                            // parser as a non-args exception (bypassing the
3158
                            // documented catch(args::Error) idiom) and is
3159
                            // thrown even under ARGS_NOEXCEPT.
3160
260
                            if (separator != chunk.npos && separator >= longprefix.size())
3161
153
                            {
3162
153
                                std::string arg(chunk, 0, separator);
3163
153
                                if (auto flag = this->Match(arg.substr(longprefix.size())))
3164
101
                                {
3165
101
                                    for (auto &choice : flag->HelpChoices(helpParams))
3166
184
                                    {
3167
184
                                        AddCompletionReply(chunk, arg + longseparator + choice);
3168
184
                                    }
3169
101
                                }
3170
153
                            }
3171
260
                        } else if (optionType == OptionType::ShortFlag && allowJoinedShortValue)
3172
122
                        {
3173
122
                            if (chunk.size() > shortprefix.size() + 1)
3174
76
                            {
3175
76
                                auto arg = chunk.at(shortprefix.size());
3176
                                //TODO: support -abcVALUE where a and b take no value
3177
76
                                if (auto flag = this->Match(arg))
3178
41
                                {
3179
41
                                    for (auto &choice : flag->HelpChoices(helpParams))
3180
44
                                    {
3181
44
                                        AddCompletionReply(chunk, shortprefix + arg + choice);
3182
44
                                    }
3183
41
                                }
3184
76
                            }
3185
122
                        }
3186
420
                    }
3187
463
                }
3188
3189
721
#ifndef ARGS_NOEXCEPT
3190
721
                throw Completion(completion->Get());
3191
#else
3192
                return true;
3193
#endif
3194
32.8k
            }
bool args::ArgumentParser::Complete<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, bool)
Line
Count
Source
3070
27.0k
            {
3071
27.0k
                auto nextIt = it;
3072
27.0k
                if (!readCompletion || (++nextIt != end))
3073
27.0k
                {
3074
27.0k
                    return false;
3075
27.0k
                }
3076
3077
0
                const auto &chunk = *it;
3078
0
                auto pos = GetNextPositional();
3079
0
                std::vector<Command *> commands = GetCommands();
3080
0
                const auto optionType = ParseOption(chunk, true);
3081
3082
                // Once the terminator has been seen the parser treats every
3083
                // following chunk as positional, so only positional choices are
3084
                // valid completions here. Suggesting flags or commands past the
3085
                // terminator offers candidates the parser would then reject.
3086
0
                if (!terminated && !commands.empty() && (chunk.empty() || optionType == OptionType::Positional))
3087
0
                {
3088
0
                    for (auto &cmd : commands)
3089
0
                    {
3090
0
                        if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3091
0
                        {
3092
0
                            AddCompletionReply(chunk, cmd->Name());
3093
0
                        }
3094
0
                    }
3095
0
                } else
3096
0
                {
3097
0
                    bool hasPositionalCompletion = true;
3098
3099
0
                    if (!terminated && !commands.empty())
3100
0
                    {
3101
0
                        for (auto &cmd : commands)
3102
0
                        {
3103
0
                            if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3104
0
                            {
3105
0
                                AddCompletionReply(chunk, cmd->Name());
3106
0
                            }
3107
0
                        }
3108
0
                    } else if (pos)
3109
0
                    {
3110
0
                        if ((pos->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3111
0
                        {
3112
0
                            auto choices = pos->HelpChoices(helpParams);
3113
0
                            hasPositionalCompletion = !choices.empty() || optionType != OptionType::Positional;
3114
0
                            for (auto &choice : choices)
3115
0
                            {
3116
0
                                AddCompletionReply(chunk, choice);
3117
0
                            }
3118
0
                        }
3119
0
                    }
3120
3121
0
                    if (!terminated && hasPositionalCompletion)
3122
0
                    {
3123
0
                        auto flags = GetAllFlags();
3124
0
                        for (auto flag : flags)
3125
0
                        {
3126
0
                            if ((flag->GetOptions() & Options::HiddenFromCompletion) != Options::None)
3127
0
                            {
3128
0
                                continue;
3129
0
                            }
3130
3131
0
                            auto &matcher = flag->GetMatcher();
3132
0
                            if (!AddCompletionReply(chunk, matcher.GetShortOrAny().str(shortprefix, longprefix)))
3133
0
                            {
3134
0
                                for (auto &flagName : matcher.GetFlagStrings())
3135
0
                                {
3136
0
                                    if (AddCompletionReply(chunk, flagName.str(shortprefix, longprefix)))
3137
0
                                    {
3138
0
                                        break;
3139
0
                                    }
3140
0
                                }
3141
0
                            }
3142
0
                        }
3143
3144
0
                        if (optionType == OptionType::LongFlag && allowJoinedLongValue)
3145
0
                        {
3146
0
                            const auto separator = longseparator.empty() ? chunk.npos : chunk.find(longseparator);
3147
                            // Only attempt joined-value completion when the
3148
                            // separator lies at or past the long prefix, so
3149
                            // there is a (possibly empty) flag name between
3150
                            // them. With a custom longseparator that overlaps
3151
                            // the prefix (e.g. LongSeparator("-") under the
3152
                            // default "--" prefix), an attacker-controlled
3153
                            // completion word like "--x" puts the separator
3154
                            // inside the prefix, making `arg` shorter than
3155
                            // longprefix. arg.substr(longprefix.size()) would
3156
                            // then throw std::out_of_range, which escapes the
3157
                            // parser as a non-args exception (bypassing the
3158
                            // documented catch(args::Error) idiom) and is
3159
                            // thrown even under ARGS_NOEXCEPT.
3160
0
                            if (separator != chunk.npos && separator >= longprefix.size())
3161
0
                            {
3162
0
                                std::string arg(chunk, 0, separator);
3163
0
                                if (auto flag = this->Match(arg.substr(longprefix.size())))
3164
0
                                {
3165
0
                                    for (auto &choice : flag->HelpChoices(helpParams))
3166
0
                                    {
3167
0
                                        AddCompletionReply(chunk, arg + longseparator + choice);
3168
0
                                    }
3169
0
                                }
3170
0
                            }
3171
0
                        } else if (optionType == OptionType::ShortFlag && allowJoinedShortValue)
3172
0
                        {
3173
0
                            if (chunk.size() > shortprefix.size() + 1)
3174
0
                            {
3175
0
                                auto arg = chunk.at(shortprefix.size());
3176
                                //TODO: support -abcVALUE where a and b take no value
3177
0
                                if (auto flag = this->Match(arg))
3178
0
                                {
3179
0
                                    for (auto &choice : flag->HelpChoices(helpParams))
3180
0
                                    {
3181
0
                                        AddCompletionReply(chunk, shortprefix + arg + choice);
3182
0
                                    }
3183
0
                                }
3184
0
                            }
3185
0
                        }
3186
0
                    }
3187
0
                }
3188
3189
0
#ifndef ARGS_NOEXCEPT
3190
0
                throw Completion(completion->Get());
3191
#else
3192
                return true;
3193
#endif
3194
27.0k
            }
bool args::ArgumentParser::Complete<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, bool)
Line
Count
Source
3070
5.81k
            {
3071
5.81k
                auto nextIt = it;
3072
5.81k
                if (!readCompletion || (++nextIt != end))
3073
5.09k
                {
3074
5.09k
                    return false;
3075
5.09k
                }
3076
3077
721
                const auto &chunk = *it;
3078
721
                auto pos = GetNextPositional();
3079
721
                std::vector<Command *> commands = GetCommands();
3080
721
                const auto optionType = ParseOption(chunk, true);
3081
3082
                // Once the terminator has been seen the parser treats every
3083
                // following chunk as positional, so only positional choices are
3084
                // valid completions here. Suggesting flags or commands past the
3085
                // terminator offers candidates the parser would then reject.
3086
721
                if (!terminated && !commands.empty() && (chunk.empty() || optionType == OptionType::Positional))
3087
258
                {
3088
258
                    for (auto &cmd : commands)
3089
516
                    {
3090
516
                        if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3091
516
                        {
3092
516
                            AddCompletionReply(chunk, cmd->Name());
3093
516
                        }
3094
516
                    }
3095
258
                } else
3096
463
                {
3097
463
                    bool hasPositionalCompletion = true;
3098
3099
463
                    if (!terminated && !commands.empty())
3100
348
                    {
3101
348
                        for (auto &cmd : commands)
3102
696
                        {
3103
696
                            if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3104
696
                            {
3105
696
                                AddCompletionReply(chunk, cmd->Name());
3106
696
                            }
3107
696
                        }
3108
348
                    } else if (pos)
3109
35
                    {
3110
35
                        if ((pos->GetOptions() & Options::HiddenFromCompletion) == Options::None)
3111
35
                        {
3112
35
                            auto choices = pos->HelpChoices(helpParams);
3113
35
                            hasPositionalCompletion = !choices.empty() || optionType != OptionType::Positional;
3114
35
                            for (auto &choice : choices)
3115
54
                            {
3116
54
                                AddCompletionReply(chunk, choice);
3117
54
                            }
3118
35
                        }
3119
35
                    }
3120
3121
463
                    if (!terminated && hasPositionalCompletion)
3122
420
                    {
3123
420
                        auto flags = GetAllFlags();
3124
420
                        for (auto flag : flags)
3125
1.80k
                        {
3126
1.80k
                            if ((flag->GetOptions() & Options::HiddenFromCompletion) != Options::None)
3127
348
                            {
3128
348
                                continue;
3129
348
                            }
3130
3131
1.45k
                            auto &matcher = flag->GetMatcher();
3132
1.45k
                            if (!AddCompletionReply(chunk, matcher.GetShortOrAny().str(shortprefix, longprefix)))
3133
1.34k
                            {
3134
1.34k
                                for (auto &flagName : matcher.GetFlagStrings())
3135
2.68k
                                {
3136
2.68k
                                    if (AddCompletionReply(chunk, flagName.str(shortprefix, longprefix)))
3137
116
                                    {
3138
116
                                        break;
3139
116
                                    }
3140
2.68k
                                }
3141
1.34k
                            }
3142
1.45k
                        }
3143
3144
420
                        if (optionType == OptionType::LongFlag && allowJoinedLongValue)
3145
260
                        {
3146
260
                            const auto separator = longseparator.empty() ? chunk.npos : chunk.find(longseparator);
3147
                            // Only attempt joined-value completion when the
3148
                            // separator lies at or past the long prefix, so
3149
                            // there is a (possibly empty) flag name between
3150
                            // them. With a custom longseparator that overlaps
3151
                            // the prefix (e.g. LongSeparator("-") under the
3152
                            // default "--" prefix), an attacker-controlled
3153
                            // completion word like "--x" puts the separator
3154
                            // inside the prefix, making `arg` shorter than
3155
                            // longprefix. arg.substr(longprefix.size()) would
3156
                            // then throw std::out_of_range, which escapes the
3157
                            // parser as a non-args exception (bypassing the
3158
                            // documented catch(args::Error) idiom) and is
3159
                            // thrown even under ARGS_NOEXCEPT.
3160
260
                            if (separator != chunk.npos && separator >= longprefix.size())
3161
153
                            {
3162
153
                                std::string arg(chunk, 0, separator);
3163
153
                                if (auto flag = this->Match(arg.substr(longprefix.size())))
3164
101
                                {
3165
101
                                    for (auto &choice : flag->HelpChoices(helpParams))
3166
184
                                    {
3167
184
                                        AddCompletionReply(chunk, arg + longseparator + choice);
3168
184
                                    }
3169
101
                                }
3170
153
                            }
3171
260
                        } else if (optionType == OptionType::ShortFlag && allowJoinedShortValue)
3172
122
                        {
3173
122
                            if (chunk.size() > shortprefix.size() + 1)
3174
76
                            {
3175
76
                                auto arg = chunk.at(shortprefix.size());
3176
                                //TODO: support -abcVALUE where a and b take no value
3177
76
                                if (auto flag = this->Match(arg))
3178
41
                                {
3179
41
                                    for (auto &choice : flag->HelpChoices(helpParams))
3180
44
                                    {
3181
44
                                        AddCompletionReply(chunk, shortprefix + arg + choice);
3182
44
                                    }
3183
41
                                }
3184
76
                            }
3185
122
                        }
3186
420
                    }
3187
463
                }
3188
3189
721
#ifndef ARGS_NOEXCEPT
3190
721
                throw Completion(completion->Get());
3191
#else
3192
                return true;
3193
#endif
3194
5.81k
            }
3195
3196
            template <typename It>
3197
            It Parse(It begin, It end)
3198
24.0k
            {
3199
24.0k
                bool terminated = false;
3200
24.0k
                std::vector<Command *> commands = GetCommands();
3201
3202
                // Check all arg chunks
3203
50.0k
                for (auto it = begin; it != end; ++it)
3204
32.8k
                {
3205
32.8k
                    if (Complete(it, end, terminated))
3206
0
                    {
3207
0
                        return end;
3208
0
                    }
3209
3210
32.8k
                    const auto &chunk = *it;
3211
3212
32.8k
                    if (!terminated && chunk == terminator)
3213
487
                    {
3214
487
                        terminated = true;
3215
32.3k
                    } else if (!terminated && ParseOption(chunk) == OptionType::LongFlag)
3216
10.2k
                    {
3217
10.2k
                        if (!ParseLong(it, end))
3218
8
                        {
3219
8
                            return it;
3220
8
                        }
3221
22.1k
                    } else if (!terminated && ParseOption(chunk) == OptionType::ShortFlag)
3222
4.98k
                    {
3223
4.98k
                        if (!ParseShort(it, end))
3224
16
                        {
3225
16
                            return it;
3226
16
                        }
3227
17.1k
                    } else if (!terminated && !commands.empty())
3228
3.74k
                    {
3229
10.0k
                        auto itCommand = std::find_if(commands.begin(), commands.end(), [&chunk](Command *c) { return c->Name() == chunk; });
args::ArgumentParser::Parse<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>)::{lambda(args::Command*)#1}::operator()(args::Command*) const
Line
Count
Source
3229
8.90k
                        auto itCommand = std::find_if(commands.begin(), commands.end(), [&chunk](Command *c) { return c->Name() == chunk; });
args::ArgumentParser::Parse<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>)::{lambda(args::Command*)#1}::operator()(args::Command*) const
Line
Count
Source
3229
1.10k
                        auto itCommand = std::find_if(commands.begin(), commands.end(), [&chunk](Command *c) { return c->Name() == chunk; });
3230
3.74k
                        if (itCommand == commands.end())
3231
3.19k
                        {
3232
3.19k
                            const std::string errorMessage("Unknown command: " + chunk);
3233
3.19k
#ifndef ARGS_NOEXCEPT
3234
3.19k
                            throw ParseError(errorMessage);
3235
#else
3236
                            error = Error::Parse;
3237
                            errorMsg = errorMessage;
3238
                            return it;
3239
#endif
3240
3.19k
                        }
3241
3242
548
                        SelectCommand(*itCommand);
3243
3244
548
                        if (const auto &coroutine = GetCoroutine())
3245
548
                        {
3246
548
                            ++it;
3247
548
                            RaiiSubparser coro(*this, std::vector<std::string>(it, end));
3248
548
                            coroutine(coro.Parser());
3249
#ifdef ARGS_NOEXCEPT
3250
                            error = GetError();
3251
                            if (error != Error::None)
3252
                            {
3253
                                return end;
3254
                            }
3255
3256
                            if (!coro.Parser().IsParsed())
3257
                            {
3258
                                error = Error::Usage;
3259
                                return end;
3260
                            }
3261
#else
3262
548
                            if (!coro.Parser().IsParsed())
3263
0
                            {
3264
0
                                throw UsageError("Subparser::Parse was not called");
3265
0
                            }
3266
548
#endif
3267
3268
548
                            break;
3269
548
                        }
3270
3271
0
                        commands = GetCommands();
3272
0
                    } else
3273
13.4k
                    {
3274
13.4k
                        auto pos = GetNextPositional();
3275
13.4k
                        if (pos)
3276
12.4k
                        {
3277
12.4k
                            pos->ParseValue(chunk);
3278
#ifdef ARGS_NOEXCEPT
3279
                            if (pos->GetError() != Error::None)
3280
                            {
3281
                                return it;
3282
                            }
3283
#endif
3284
3285
12.4k
                            if (pos->KickOut())
3286
0
                            {
3287
0
                                return ++it;
3288
0
                            }
3289
12.4k
                        } else
3290
993
                        {
3291
993
                            const std::string errorMessage("Passed in argument, but no positional arguments were ready to receive it: " + chunk);
3292
993
#ifndef ARGS_NOEXCEPT
3293
993
                            throw ParseError(errorMessage);
3294
#else
3295
                            error = Error::Parse;
3296
                            errorMsg = errorMessage;
3297
                            return it;
3298
#endif
3299
993
                        }
3300
13.4k
                    }
3301
3302
28.1k
                    if (!readCompletion && completion != nullptr && completion->Matched())
3303
2.09k
                    {
3304
#ifdef ARGS_NOEXCEPT
3305
                        if (completion->GetError() != Error::None)
3306
                        {
3307
                            error = completion->GetError();
3308
                            if (errorMsg.empty())
3309
                            {
3310
                                errorMsg = completion->GetErrorMsg();
3311
                            }
3312
                            return it;
3313
                        }
3314
3315
                        error = Error::Completion;
3316
#endif
3317
2.09k
                        readCompletion = true;
3318
2.09k
                        ++it;
3319
2.09k
                        const auto argsLeft = static_cast<size_t>(std::distance(it, end));
3320
2.09k
                        if (completion->cword == 0 || argsLeft <= 1 || completion->cword >= argsLeft)
3321
358
                        {
3322
358
#ifndef ARGS_NOEXCEPT
3323
358
                            throw Completion("");
3324
#else
3325
                            return end;
3326
#endif
3327
358
                        }
3328
3329
1.73k
                        ++it;
3330
1.73k
                        std::vector<std::string> curArgs;
3331
1.73k
                        curArgs.reserve(completion->cword);
3332
1.73k
                        auto curIt = it;
3333
6.09k
                        for (size_t idx = 0; idx < completion->cword && curIt != end; ++idx, ++curIt)
3334
4.35k
                        {
3335
4.35k
                            curArgs.push_back(*curIt);
3336
4.35k
                        }
3337
3338
1.73k
                        if (completion->syntax == "bash")
3339
1.61k
                        {
3340
                            // bash tokenizes --flag=value as --flag=value
3341
                            // Security fix: Use size_t arithmetic throughout to avoid conversion issues
3342
5.51k
                            for (size_t idx = 0; idx < curArgs.size(); )
3343
3.90k
                            {
3344
3.90k
                                if (idx > 0 && curArgs[idx] == "=")
3345
252
                                {
3346
252
                                    size_t prev_idx = idx - 1;  // Safe since we checked idx > 0
3347
252
                                    curArgs[prev_idx] += "=";
3348
252
                                    size_t next_idx = 0;
3349
252
                                    if (SafeAdd<size_t>(idx, static_cast<size_t>(1), next_idx) && next_idx < curArgs.size())
3350
239
                                    {
3351
239
                                        curArgs[prev_idx] += curArgs[next_idx];
3352
                                        // Erase the '=' token and the following value token.
3353
239
                                        size_t erase_end = 0;
3354
239
                                        if (SafeAdd<size_t>(next_idx, static_cast<size_t>(1), erase_end))
3355
239
                                        {
3356
239
                                            typedef std::vector<std::string>::difference_type diff_t;
3357
239
                                            curArgs.erase(curArgs.begin() + static_cast<diff_t>(idx),
3358
239
                                                         curArgs.begin() + static_cast<diff_t>(erase_end));
3359
239
                                        }
3360
239
                                    } else
3361
13
                                    {
3362
                                        // Safe erase of single '=' token at the end
3363
13
                                        typedef std::vector<std::string>::difference_type diff_t;
3364
13
                                        curArgs.erase(curArgs.begin() + static_cast<diff_t>(idx));
3365
13
                                    }
3366
                                    // Do not increment idx - next element slides into current position
3367
252
                                } else
3368
3.64k
                                {
3369
3.64k
                                    ++idx;
3370
3.64k
                                }
3371
3.90k
                            }
3372
3373
1.61k
                        }
3374
1.73k
#ifndef ARGS_NOEXCEPT
3375
1.73k
                        try
3376
1.73k
                        {
3377
1.73k
                            Parse(curArgs.begin(), curArgs.end());
3378
1.73k
                            throw Completion("");
3379
1.73k
                        }
3380
1.73k
                        catch (Completion &)
3381
1.73k
                        {
3382
757
                            throw;
3383
757
                        }
3384
1.73k
                        catch (args::Error&)
3385
1.73k
                        {
3386
978
                            throw Completion("");
3387
978
                        }
3388
#else
3389
                        // Discard the nested Parse's return value: it points
3390
                        // into the local curArgs vector, which is destroyed
3391
                        // when this function returns, leaving the caller with
3392
                        // a dangling iterator that would be compared against
3393
                        // the outer `end` in ParseCLI. Return the outer
3394
                        // `end` instead so the iterator stays in the caller's
3395
                        // container.
3396
                        Parse(curArgs.begin(), curArgs.end());
3397
                        error = Error::Completion;
3398
                        errorMsg.clear();
3399
                        return end;
3400
#endif
3401
1.73k
                    }
3402
28.1k
                }
3403
3404
17.7k
                Validate(shortprefix, longprefix);
3405
17.7k
                return end;
3406
24.0k
            }
std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> args::ArgumentParser::Parse<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>)
Line
Count
Source
3198
20.7k
            {
3199
20.7k
                bool terminated = false;
3200
20.7k
                std::vector<Command *> commands = GetCommands();
3201
3202
                // Check all arg chunks
3203
42.6k
                for (auto it = begin; it != end; ++it)
3204
27.0k
                {
3205
27.0k
                    if (Complete(it, end, terminated))
3206
0
                    {
3207
0
                        return end;
3208
0
                    }
3209
3210
27.0k
                    const auto &chunk = *it;
3211
3212
27.0k
                    if (!terminated && chunk == terminator)
3213
332
                    {
3214
332
                        terminated = true;
3215
26.7k
                    } else if (!terminated && ParseOption(chunk) == OptionType::LongFlag)
3216
8.82k
                    {
3217
8.82k
                        if (!ParseLong(it, end))
3218
0
                        {
3219
0
                            return it;
3220
0
                        }
3221
17.9k
                    } else if (!terminated && ParseOption(chunk) == OptionType::ShortFlag)
3222
3.73k
                    {
3223
3.73k
                        if (!ParseShort(it, end))
3224
0
                        {
3225
0
                            return it;
3226
0
                        }
3227
14.1k
                    } else if (!terminated && !commands.empty())
3228
3.14k
                    {
3229
3.14k
                        auto itCommand = std::find_if(commands.begin(), commands.end(), [&chunk](Command *c) { return c->Name() == chunk; });
3230
3.14k
                        if (itCommand == commands.end())
3231
2.70k
                        {
3232
2.70k
                            const std::string errorMessage("Unknown command: " + chunk);
3233
2.70k
#ifndef ARGS_NOEXCEPT
3234
2.70k
                            throw ParseError(errorMessage);
3235
#else
3236
                            error = Error::Parse;
3237
                            errorMsg = errorMessage;
3238
                            return it;
3239
#endif
3240
2.70k
                        }
3241
3242
440
                        SelectCommand(*itCommand);
3243
3244
440
                        if (const auto &coroutine = GetCoroutine())
3245
440
                        {
3246
440
                            ++it;
3247
440
                            RaiiSubparser coro(*this, std::vector<std::string>(it, end));
3248
440
                            coroutine(coro.Parser());
3249
#ifdef ARGS_NOEXCEPT
3250
                            error = GetError();
3251
                            if (error != Error::None)
3252
                            {
3253
                                return end;
3254
                            }
3255
3256
                            if (!coro.Parser().IsParsed())
3257
                            {
3258
                                error = Error::Usage;
3259
                                return end;
3260
                            }
3261
#else
3262
440
                            if (!coro.Parser().IsParsed())
3263
0
                            {
3264
0
                                throw UsageError("Subparser::Parse was not called");
3265
0
                            }
3266
440
#endif
3267
3268
440
                            break;
3269
440
                        }
3270
3271
0
                        commands = GetCommands();
3272
0
                    } else
3273
11.0k
                    {
3274
11.0k
                        auto pos = GetNextPositional();
3275
11.0k
                        if (pos)
3276
11.0k
                        {
3277
11.0k
                            pos->ParseValue(chunk);
3278
#ifdef ARGS_NOEXCEPT
3279
                            if (pos->GetError() != Error::None)
3280
                            {
3281
                                return it;
3282
                            }
3283
#endif
3284
3285
11.0k
                            if (pos->KickOut())
3286
0
                            {
3287
0
                                return ++it;
3288
0
                            }
3289
11.0k
                        } else
3290
0
                        {
3291
0
                            const std::string errorMessage("Passed in argument, but no positional arguments were ready to receive it: " + chunk);
3292
0
#ifndef ARGS_NOEXCEPT
3293
0
                            throw ParseError(errorMessage);
3294
#else
3295
                            error = Error::Parse;
3296
                            errorMsg = errorMessage;
3297
                            return it;
3298
#endif
3299
0
                        }
3300
11.0k
                    }
3301
3302
23.9k
                    if (!readCompletion && completion != nullptr && completion->Matched())
3303
2.09k
                    {
3304
#ifdef ARGS_NOEXCEPT
3305
                        if (completion->GetError() != Error::None)
3306
                        {
3307
                            error = completion->GetError();
3308
                            if (errorMsg.empty())
3309
                            {
3310
                                errorMsg = completion->GetErrorMsg();
3311
                            }
3312
                            return it;
3313
                        }
3314
3315
                        error = Error::Completion;
3316
#endif
3317
2.09k
                        readCompletion = true;
3318
2.09k
                        ++it;
3319
2.09k
                        const auto argsLeft = static_cast<size_t>(std::distance(it, end));
3320
2.09k
                        if (completion->cword == 0 || argsLeft <= 1 || completion->cword >= argsLeft)
3321
358
                        {
3322
358
#ifndef ARGS_NOEXCEPT
3323
358
                            throw Completion("");
3324
#else
3325
                            return end;
3326
#endif
3327
358
                        }
3328
3329
1.73k
                        ++it;
3330
1.73k
                        std::vector<std::string> curArgs;
3331
1.73k
                        curArgs.reserve(completion->cword);
3332
1.73k
                        auto curIt = it;
3333
6.09k
                        for (size_t idx = 0; idx < completion->cword && curIt != end; ++idx, ++curIt)
3334
4.35k
                        {
3335
4.35k
                            curArgs.push_back(*curIt);
3336
4.35k
                        }
3337
3338
1.73k
                        if (completion->syntax == "bash")
3339
1.61k
                        {
3340
                            // bash tokenizes --flag=value as --flag=value
3341
                            // Security fix: Use size_t arithmetic throughout to avoid conversion issues
3342
5.51k
                            for (size_t idx = 0; idx < curArgs.size(); )
3343
3.90k
                            {
3344
3.90k
                                if (idx > 0 && curArgs[idx] == "=")
3345
252
                                {
3346
252
                                    size_t prev_idx = idx - 1;  // Safe since we checked idx > 0
3347
252
                                    curArgs[prev_idx] += "=";
3348
252
                                    size_t next_idx = 0;
3349
252
                                    if (SafeAdd<size_t>(idx, static_cast<size_t>(1), next_idx) && next_idx < curArgs.size())
3350
239
                                    {
3351
239
                                        curArgs[prev_idx] += curArgs[next_idx];
3352
                                        // Erase the '=' token and the following value token.
3353
239
                                        size_t erase_end = 0;
3354
239
                                        if (SafeAdd<size_t>(next_idx, static_cast<size_t>(1), erase_end))
3355
239
                                        {
3356
239
                                            typedef std::vector<std::string>::difference_type diff_t;
3357
239
                                            curArgs.erase(curArgs.begin() + static_cast<diff_t>(idx),
3358
239
                                                         curArgs.begin() + static_cast<diff_t>(erase_end));
3359
239
                                        }
3360
239
                                    } else
3361
13
                                    {
3362
                                        // Safe erase of single '=' token at the end
3363
13
                                        typedef std::vector<std::string>::difference_type diff_t;
3364
13
                                        curArgs.erase(curArgs.begin() + static_cast<diff_t>(idx));
3365
13
                                    }
3366
                                    // Do not increment idx - next element slides into current position
3367
252
                                } else
3368
3.64k
                                {
3369
3.64k
                                    ++idx;
3370
3.64k
                                }
3371
3.90k
                            }
3372
3373
1.61k
                        }
3374
1.73k
#ifndef ARGS_NOEXCEPT
3375
1.73k
                        try
3376
1.73k
                        {
3377
1.73k
                            Parse(curArgs.begin(), curArgs.end());
3378
1.73k
                            throw Completion("");
3379
1.73k
                        }
3380
1.73k
                        catch (Completion &)
3381
1.73k
                        {
3382
757
                            throw;
3383
757
                        }
3384
1.73k
                        catch (args::Error&)
3385
1.73k
                        {
3386
978
                            throw Completion("");
3387
978
                        }
3388
#else
3389
                        // Discard the nested Parse's return value: it points
3390
                        // into the local curArgs vector, which is destroyed
3391
                        // when this function returns, leaving the caller with
3392
                        // a dangling iterator that would be compared against
3393
                        // the outer `end` in ParseCLI. Return the outer
3394
                        // `end` instead so the iterator stays in the caller's
3395
                        // container.
3396
                        Parse(curArgs.begin(), curArgs.end());
3397
                        error = Error::Completion;
3398
                        errorMsg.clear();
3399
                        return end;
3400
#endif
3401
1.73k
                    }
3402
23.9k
                }
3403
3404
15.9k
                Validate(shortprefix, longprefix);
3405
15.9k
                return end;
3406
20.7k
            }
std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> args::ArgumentParser::Parse<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>)
Line
Count
Source
3198
3.25k
            {
3199
3.25k
                bool terminated = false;
3200
3.25k
                std::vector<Command *> commands = GetCommands();
3201
3202
                // Check all arg chunks
3203
7.45k
                for (auto it = begin; it != end; ++it)
3204
5.81k
                {
3205
5.81k
                    if (Complete(it, end, terminated))
3206
0
                    {
3207
0
                        return end;
3208
0
                    }
3209
3210
5.81k
                    const auto &chunk = *it;
3211
3212
5.81k
                    if (!terminated && chunk == terminator)
3213
155
                    {
3214
155
                        terminated = true;
3215
5.65k
                    } else if (!terminated && ParseOption(chunk) == OptionType::LongFlag)
3216
1.41k
                    {
3217
1.41k
                        if (!ParseLong(it, end))
3218
8
                        {
3219
8
                            return it;
3220
8
                        }
3221
4.24k
                    } else if (!terminated && ParseOption(chunk) == OptionType::ShortFlag)
3222
1.25k
                    {
3223
1.25k
                        if (!ParseShort(it, end))
3224
16
                        {
3225
16
                            return it;
3226
16
                        }
3227
2.99k
                    } else if (!terminated && !commands.empty())
3228
594
                    {
3229
594
                        auto itCommand = std::find_if(commands.begin(), commands.end(), [&chunk](Command *c) { return c->Name() == chunk; });
3230
594
                        if (itCommand == commands.end())
3231
486
                        {
3232
486
                            const std::string errorMessage("Unknown command: " + chunk);
3233
486
#ifndef ARGS_NOEXCEPT
3234
486
                            throw ParseError(errorMessage);
3235
#else
3236
                            error = Error::Parse;
3237
                            errorMsg = errorMessage;
3238
                            return it;
3239
#endif
3240
486
                        }
3241
3242
108
                        SelectCommand(*itCommand);
3243
3244
108
                        if (const auto &coroutine = GetCoroutine())
3245
108
                        {
3246
108
                            ++it;
3247
108
                            RaiiSubparser coro(*this, std::vector<std::string>(it, end));
3248
108
                            coroutine(coro.Parser());
3249
#ifdef ARGS_NOEXCEPT
3250
                            error = GetError();
3251
                            if (error != Error::None)
3252
                            {
3253
                                return end;
3254
                            }
3255
3256
                            if (!coro.Parser().IsParsed())
3257
                            {
3258
                                error = Error::Usage;
3259
                                return end;
3260
                            }
3261
#else
3262
108
                            if (!coro.Parser().IsParsed())
3263
0
                            {
3264
0
                                throw UsageError("Subparser::Parse was not called");
3265
0
                            }
3266
108
#endif
3267
3268
108
                            break;
3269
108
                        }
3270
3271
0
                        commands = GetCommands();
3272
0
                    } else
3273
2.40k
                    {
3274
2.40k
                        auto pos = GetNextPositional();
3275
2.40k
                        if (pos)
3276
1.41k
                        {
3277
1.41k
                            pos->ParseValue(chunk);
3278
#ifdef ARGS_NOEXCEPT
3279
                            if (pos->GetError() != Error::None)
3280
                            {
3281
                                return it;
3282
                            }
3283
#endif
3284
3285
1.41k
                            if (pos->KickOut())
3286
0
                            {
3287
0
                                return ++it;
3288
0
                            }
3289
1.41k
                        } else
3290
993
                        {
3291
993
                            const std::string errorMessage("Passed in argument, but no positional arguments were ready to receive it: " + chunk);
3292
993
#ifndef ARGS_NOEXCEPT
3293
993
                            throw ParseError(errorMessage);
3294
#else
3295
                            error = Error::Parse;
3296
                            errorMsg = errorMessage;
3297
                            return it;
3298
#endif
3299
993
                        }
3300
2.40k
                    }
3301
3302
4.20k
                    if (!readCompletion && completion != nullptr && completion->Matched())
3303
0
                    {
3304
#ifdef ARGS_NOEXCEPT
3305
                        if (completion->GetError() != Error::None)
3306
                        {
3307
                            error = completion->GetError();
3308
                            if (errorMsg.empty())
3309
                            {
3310
                                errorMsg = completion->GetErrorMsg();
3311
                            }
3312
                            return it;
3313
                        }
3314
3315
                        error = Error::Completion;
3316
#endif
3317
0
                        readCompletion = true;
3318
0
                        ++it;
3319
0
                        const auto argsLeft = static_cast<size_t>(std::distance(it, end));
3320
0
                        if (completion->cword == 0 || argsLeft <= 1 || completion->cword >= argsLeft)
3321
0
                        {
3322
0
#ifndef ARGS_NOEXCEPT
3323
0
                            throw Completion("");
3324
#else
3325
                            return end;
3326
#endif
3327
0
                        }
3328
3329
0
                        ++it;
3330
0
                        std::vector<std::string> curArgs;
3331
0
                        curArgs.reserve(completion->cword);
3332
0
                        auto curIt = it;
3333
0
                        for (size_t idx = 0; idx < completion->cword && curIt != end; ++idx, ++curIt)
3334
0
                        {
3335
0
                            curArgs.push_back(*curIt);
3336
0
                        }
3337
3338
0
                        if (completion->syntax == "bash")
3339
0
                        {
3340
                            // bash tokenizes --flag=value as --flag=value
3341
                            // Security fix: Use size_t arithmetic throughout to avoid conversion issues
3342
0
                            for (size_t idx = 0; idx < curArgs.size(); )
3343
0
                            {
3344
0
                                if (idx > 0 && curArgs[idx] == "=")
3345
0
                                {
3346
0
                                    size_t prev_idx = idx - 1;  // Safe since we checked idx > 0
3347
0
                                    curArgs[prev_idx] += "=";
3348
0
                                    size_t next_idx = 0;
3349
0
                                    if (SafeAdd<size_t>(idx, static_cast<size_t>(1), next_idx) && next_idx < curArgs.size())
3350
0
                                    {
3351
0
                                        curArgs[prev_idx] += curArgs[next_idx];
3352
                                        // Erase the '=' token and the following value token.
3353
0
                                        size_t erase_end = 0;
3354
0
                                        if (SafeAdd<size_t>(next_idx, static_cast<size_t>(1), erase_end))
3355
0
                                        {
3356
0
                                            typedef std::vector<std::string>::difference_type diff_t;
3357
0
                                            curArgs.erase(curArgs.begin() + static_cast<diff_t>(idx),
3358
0
                                                         curArgs.begin() + static_cast<diff_t>(erase_end));
3359
0
                                        }
3360
0
                                    } else
3361
0
                                    {
3362
                                        // Safe erase of single '=' token at the end
3363
0
                                        typedef std::vector<std::string>::difference_type diff_t;
3364
0
                                        curArgs.erase(curArgs.begin() + static_cast<diff_t>(idx));
3365
0
                                    }
3366
                                    // Do not increment idx - next element slides into current position
3367
0
                                } else
3368
0
                                {
3369
0
                                    ++idx;
3370
0
                                }
3371
0
                            }
3372
3373
0
                        }
3374
0
#ifndef ARGS_NOEXCEPT
3375
0
                        try
3376
0
                        {
3377
0
                            Parse(curArgs.begin(), curArgs.end());
3378
0
                            throw Completion("");
3379
0
                        }
3380
0
                        catch (Completion &)
3381
0
                        {
3382
0
                            throw;
3383
0
                        }
3384
0
                        catch (args::Error&)
3385
0
                        {
3386
0
                            throw Completion("");
3387
0
                        }
3388
#else
3389
                        // Discard the nested Parse's return value: it points
3390
                        // into the local curArgs vector, which is destroyed
3391
                        // when this function returns, leaving the caller with
3392
                        // a dangling iterator that would be compared against
3393
                        // the outer `end` in ParseCLI. Return the outer
3394
                        // `end` instead so the iterator stays in the caller's
3395
                        // container.
3396
                        Parse(curArgs.begin(), curArgs.end());
3397
                        error = Error::Completion;
3398
                        errorMsg.clear();
3399
                        return end;
3400
#endif
3401
0
                    }
3402
4.20k
                }
3403
3404
1.75k
                Validate(shortprefix, longprefix);
3405
1.75k
                return end;
3406
3.25k
            }
3407
3408
        public:
3409
            HelpParams helpParams;
3410
3411
            ArgumentParser(const std::string &description_, const std::string &epilog_ = std::string())
3412
13.8k
            {
3413
13.8k
                Description(description_);
3414
13.8k
                Epilog(epilog_);
3415
13.8k
                LongPrefix("--");
3416
13.8k
                ShortPrefix("-");
3417
13.8k
                LongSeparator("=");
3418
13.8k
                Terminator("--");
3419
13.8k
                SetArgumentSeparations(true, true, true, true);
3420
13.8k
                matched = true;
3421
13.8k
            }
3422
3423
            void AddCompletion(CompletionFlag &completionFlag)
3424
2.49k
            {
3425
                // Only take the pointer once registration has succeeded: Add()
3426
                // throws on a duplicate flag, and the flag it was handed is
3427
                // gone by the time that error reaches the caller.
3428
2.49k
                Add(completionFlag);
3429
2.49k
                completion = &completionFlag;
3430
2.49k
            }
3431
3432
            /** The program name for help generation
3433
             */
3434
            const std::string &Prog() const
3435
20.1k
            { return helpParams.programName; }
3436
            /** The program name for help generation
3437
             */
3438
            void Prog(const std::string &prog_)
3439
0
            { this->helpParams.programName = prog_; }
3440
3441
            /** The prefix for long flags
3442
             */
3443
            const std::string &LongPrefix() const
3444
261
            { return longprefix; }
3445
            /** The prefix for long flags
3446
             */
3447
            void LongPrefix(const std::string &longprefix_)
3448
20.5k
            {
3449
20.5k
                this->longprefix = longprefix_;
3450
20.5k
                this->helpParams.longPrefix = longprefix_;
3451
20.5k
            }
3452
3453
            /** The prefix for short flags
3454
             */
3455
            const std::string &ShortPrefix() const
3456
261
            { return shortprefix; }
3457
            /** The prefix for short flags
3458
             */
3459
            void ShortPrefix(const std::string &shortprefix_)
3460
20.5k
            {
3461
20.5k
                this->shortprefix = shortprefix_;
3462
20.5k
                this->helpParams.shortPrefix = shortprefix_;
3463
20.5k
            }
3464
3465
            /** The separator for long flags
3466
             */
3467
            const std::string &LongSeparator() const
3468
0
            { return longseparator; }
3469
            /** The separator for long flags
3470
             */
3471
            void LongSeparator(const std::string &longseparator_)
3472
23.0k
            {
3473
23.0k
                if (longseparator_.empty())
3474
0
                {
3475
0
                    const std::string errorMessage("longseparator can not be set to empty");
3476
#ifdef ARGS_NOEXCEPT
3477
                    error = Error::Usage;
3478
                    errorMsg = errorMessage;
3479
#else
3480
0
                    throw UsageError(errorMessage);
3481
0
#endif
3482
0
                } else
3483
23.0k
                {
3484
23.0k
                    this->longseparator = longseparator_;
3485
23.0k
                    this->helpParams.longSeparator = allowJoinedLongValue ? longseparator : " ";
3486
23.0k
                }
3487
23.0k
            }
3488
3489
            /** The terminator that forcibly separates flags from positionals
3490
             */
3491
            const std::string &Terminator() const
3492
0
            { return terminator; }
3493
            /** The terminator that forcibly separates flags from positionals
3494
             */
3495
            void Terminator(const std::string &terminator_)
3496
20.5k
            { this->terminator = terminator_; }
3497
3498
            /** Get the current argument separation parameters.
3499
             *
3500
             * See SetArgumentSeparations for details on what each one means.
3501
             */
3502
            void GetArgumentSeparations(
3503
                bool &allowJoinedShortValue_,
3504
                bool &allowJoinedLongValue_,
3505
                bool &allowSeparateShortValue_,
3506
                bool &allowSeparateLongValue_) const
3507
0
            {
3508
0
                allowJoinedShortValue_ = this->allowJoinedShortValue;
3509
0
                allowJoinedLongValue_ = this->allowJoinedLongValue;
3510
0
                allowSeparateShortValue_ = this->allowSeparateShortValue;
3511
0
                allowSeparateLongValue_ = this->allowSeparateLongValue;
3512
0
            }
3513
3514
            /** Change allowed option separation.
3515
             *
3516
             * \param allowJoinedShortValue_ Allow a short flag that accepts an argument to be passed its argument immediately next to it (ie. in the same argv field)
3517
             * \param allowJoinedLongValue_ Allow a long flag that accepts an argument to be passed its argument separated by the longseparator (ie. in the same argv field)
3518
             * \param allowSeparateShortValue_ Allow a short flag that accepts an argument to be passed its argument separated by whitespace (ie. in the next argv field)
3519
             * \param allowSeparateLongValue_ Allow a long flag that accepts an argument to be passed its argument separated by whitespace (ie. in the next argv field)
3520
             */
3521
            void SetArgumentSeparations(
3522
                const bool allowJoinedShortValue_,
3523
                const bool allowJoinedLongValue_,
3524
                const bool allowSeparateShortValue_,
3525
                const bool allowSeparateLongValue_)
3526
13.8k
            {
3527
13.8k
                this->allowJoinedShortValue = allowJoinedShortValue_;
3528
13.8k
                this->allowJoinedLongValue = allowJoinedLongValue_;
3529
13.8k
                this->allowSeparateShortValue = allowSeparateShortValue_;
3530
13.8k
                this->allowSeparateLongValue = allowSeparateLongValue_;
3531
3532
13.8k
                this->helpParams.longSeparator = allowJoinedLongValue ? longseparator : " ";
3533
13.8k
                this->helpParams.shortSeparator = allowJoinedShortValue ? "" : " ";
3534
13.8k
            }
3535
3536
            /** Pass the help menu into an ostream
3537
             */
3538
            void Help(std::ostream &help_) const
3539
0
            {
3540
0
                auto &command = SelectedCommand();
3541
0
                const auto &commandDescription = command.Description().empty() ? command.Help() : command.Description();
3542
0
                const auto desc_indent = helpParams.descriptionindent;
3543
0
                const auto effective_desc_width = (helpParams.width > desc_indent) ? helpParams.width - desc_indent : 0;
3544
0
                const auto description_text = Wrap(commandDescription, effective_desc_width);
3545
0
                const auto epilog_text = Wrap(command.Epilog(), effective_desc_width);
3546
0
3547
0
                const bool hasoptions = command.HasFlag();
3548
0
                const bool hasarguments = command.HasPositional();
3549
0
3550
0
                std::vector<std::string> prognameline;
3551
0
                prognameline.push_back(helpParams.usageString);
3552
0
                prognameline.push_back(Prog());
3553
0
                auto commandProgLine = command.GetProgramLine(helpParams);
3554
0
                prognameline.insert(prognameline.end(), commandProgLine.begin(), commandProgLine.end());
3555
0
3556
0
                const auto prog_sum = helpParams.progindent + helpParams.progtailindent;
3557
0
                const auto effective_prog_width = (helpParams.width > prog_sum) ? helpParams.width - prog_sum : 0;
3558
0
                const auto effective_prog_first = (helpParams.width > helpParams.progindent) ? helpParams.width - helpParams.progindent : 0;
3559
0
                const auto proglines = Wrap(prognameline.begin(), prognameline.end(),
3560
0
                                            effective_prog_width,
3561
0
                                            effective_prog_first);
3562
0
                auto progit = std::begin(proglines);
3563
0
                if (progit != std::end(proglines))
3564
0
                {
3565
0
                    help_ << std::string(helpParams.progindent, ' ') << *progit << '\n';
3566
0
                    ++progit;
3567
0
                }
3568
0
                for (; progit != std::end(proglines); ++progit)
3569
0
                {
3570
0
                    help_ << std::string(helpParams.progtailindent, ' ') << *progit << '\n';
3571
0
                }
3572
0
3573
0
                help_ << '\n';
3574
0
3575
0
                if (!description_text.empty())
3576
0
                {
3577
0
                    for (const auto &line: description_text)
3578
0
                    {
3579
0
                        help_ << std::string(helpParams.descriptionindent, ' ') << line << "\n";
3580
0
                    }
3581
0
                    help_ << "\n";
3582
0
                }
3583
0
3584
0
                bool lastDescriptionIsNewline = false;
3585
0
3586
0
                if (!helpParams.optionsString.empty())
3587
0
                {
3588
0
                    help_ << std::string(helpParams.progindent, ' ') << helpParams.optionsString << "\n\n";
3589
0
                }
3590
0
3591
0
                for (const auto &desc: command.GetDescription(helpParams, 0))
3592
0
                {
3593
0
                    lastDescriptionIsNewline = std::get<0>(desc).empty() && std::get<1>(desc).empty();
3594
0
                    const auto groupindent = std::get<2>(desc) * helpParams.eachgroupindent;
3595
0
                    const auto flag_sum = helpParams.flagindent + helpParams.helpindent + helpParams.gutter;
3596
0
                    const auto effective_flag_width = (helpParams.width > flag_sum) ? helpParams.width - flag_sum : 0;
3597
0
                    const auto flags = Wrap(std::get<0>(desc), effective_flag_width);
3598
0
                    const auto info_sum = helpParams.helpindent + groupindent;
3599
0
                    const auto effective_info_width = (helpParams.width > info_sum) ? helpParams.width - info_sum : 0;
3600
0
                    const auto info = Wrap(std::get<1>(desc), effective_info_width);
3601
0
3602
0
                    std::string::size_type flagssize = 0;
3603
0
                    for (auto flagsit = std::begin(flags); flagsit != std::end(flags); ++flagsit)
3604
0
                    {
3605
0
                        if (flagsit != std::begin(flags))
3606
0
                        {
3607
0
                            help_ << '\n';
3608
0
                        }
3609
0
                        help_ << std::string(groupindent + helpParams.flagindent, ' ') << *flagsit;
3610
0
                        flagssize = Glyphs(*flagsit);
3611
0
                    }
3612
0
3613
0
                    auto infoit = std::begin(info);
3614
0
                    // groupindent is on both sides of this inequality, and therefore can be removed
3615
0
                    if ((helpParams.flagindent + flagssize + helpParams.gutter) > helpParams.helpindent || infoit == std::end(info) || helpParams.addNewlineBeforeDescription)
3616
0
                    {
3617
0
                        help_ << '\n';
3618
0
                    } else
3619
0
                    {
3620
0
                        // groupindent is on both sides of the minus sign, and therefore doesn't actually need to be in here
3621
0
                        const auto indent_sum = helpParams.flagindent + flagssize;
3622
0
                        const auto effective_space = (helpParams.helpindent > indent_sum) ? helpParams.helpindent - indent_sum : 0;
3623
0
                        help_ << std::string(effective_space, ' ') << *infoit << '\n';
3624
0
                        ++infoit;
3625
0
                    }
3626
0
                    for (; infoit != std::end(info); ++infoit)
3627
0
                    {
3628
0
                        help_ << std::string(groupindent + helpParams.helpindent, ' ') << *infoit << '\n';
3629
0
                    }
3630
0
                }
3631
0
                if (hasoptions && hasarguments && helpParams.showTerminator)
3632
0
                {
3633
0
                    lastDescriptionIsNewline = false;
3634
0
                    const auto effective_term_width = (helpParams.width > helpParams.flagindent) ? helpParams.width - helpParams.flagindent : 0;
3635
0
                    for (const auto &item: Wrap(std::string("\"") + terminator + "\" can be used to terminate flag options and force all following arguments to be treated as positional options", effective_term_width))
3636
0
                    {
3637
0
                        help_ << std::string(helpParams.flagindent, ' ') << item << '\n';
3638
0
                    }
3639
0
                }
3640
0
3641
0
                if (!lastDescriptionIsNewline)
3642
0
                {
3643
0
                    help_ << "\n";
3644
0
                }
3645
0
3646
0
                for (const auto &line: epilog_text)
3647
0
                {
3648
0
                    help_ << std::string(helpParams.descriptionindent, ' ') << line << "\n";
3649
0
                }
3650
0
            }
3651
3652
            /** Generate a help menu as a string.
3653
             *
3654
             * \return the help text as a single string
3655
             */
3656
            std::string Help() const
3657
13.4k
            {
3658
13.4k
                std::ostringstream help_;
3659
13.4k
                Help(help_);
3660
13.4k
                return help_.str();
3661
13.4k
            }
3662
3663
            virtual void Reset() noexcept override
3664
21.7k
            {
3665
21.7k
                Command::Reset();
3666
21.7k
                matched = true;
3667
21.7k
                readCompletion = false;
3668
21.7k
            }
3669
3670
            /** Parse all arguments.
3671
             *
3672
             * \param begin an iterator to the beginning of the argument list
3673
             * \param end an iterator to the past-the-end element of the argument list
3674
             * \return the iterator after the last parsed value.  Only useful for kick-out
3675
             */
3676
            template <typename It>
3677
            It ParseArgs(It begin, It end)
3678
21.7k
            {
3679
                // Reset all Matched statuses and errors
3680
21.7k
                Reset();
3681
#ifdef ARGS_NOEXCEPT
3682
                error = GetError();
3683
                if (error != Error::None)
3684
                {
3685
                    return end;
3686
                }
3687
#endif
3688
21.7k
                return Parse(begin, end);
3689
21.7k
            }
std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> args::ArgumentParser::ParseArgs<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>)
Line
Count
Source
3678
20.7k
            {
3679
                // Reset all Matched statuses and errors
3680
20.7k
                Reset();
3681
#ifdef ARGS_NOEXCEPT
3682
                error = GetError();
3683
                if (error != Error::None)
3684
                {
3685
                    return end;
3686
                }
3687
#endif
3688
20.7k
                return Parse(begin, end);
3689
20.7k
            }
std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> args::ArgumentParser::ParseArgs<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*>)
Line
Count
Source
3678
972
            {
3679
                // Reset all Matched statuses and errors
3680
972
                Reset();
3681
#ifdef ARGS_NOEXCEPT
3682
                error = GetError();
3683
                if (error != Error::None)
3684
                {
3685
                    return end;
3686
                }
3687
#endif
3688
972
                return Parse(begin, end);
3689
972
            }
3690
3691
            /** Parse all arguments.
3692
             *
3693
             * \param args an iterable of the arguments
3694
             * \return the iterator after the last parsed value.  Only useful for kick-out
3695
             */
3696
            template <typename T>
3697
            auto ParseArgs(const T &args) -> decltype(std::begin(args))
3698
20.7k
            {
3699
20.7k
                return ParseArgs(std::begin(args), std::end(args));
3700
20.7k
            }
3701
3702
            /** Convenience function to parse the CLI from argc and argv
3703
             *
3704
             * Just assigns the program name and vectorizes arguments for passing into ParseArgs()
3705
             *
3706
             * \return whether or not all arguments were parsed.  This works for detecting kick-out, but is generally useless as it can't do anything with it.
3707
             */
3708
            bool ParseCLI(const int argc, const char * const * argv)
3709
0
            {
3710
0
                if (argc > 0 && argv != nullptr && argv[0] != nullptr && Prog().empty())
3711
0
                {
3712
0
                    Prog(argv[0]);
3713
0
                }
3714
0
3715
0
                std::vector<std::string> args;
3716
0
                if (argc > 1 && argv != nullptr)
3717
0
                {
3718
0
                    args.assign(argv + 1, argv + argc);
3719
0
                }
3720
0
3721
0
                return ParseArgs(args) == std::end(args);
3722
0
            }
3723
            
3724
            template <typename T>
3725
            bool ParseCLI(const T &args)
3726
            {
3727
                return ParseArgs(args) == std::end(args);
3728
            }
3729
    };
3730
3731
    inline Command::RaiiSubparser::RaiiSubparser(ArgumentParser &parser_, std::vector<std::string> args_)
3732
548
        : command(parser_.SelectedCommand()), parser(std::move(args_), parser_, command, parser_.helpParams), oldSubparser(command.subparser)
3733
548
    {
3734
548
        command.subparser = &parser;
3735
548
    }
3736
3737
51
    inline Command::RaiiSubparser::RaiiSubparser(const Command &command_, const HelpParams &params_): command(command_), parser(command, params_), oldSubparser(command.subparser)
3738
51
    {
3739
51
        command.subparser = &parser;
3740
51
    }
3741
3742
    inline void Subparser::Parse()
3743
599
    {
3744
599
        isParsed = true;
3745
599
        Reset();
3746
599
        command.subparserDescription = GetDescription(helpParams, 0);
3747
599
        command.subparserHasFlag = HasFlag();
3748
599
        command.subparserHasPositional = HasPositional();
3749
599
        command.subparserHasCommand = HasCommand();
3750
599
        command.subparserProgramLine = GetProgramLine(helpParams);
3751
599
        if (parser == nullptr)
3752
51
        {
3753
51
#ifndef ARGS_NOEXCEPT
3754
51
            throw args::SubparserError();
3755
#else
3756
            error = Error::Subparser;
3757
            return;
3758
#endif
3759
51
        }
3760
3761
548
        auto it = parser->Parse(args.begin(), args.end());
3762
548
        command.Validate(parser->ShortPrefix(), parser->LongPrefix());
3763
548
        kicked.assign(it, args.end());
3764
3765
#ifdef ARGS_NOEXCEPT
3766
        command.subparserError = GetError();
3767
#endif
3768
548
    }
3769
3770
    inline std::ostream &operator<<(std::ostream &os, const ArgumentParser &parser)
3771
6.72k
    {
3772
6.72k
        parser.Help(os);
3773
6.72k
        return os;
3774
6.72k
    }
3775
3776
    /** Boolean argument matcher
3777
     */
3778
    class Flag : public FlagBase
3779
    {
3780
        public:
3781
41.5k
            Flag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_): FlagBase(name_, help_, std::move(matcher_), options_)
3782
41.5k
            {
3783
41.5k
                group_.Add(*this);
3784
41.5k
            }
3785
3786
13.2k
            Flag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const bool extraError_ = false): Flag(group_, name_, help_, std::move(matcher_), extraError_ ? Options::Single : Options::None)
3787
13.2k
            {
3788
13.2k
            }
3789
3790
0
            virtual ~Flag() {}
3791
3792
            /** Get whether this was matched
3793
             */
3794
            bool Get() const
3795
24
            {
3796
24
                return Matched();
3797
24
            }
3798
3799
            virtual Nargs NumberOfArguments() const noexcept override
3800
75.1k
            {
3801
75.1k
                return 0;
3802
75.1k
            }
3803
3804
            virtual void ParseValue(const std::vector<std::string>&) override
3805
16.9k
            {
3806
16.9k
            }
3807
    };
3808
3809
    /** Help flag class
3810
     *
3811
     * Works like a regular flag, but throws an instance of Help when it is matched
3812
     */
3813
    class HelpFlag : public Flag
3814
    {
3815
        public:
3816
11.3k
            HelpFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_ = {}): Flag(group_, name_, help_, std::move(matcher_), options_) {}
3817
3818
0
            virtual ~HelpFlag() {}
3819
3820
            virtual void ParseValue(const std::vector<std::string> &)
3821
223
            {
3822
#ifdef ARGS_NOEXCEPT
3823
                    error = Error::Help;
3824
                    errorMsg = Name();
3825
#else
3826
223
                    throw Help(Name());
3827
223
#endif
3828
223
            }
3829
3830
            /** Get whether this was matched
3831
             */
3832
            bool Get() const noexcept
3833
0
            {
3834
0
                return Matched();
3835
0
            }
3836
    };
3837
3838
    /** A flag class that simply counts the number of times it's matched
3839
     */
3840
    class CounterFlag : public Flag
3841
    {
3842
        private:
3843
            const int startcount;
3844
            int count;
3845
3846
        public:
3847
            CounterFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const int startcount_ = 0, Options options_ = {}):
3848
8.52k
                Flag(group_, name_, help_, std::move(matcher_), options_), startcount(startcount_), count(startcount_) {}
3849
3850
0
            virtual ~CounterFlag() {}
3851
3852
            virtual FlagBase *Match(const EitherFlag &arg) override
3853
10.5k
            {
3854
10.5k
                auto me = FlagBase::Match(arg);
3855
10.5k
                if (me)
3856
3.59k
                {
3857
#ifdef ARGS_NOEXCEPT
3858
                    // Suppress increment when FlagBase::Match recorded an
3859
                    // error on this same call (e.g. Options::Single violated).
3860
                    // In non-noexcept mode that path would have thrown before
3861
                    // reaching here and the count would not have advanced.
3862
                    if (GetError() != Error::None)
3863
                    {
3864
                        return me;
3865
                    }
3866
#endif
3867
3.59k
                    ++count;
3868
3.59k
                }
3869
10.5k
                return me;
3870
10.5k
            }
3871
3872
            /** Get the count
3873
             */
3874
            int &Get() noexcept
3875
1.80k
            {
3876
1.80k
                return count;
3877
1.80k
            }
3878
3879
0
            int &operator *() noexcept {
3880
0
                return count;
3881
0
            }
3882
            
3883
0
            const int &operator *() const noexcept {
3884
0
                return count;
3885
0
            }
3886
3887
            virtual void Reset() noexcept override
3888
12.1k
            {
3889
12.1k
                FlagBase::Reset();
3890
12.1k
                count = startcount;
3891
12.1k
            }
3892
    };
3893
3894
    /** A flag class that calls a function when it's matched
3895
     */
3896
    class ActionFlag : public FlagBase
3897
    {
3898
        private:
3899
            std::function<void(const std::vector<std::string> &)> action;
3900
            Nargs nargs;
3901
3902
        public:
3903
            ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Nargs nargs_, std::function<void(const std::vector<std::string> &)> action_, Options options_ = {}):
3904
                FlagBase(name_, help_, std::move(matcher_), options_), action(std::move(action_)), nargs(nargs_)
3905
0
            {
3906
0
                group_.Add(*this);
3907
0
            }
3908
3909
            ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, std::function<void(const std::string &)> action_, Options options_ = {}):
3910
                FlagBase(name_, help_, std::move(matcher_), options_), nargs(1)
3911
0
            {
3912
0
                group_.Add(*this);
3913
0
                action = [action_](const std::vector<std::string> &a) { return action_(a.at(0)); };
3914
0
            }
3915
3916
            ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, std::function<void()> action_, Options options_ = {}):
3917
                FlagBase(name_, help_, std::move(matcher_), options_), nargs(0)
3918
0
            {
3919
0
                group_.Add(*this);
3920
0
                action = [action_](const std::vector<std::string> &) { return action_(); };
3921
0
            }
3922
3923
            virtual Nargs NumberOfArguments() const noexcept override
3924
0
            { return nargs; }
3925
3926
            virtual void ParseValue(const std::vector<std::string> &value) override
3927
0
            { action(value); }
3928
    };
3929
3930
    /** A default Reader class for argument classes
3931
     *
3932
     * If destination type is assignable to std::string it uses an assignment to std::string.
3933
     * Otherwise ValueReader simply uses a std::istringstream to read into the destination type, and
3934
     * raises a ParseError if there are any characters left.
3935
     */
3936
    struct ValueReader
3937
    {
3938
      private:
3939
        template <typename T>
3940
        static typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, bool>::type
3941
        HasUnsignedNegativeSign(const std::string &value)
3942
1.96k
        {
3943
1.96k
            const auto firstNonSpace = std::find_if_not(value.begin(), value.end(), [](char c)
3944
3.03k
            {
3945
3.03k
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
3.03k
            });
_ZZN4args11ValueReader23HasUnsignedNegativeSignItEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEENKUlcE_clEc
Line
Count
Source
3944
759
            {
3945
759
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
759
            });
_ZZN4args11ValueReader23HasUnsignedNegativeSignIjEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEENKUlcE_clEc
Line
Count
Source
3944
759
            {
3945
759
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
759
            });
_ZZN4args11ValueReader23HasUnsignedNegativeSignImEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEENKUlcE_clEc
Line
Count
Source
3944
759
            {
3945
759
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
759
            });
_ZZN4args11ValueReader23HasUnsignedNegativeSignIyEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEENKUlcE_clEc
Line
Count
Source
3944
759
            {
3945
759
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
759
            });
3947
3948
1.96k
            return firstNonSpace != value.end() && *firstNonSpace == '-';
3949
1.96k
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignItEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3942
492
        {
3943
492
            const auto firstNonSpace = std::find_if_not(value.begin(), value.end(), [](char c)
3944
492
            {
3945
492
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
492
            });
3947
3948
492
            return firstNonSpace != value.end() && *firstNonSpace == '-';
3949
492
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignIjEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3942
492
        {
3943
492
            const auto firstNonSpace = std::find_if_not(value.begin(), value.end(), [](char c)
3944
492
            {
3945
492
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
492
            });
3947
3948
492
            return firstNonSpace != value.end() && *firstNonSpace == '-';
3949
492
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignImEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3942
492
        {
3943
492
            const auto firstNonSpace = std::find_if_not(value.begin(), value.end(), [](char c)
3944
492
            {
3945
492
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
492
            });
3947
3948
492
            return firstNonSpace != value.end() && *firstNonSpace == '-';
3949
492
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignIyEENSt3__19enable_ifIXaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3942
492
        {
3943
492
            const auto firstNonSpace = std::find_if_not(value.begin(), value.end(), [](char c)
3944
492
            {
3945
492
                return std::isspace(static_cast<unsigned char>(c)) != 0;
3946
492
            });
3947
3948
492
            return firstNonSpace != value.end() && *firstNonSpace == '-';
3949
492
        }
3950
3951
        template <typename T>
3952
        static typename std::enable_if<!std::is_integral<T>::value || !std::is_unsigned<T>::value, bool>::type
3953
        HasUnsignedNegativeSign(const std::string &)
3954
2.68k
        {
3955
2.68k
            return false;
3956
2.68k
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignIiEENSt3__19enable_ifIXoontsr3std11is_integralIT_EE5valuentsr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3954
1.20k
        {
3955
1.20k
            return false;
3956
1.20k
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignIsEENSt3__19enable_ifIXoontsr3std11is_integralIT_EE5valuentsr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3954
492
        {
3955
492
            return false;
3956
492
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignIlEENSt3__19enable_ifIXoontsr3std11is_integralIT_EE5valuentsr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3954
492
        {
3955
492
            return false;
3956
492
        }
_ZN4args11ValueReader23HasUnsignedNegativeSignIxEENSt3__19enable_ifIXoontsr3std11is_integralIT_EE5valuentsr3std11is_unsignedIS4_EE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
3954
492
        {
3955
492
            return false;
3956
492
        }
3957
3958
      public:
3959
        template <typename T>
3960
        typename std::enable_if<
3961
            std::is_integral<T>::value &&
3962
            !std::is_same<T, bool>::value &&
3963
            !std::is_same<T, char>::value &&
3964
            !std::is_same<T, signed char>::value &&
3965
            !std::is_same<T, unsigned char>::value,
3966
            bool>::type
3967
        ParseNumericValue(const std::string &value, T &destination)
3968
4.65k
        {
3969
4.65k
            if (HasUnsignedNegativeSign<T>(value))
3970
560
            {
3971
560
                return false;
3972
560
            }
3973
3974
4.09k
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
4.09k
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
4.09k
            const int saved_errno = errno;
3989
4.09k
            errno = 0;
3990
3991
4.09k
            char *end = nullptr;
3992
3993
4.09k
            if (std::is_unsigned<T>::value)
3994
1.40k
            {
3995
1.40k
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
1.40k
                if (end == begin)
3997
176
                {
3998
176
                    errno = saved_errno;
3999
176
                    return false;
4000
176
                }
4001
2.46k
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
1.23k
                {
4003
1.23k
                    ++end;
4004
1.23k
                }
4005
1.23k
                if (end != stop || errno == ERANGE ||
4006
1.05k
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
497
                {
4008
497
                    errno = saved_errno;
4009
497
                    return false;
4010
497
                }
4011
4012
735
                destination = static_cast<T>(parsed);
4013
735
            }
4014
2.68k
            else
4015
2.68k
            {
4016
2.68k
                const long long parsed = std::strtoll(begin, &end, 0);
4017
2.68k
                if (end == begin)
4018
242
                {
4019
242
                    errno = saved_errno;
4020
242
                    return false;
4021
242
                }
4022
3.97k
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
1.53k
                {
4024
1.53k
                    ++end;
4025
1.53k
                }
4026
2.44k
                if (end != stop || errno == ERANGE ||
4027
2.00k
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
1.71k
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
1.00k
                {
4030
1.00k
                    errno = saved_errno;
4031
1.00k
                    return false;
4032
1.00k
                }
4033
4034
1.43k
                destination = static_cast<T>(parsed);
4035
1.43k
            }
4036
4037
4.09k
            errno = saved_errno;
4038
2.17k
            return true;
4039
4.09k
        }
_ZN4args11ValueReader17ParseNumericValueIiEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
1.20k
        {
3969
1.20k
            if (HasUnsignedNegativeSign<T>(value))
3970
0
            {
3971
0
                return false;
3972
0
            }
3973
3974
1.20k
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
1.20k
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
1.20k
            const int saved_errno = errno;
3989
1.20k
            errno = 0;
3990
3991
1.20k
            char *end = nullptr;
3992
3993
1.20k
            if (std::is_unsigned<T>::value)
3994
0
            {
3995
0
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
0
                if (end == begin)
3997
0
                {
3998
0
                    errno = saved_errno;
3999
0
                    return false;
4000
0
                }
4001
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
0
                {
4003
0
                    ++end;
4004
0
                }
4005
0
                if (end != stop || errno == ERANGE ||
4006
0
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
0
                {
4008
0
                    errno = saved_errno;
4009
0
                    return false;
4010
0
                }
4011
4012
0
                destination = static_cast<T>(parsed);
4013
0
            }
4014
1.20k
            else
4015
1.20k
            {
4016
1.20k
                const long long parsed = std::strtoll(begin, &end, 0);
4017
1.20k
                if (end == begin)
4018
107
                {
4019
107
                    errno = saved_errno;
4020
107
                    return false;
4021
107
                }
4022
1.70k
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
609
                {
4024
609
                    ++end;
4025
609
                }
4026
1.10k
                if (end != stop || errno == ERANGE ||
4027
960
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
786
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
419
                {
4030
419
                    errno = saved_errno;
4031
419
                    return false;
4032
419
                }
4033
4034
681
                destination = static_cast<T>(parsed);
4035
681
            }
4036
4037
1.20k
            errno = saved_errno;
4038
681
            return true;
4039
1.20k
        }
_ZN4args11ValueReader17ParseNumericValueIsEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
492
        {
3969
492
            if (HasUnsignedNegativeSign<T>(value))
3970
0
            {
3971
0
                return false;
3972
0
            }
3973
3974
492
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
492
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
492
            const int saved_errno = errno;
3989
492
            errno = 0;
3990
3991
492
            char *end = nullptr;
3992
3993
492
            if (std::is_unsigned<T>::value)
3994
0
            {
3995
0
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
0
                if (end == begin)
3997
0
                {
3998
0
                    errno = saved_errno;
3999
0
                    return false;
4000
0
                }
4001
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
0
                {
4003
0
                    ++end;
4004
0
                }
4005
0
                if (end != stop || errno == ERANGE ||
4006
0
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
0
                {
4008
0
                    errno = saved_errno;
4009
0
                    return false;
4010
0
                }
4011
4012
0
                destination = static_cast<T>(parsed);
4013
0
            }
4014
492
            else
4015
492
            {
4016
492
                const long long parsed = std::strtoll(begin, &end, 0);
4017
492
                if (end == begin)
4018
45
                {
4019
45
                    errno = saved_errno;
4020
45
                    return false;
4021
45
                }
4022
755
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
308
                {
4024
308
                    ++end;
4025
308
                }
4026
447
                if (end != stop || errno == ERANGE ||
4027
348
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
230
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
388
                {
4030
388
                    errno = saved_errno;
4031
388
                    return false;
4032
388
                }
4033
4034
59
                destination = static_cast<T>(parsed);
4035
59
            }
4036
4037
492
            errno = saved_errno;
4038
59
            return true;
4039
492
        }
_ZN4args11ValueReader17ParseNumericValueIlEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
492
        {
3969
492
            if (HasUnsignedNegativeSign<T>(value))
3970
0
            {
3971
0
                return false;
3972
0
            }
3973
3974
492
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
492
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
492
            const int saved_errno = errno;
3989
492
            errno = 0;
3990
3991
492
            char *end = nullptr;
3992
3993
492
            if (std::is_unsigned<T>::value)
3994
0
            {
3995
0
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
0
                if (end == begin)
3997
0
                {
3998
0
                    errno = saved_errno;
3999
0
                    return false;
4000
0
                }
4001
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
0
                {
4003
0
                    ++end;
4004
0
                }
4005
0
                if (end != stop || errno == ERANGE ||
4006
0
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
0
                {
4008
0
                    errno = saved_errno;
4009
0
                    return false;
4010
0
                }
4011
4012
0
                destination = static_cast<T>(parsed);
4013
0
            }
4014
492
            else
4015
492
            {
4016
492
                const long long parsed = std::strtoll(begin, &end, 0);
4017
492
                if (end == begin)
4018
45
                {
4019
45
                    errno = saved_errno;
4020
45
                    return false;
4021
45
                }
4022
755
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
308
                {
4024
308
                    ++end;
4025
308
                }
4026
447
                if (end != stop || errno == ERANGE ||
4027
348
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
348
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
99
                {
4030
99
                    errno = saved_errno;
4031
99
                    return false;
4032
99
                }
4033
4034
348
                destination = static_cast<T>(parsed);
4035
348
            }
4036
4037
492
            errno = saved_errno;
4038
348
            return true;
4039
492
        }
_ZN4args11ValueReader17ParseNumericValueIxEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
492
        {
3969
492
            if (HasUnsignedNegativeSign<T>(value))
3970
0
            {
3971
0
                return false;
3972
0
            }
3973
3974
492
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
492
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
492
            const int saved_errno = errno;
3989
492
            errno = 0;
3990
3991
492
            char *end = nullptr;
3992
3993
492
            if (std::is_unsigned<T>::value)
3994
0
            {
3995
0
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
0
                if (end == begin)
3997
0
                {
3998
0
                    errno = saved_errno;
3999
0
                    return false;
4000
0
                }
4001
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
0
                {
4003
0
                    ++end;
4004
0
                }
4005
0
                if (end != stop || errno == ERANGE ||
4006
0
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
0
                {
4008
0
                    errno = saved_errno;
4009
0
                    return false;
4010
0
                }
4011
4012
0
                destination = static_cast<T>(parsed);
4013
0
            }
4014
492
            else
4015
492
            {
4016
492
                const long long parsed = std::strtoll(begin, &end, 0);
4017
492
                if (end == begin)
4018
45
                {
4019
45
                    errno = saved_errno;
4020
45
                    return false;
4021
45
                }
4022
755
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
308
                {
4024
308
                    ++end;
4025
308
                }
4026
447
                if (end != stop || errno == ERANGE ||
4027
348
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
348
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
99
                {
4030
99
                    errno = saved_errno;
4031
99
                    return false;
4032
99
                }
4033
4034
348
                destination = static_cast<T>(parsed);
4035
348
            }
4036
4037
492
            errno = saved_errno;
4038
348
            return true;
4039
492
        }
_ZN4args11ValueReader17ParseNumericValueItEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
492
        {
3969
492
            if (HasUnsignedNegativeSign<T>(value))
3970
140
            {
3971
140
                return false;
3972
140
            }
3973
3974
352
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
352
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
352
            const int saved_errno = errno;
3989
352
            errno = 0;
3990
3991
352
            char *end = nullptr;
3992
3993
352
            if (std::is_unsigned<T>::value)
3994
352
            {
3995
352
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
352
                if (end == begin)
3997
44
                {
3998
44
                    errno = saved_errno;
3999
44
                    return false;
4000
44
                }
4001
616
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
308
                {
4003
308
                    ++end;
4004
308
                }
4005
308
                if (end != stop || errno == ERANGE ||
4006
263
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
250
                {
4008
250
                    errno = saved_errno;
4009
250
                    return false;
4010
250
                }
4011
4012
58
                destination = static_cast<T>(parsed);
4013
58
            }
4014
0
            else
4015
0
            {
4016
0
                const long long parsed = std::strtoll(begin, &end, 0);
4017
0
                if (end == begin)
4018
0
                {
4019
0
                    errno = saved_errno;
4020
0
                    return false;
4021
0
                }
4022
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
0
                {
4024
0
                    ++end;
4025
0
                }
4026
0
                if (end != stop || errno == ERANGE ||
4027
0
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
0
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
0
                {
4030
0
                    errno = saved_errno;
4031
0
                    return false;
4032
0
                }
4033
4034
0
                destination = static_cast<T>(parsed);
4035
0
            }
4036
4037
352
            errno = saved_errno;
4038
58
            return true;
4039
352
        }
_ZN4args11ValueReader17ParseNumericValueIjEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
492
        {
3969
492
            if (HasUnsignedNegativeSign<T>(value))
3970
140
            {
3971
140
                return false;
3972
140
            }
3973
3974
352
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
352
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
352
            const int saved_errno = errno;
3989
352
            errno = 0;
3990
3991
352
            char *end = nullptr;
3992
3993
352
            if (std::is_unsigned<T>::value)
3994
352
            {
3995
352
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
352
                if (end == begin)
3997
44
                {
3998
44
                    errno = saved_errno;
3999
44
                    return false;
4000
44
                }
4001
616
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
308
                {
4003
308
                    ++end;
4004
308
                }
4005
308
                if (end != stop || errno == ERANGE ||
4006
263
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
157
                {
4008
157
                    errno = saved_errno;
4009
157
                    return false;
4010
157
                }
4011
4012
151
                destination = static_cast<T>(parsed);
4013
151
            }
4014
0
            else
4015
0
            {
4016
0
                const long long parsed = std::strtoll(begin, &end, 0);
4017
0
                if (end == begin)
4018
0
                {
4019
0
                    errno = saved_errno;
4020
0
                    return false;
4021
0
                }
4022
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
0
                {
4024
0
                    ++end;
4025
0
                }
4026
0
                if (end != stop || errno == ERANGE ||
4027
0
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
0
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
0
                {
4030
0
                    errno = saved_errno;
4031
0
                    return false;
4032
0
                }
4033
4034
0
                destination = static_cast<T>(parsed);
4035
0
            }
4036
4037
352
            errno = saved_errno;
4038
151
            return true;
4039
352
        }
_ZN4args11ValueReader17ParseNumericValueImEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
492
        {
3969
492
            if (HasUnsignedNegativeSign<T>(value))
3970
140
            {
3971
140
                return false;
3972
140
            }
3973
3974
352
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
352
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
352
            const int saved_errno = errno;
3989
352
            errno = 0;
3990
3991
352
            char *end = nullptr;
3992
3993
352
            if (std::is_unsigned<T>::value)
3994
352
            {
3995
352
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
352
                if (end == begin)
3997
44
                {
3998
44
                    errno = saved_errno;
3999
44
                    return false;
4000
44
                }
4001
616
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
308
                {
4003
308
                    ++end;
4004
308
                }
4005
308
                if (end != stop || errno == ERANGE ||
4006
263
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
45
                {
4008
45
                    errno = saved_errno;
4009
45
                    return false;
4010
45
                }
4011
4012
263
                destination = static_cast<T>(parsed);
4013
263
            }
4014
0
            else
4015
0
            {
4016
0
                const long long parsed = std::strtoll(begin, &end, 0);
4017
0
                if (end == begin)
4018
0
                {
4019
0
                    errno = saved_errno;
4020
0
                    return false;
4021
0
                }
4022
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
0
                {
4024
0
                    ++end;
4025
0
                }
4026
0
                if (end != stop || errno == ERANGE ||
4027
0
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
0
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
0
                {
4030
0
                    errno = saved_errno;
4031
0
                    return false;
4032
0
                }
4033
4034
0
                destination = static_cast<T>(parsed);
4035
0
            }
4036
4037
352
            errno = saved_errno;
4038
263
            return true;
4039
352
        }
_ZN4args11ValueReader17ParseNumericValueIyEENSt3__19enable_ifIXaaaaaaaasr3std11is_integralIT_EE5valuentsr3std7is_sameIS4_bEE5valuentsr3std7is_sameIS4_cEE5valuentsr3std7is_sameIS4_aEE5valuentsr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
3968
492
        {
3969
492
            if (HasUnsignedNegativeSign<T>(value))
3970
140
            {
3971
140
                return false;
3972
140
            }
3973
3974
352
            const char *begin = value.c_str();
3975
            // The true end of the value, derived from its length rather than
3976
            // from the first NUL. strtoull/strtoll treat the buffer as a C
3977
            // string and stop at an embedded '\0', so checking `*end == '\0'`
3978
            // for "no trailing data" is defeated by a value like "12\0junk":
3979
            // end lands on the embedded NUL and the junk after it is silently
3980
            // accepted. Comparing against `stop` validates the whole string
3981
            // and matches the istringstream-based reader used for other types.
3982
352
            const char *const stop = begin + value.size();
3983
3984
            // C++11-compatible: use strtoull/strtoll. Hardening retained from
3985
            // the original from_chars draft (errno save/restore, ERANGE check,
3986
            // narrowing range check, trailing-whitespace tolerance). No
3987
            // unconditional dependency on <charconv> / C++17.
3988
352
            const int saved_errno = errno;
3989
352
            errno = 0;
3990
3991
352
            char *end = nullptr;
3992
3993
352
            if (std::is_unsigned<T>::value)
3994
352
            {
3995
352
                const unsigned long long parsed = std::strtoull(begin, &end, 0);
3996
352
                if (end == begin)
3997
44
                {
3998
44
                    errno = saved_errno;
3999
44
                    return false;
4000
44
                }
4001
616
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4002
308
                {
4003
308
                    ++end;
4004
308
                }
4005
308
                if (end != stop || errno == ERANGE ||
4006
263
                    parsed > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
4007
45
                {
4008
45
                    errno = saved_errno;
4009
45
                    return false;
4010
45
                }
4011
4012
263
                destination = static_cast<T>(parsed);
4013
263
            }
4014
0
            else
4015
0
            {
4016
0
                const long long parsed = std::strtoll(begin, &end, 0);
4017
0
                if (end == begin)
4018
0
                {
4019
0
                    errno = saved_errno;
4020
0
                    return false;
4021
0
                }
4022
0
                while (end != stop && std::isspace(static_cast<unsigned char>(*end)))
4023
0
                {
4024
0
                    ++end;
4025
0
                }
4026
0
                if (end != stop || errno == ERANGE ||
4027
0
                    parsed < static_cast<long long>(std::numeric_limits<T>::min()) ||
4028
0
                    parsed > static_cast<long long>(std::numeric_limits<T>::max()))
4029
0
                {
4030
0
                    errno = saved_errno;
4031
0
                    return false;
4032
0
                }
4033
4034
0
                destination = static_cast<T>(parsed);
4035
0
            }
4036
4037
352
            errno = saved_errno;
4038
263
            return true;
4039
352
        }
4040
4041
        template <typename T>
4042
        typename std::enable_if<
4043
            !std::is_integral<T>::value ||
4044
            std::is_same<T, bool>::value ||
4045
            std::is_same<T, char>::value ||
4046
            std::is_same<T, signed char>::value ||
4047
            std::is_same<T, unsigned char>::value,
4048
            bool>::type
4049
        ParseNumericValue(const std::string &value, T &destination)
4050
3.72k
        {
4051
3.72k
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
3.72k
            ss.imbue(std::locale::classic());
4058
3.72k
            ss >> destination;
4059
3.72k
            if (ss.fail())
4060
448
            {
4061
448
                return false;
4062
448
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
3.27k
            char extra = '\0';
4068
3.27k
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
3.27k
            if (ss.fail())
4073
1.54k
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
1.54k
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
1.54k
                return ss.eof();
4077
1.54k
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
1.73k
            return false;
4080
3.27k
        }
_ZN4args11ValueReader17ParseNumericValueIdEENSt3__19enable_ifIXoooooooontsr3std11is_integralIT_EE5valuesr3std7is_sameIS4_bEE5valuesr3std7is_sameIS4_cEE5valuesr3std7is_sameIS4_aEE5valuesr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
4050
770
        {
4051
770
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
770
            ss.imbue(std::locale::classic());
4058
770
            ss >> destination;
4059
770
            if (ss.fail())
4060
74
            {
4061
74
                return false;
4062
74
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
696
            char extra = '\0';
4068
696
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
696
            if (ss.fail())
4073
637
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
637
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
637
                return ss.eof();
4077
637
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
59
            return false;
4080
696
        }
_ZN4args11ValueReader17ParseNumericValueIbEENSt3__19enable_ifIXoooooooontsr3std11is_integralIT_EE5valuesr3std7is_sameIS4_bEE5valuesr3std7is_sameIS4_cEE5valuesr3std7is_sameIS4_aEE5valuesr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
4050
492
        {
4051
492
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
492
            ss.imbue(std::locale::classic());
4058
492
            ss >> destination;
4059
492
            if (ss.fail())
4060
240
            {
4061
240
                return false;
4062
240
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
252
            char extra = '\0';
4068
252
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
252
            if (ss.fail())
4073
10
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
10
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
10
                return ss.eof();
4077
10
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
242
            return false;
4080
252
        }
_ZN4args11ValueReader17ParseNumericValueIcEENSt3__19enable_ifIXoooooooontsr3std11is_integralIT_EE5valuesr3std7is_sameIS4_bEE5valuesr3std7is_sameIS4_cEE5valuesr3std7is_sameIS4_aEE5valuesr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
4050
492
        {
4051
492
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
492
            ss.imbue(std::locale::classic());
4058
492
            ss >> destination;
4059
492
            if (ss.fail())
4060
5
            {
4061
5
                return false;
4062
5
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
487
            char extra = '\0';
4068
487
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
487
            if (ss.fail())
4073
29
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
29
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
29
                return ss.eof();
4077
29
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
458
            return false;
4080
487
        }
_ZN4args11ValueReader17ParseNumericValueIaEENSt3__19enable_ifIXoooooooontsr3std11is_integralIT_EE5valuesr3std7is_sameIS4_bEE5valuesr3std7is_sameIS4_cEE5valuesr3std7is_sameIS4_aEE5valuesr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
4050
492
        {
4051
492
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
492
            ss.imbue(std::locale::classic());
4058
492
            ss >> destination;
4059
492
            if (ss.fail())
4060
5
            {
4061
5
                return false;
4062
5
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
487
            char extra = '\0';
4068
487
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
487
            if (ss.fail())
4073
29
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
29
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
29
                return ss.eof();
4077
29
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
458
            return false;
4080
487
        }
_ZN4args11ValueReader17ParseNumericValueIhEENSt3__19enable_ifIXoooooooontsr3std11is_integralIT_EE5valuesr3std7is_sameIS4_bEE5valuesr3std7is_sameIS4_cEE5valuesr3std7is_sameIS4_aEE5valuesr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
4050
492
        {
4051
492
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
492
            ss.imbue(std::locale::classic());
4058
492
            ss >> destination;
4059
492
            if (ss.fail())
4060
5
            {
4061
5
                return false;
4062
5
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
487
            char extra = '\0';
4068
487
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
487
            if (ss.fail())
4073
29
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
29
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
29
                return ss.eof();
4077
29
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
458
            return false;
4080
487
        }
_ZN4args11ValueReader17ParseNumericValueIfEENSt3__19enable_ifIXoooooooontsr3std11is_integralIT_EE5valuesr3std7is_sameIS4_bEE5valuesr3std7is_sameIS4_cEE5valuesr3std7is_sameIS4_aEE5valuesr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
4050
492
        {
4051
492
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
492
            ss.imbue(std::locale::classic());
4058
492
            ss >> destination;
4059
492
            if (ss.fail())
4060
64
            {
4061
64
                return false;
4062
64
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
428
            char extra = '\0';
4068
428
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
428
            if (ss.fail())
4073
400
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
400
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
400
                return ss.eof();
4077
400
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
28
            return false;
4080
428
        }
_ZN4args11ValueReader17ParseNumericValueIeEENSt3__19enable_ifIXoooooooontsr3std11is_integralIT_EE5valuesr3std7is_sameIS4_bEE5valuesr3std7is_sameIS4_cEE5valuesr3std7is_sameIS4_aEE5valuesr3std7is_sameIS4_hEE5valueEbE4typeERKNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERS4_
Line
Count
Source
4050
492
        {
4051
492
            std::istringstream ss(value);
4052
            // Pin parsing to the C locale so that the decimal separator and
4053
            // thousands grouping behavior do not silently depend on whatever
4054
            // std::locale::global was last set to elsewhere in the process.
4055
            // Without this, e.g. "3.14" parses as 3 (with ".14" trailing) in
4056
            // any locale whose numpunct facet treats ',' as the decimal point.
4057
492
            ss.imbue(std::locale::classic());
4058
492
            ss >> destination;
4059
492
            if (ss.fail())
4060
55
            {
4061
55
                return false;
4062
55
            }
4063
4064
            // Check for trailing garbage by attempting to extract any remaining characters.
4065
            // Do not use 'ss >> std::ws' followed by peek(), as std::ws can set failbit
4066
            // on EOF, causing false rejection of valid input.
4067
437
            char extra = '\0';
4068
437
            ss >> std::ws >> extra;
4069
            // If extraction succeeded, there's trailing garbage (return false).
4070
            // If extraction failed due to EOF only (goodbit after ws extraction), it's valid (return true).
4071
            // If extraction failed for other reasons, it's invalid (return false).
4072
437
            if (ss.fail())
4073
406
            {
4074
                // Clear the failbit to check if EOF is the only issue
4075
406
                ss.clear(ss.rdstate() & ~std::ios::failbit);
4076
406
                return ss.eof();
4077
406
            }
4078
            // Extraction succeeded, meaning there's trailing garbage
4079
31
            return false;
4080
437
        }
4081
4082
        template <typename T>
4083
        typename std::enable_if<!std::is_assignable<T, std::string>::value, bool>::type
4084
        operator ()(const std::string &name, const std::string &value, T &destination)
4085
8.37k
        {
4086
8.37k
            const bool success = ParseNumericValue(value, destination);
4087
8.37k
            if (!success)
4088
4.66k
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
4.66k
                std::ostringstream problem;
4094
4.66k
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
4.66k
                throw ParseError(problem.str());
4096
4.66k
#endif
4097
4.66k
            }
4098
3.71k
            return true;
4099
8.37k
        }
_ZN4args11ValueReaderclIiEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
1.20k
        {
4086
1.20k
            const bool success = ParseNumericValue(value, destination);
4087
1.20k
            if (!success)
4088
526
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
526
                std::ostringstream problem;
4094
526
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
526
                throw ParseError(problem.str());
4096
526
#endif
4097
526
            }
4098
681
            return true;
4099
1.20k
        }
_ZN4args11ValueReaderclIdEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
770
        {
4086
770
            const bool success = ParseNumericValue(value, destination);
4087
770
            if (!success)
4088
133
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
133
                std::ostringstream problem;
4094
133
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
133
                throw ParseError(problem.str());
4096
133
#endif
4097
133
            }
4098
637
            return true;
4099
770
        }
_ZN4args11ValueReaderclIbEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
482
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
482
                std::ostringstream problem;
4094
482
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
482
                throw ParseError(problem.str());
4096
482
#endif
4097
482
            }
4098
10
            return true;
4099
492
        }
_ZN4args11ValueReaderclIcEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
463
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
463
                std::ostringstream problem;
4094
463
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
463
                throw ParseError(problem.str());
4096
463
#endif
4097
463
            }
4098
29
            return true;
4099
492
        }
_ZN4args11ValueReaderclIaEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
463
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
463
                std::ostringstream problem;
4094
463
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
463
                throw ParseError(problem.str());
4096
463
#endif
4097
463
            }
4098
29
            return true;
4099
492
        }
_ZN4args11ValueReaderclIhEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
463
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
463
                std::ostringstream problem;
4094
463
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
463
                throw ParseError(problem.str());
4096
463
#endif
4097
463
            }
4098
29
            return true;
4099
492
        }
_ZN4args11ValueReaderclIsEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
433
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
433
                std::ostringstream problem;
4094
433
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
433
                throw ParseError(problem.str());
4096
433
#endif
4097
433
            }
4098
59
            return true;
4099
492
        }
_ZN4args11ValueReaderclIlEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
144
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
144
                std::ostringstream problem;
4094
144
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
144
                throw ParseError(problem.str());
4096
144
#endif
4097
144
            }
4098
348
            return true;
4099
492
        }
_ZN4args11ValueReaderclIxEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
144
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
144
                std::ostringstream problem;
4094
144
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
144
                throw ParseError(problem.str());
4096
144
#endif
4097
144
            }
4098
348
            return true;
4099
492
        }
_ZN4args11ValueReaderclItEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
434
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
434
                std::ostringstream problem;
4094
434
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
434
                throw ParseError(problem.str());
4096
434
#endif
4097
434
            }
4098
58
            return true;
4099
492
        }
_ZN4args11ValueReaderclIjEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
341
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
341
                std::ostringstream problem;
4094
341
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
341
                throw ParseError(problem.str());
4096
341
#endif
4097
341
            }
4098
151
            return true;
4099
492
        }
_ZN4args11ValueReaderclImEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
229
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
229
                std::ostringstream problem;
4094
229
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
229
                throw ParseError(problem.str());
4096
229
#endif
4097
229
            }
4098
263
            return true;
4099
492
        }
_ZN4args11ValueReaderclIyEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
229
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
229
                std::ostringstream problem;
4094
229
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
229
                throw ParseError(problem.str());
4096
229
#endif
4097
229
            }
4098
263
            return true;
4099
492
        }
_ZN4args11ValueReaderclIfEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
92
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
92
                std::ostringstream problem;
4094
92
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
92
                throw ParseError(problem.str());
4096
92
#endif
4097
92
            }
4098
400
            return true;
4099
492
        }
_ZN4args11ValueReaderclIeEENSt3__19enable_ifIXntsr3std13is_assignableIT_NS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEE5valueEbE4typeERKSA_SE_RS4_
Line
Count
Source
4085
492
        {
4086
492
            const bool success = ParseNumericValue(value, destination);
4087
492
            if (!success)
4088
86
            {
4089
#ifdef ARGS_NOEXCEPT
4090
                (void)name;
4091
                return false;
4092
#else
4093
86
                std::ostringstream problem;
4094
86
                problem << "Argument '" << name << "' received invalid value type '" << value << "'";
4095
86
                throw ParseError(problem.str());
4096
86
#endif
4097
86
            }
4098
406
            return true;
4099
492
        }
4100
4101
        template <typename T>
4102
        typename std::enable_if<std::is_assignable<T, std::string>::value, bool>::type
4103
        operator()(const std::string &, const std::string &value, T &destination)
4104
16.4k
        {
4105
16.4k
            destination = value;
4106
16.4k
            return true;
4107
16.4k
        }
4108
    };
4109
4110
    /** An argument-accepting flag class
4111
     * 
4112
     * \tparam T the type to extract the argument as
4113
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4114
     */
4115
    template <
4116
        typename T,
4117
        typename Reader = ValueReader>
4118
    class ValueFlag : public ValueFlagBase
4119
    {
4120
        protected:
4121
            T value;
4122
            T defaultValue;
4123
4124
            virtual std::string GetDefaultString(const HelpParams&) const override
4125
81.0k
            {
4126
81.0k
                return detail::ToString(defaultValue);
4127
81.0k
            }
args::ValueFlag<int, args::ValueReader>::GetDefaultString(args::HelpParams const&) const
Line
Count
Source
4125
26.9k
            {
4126
26.9k
                return detail::ToString(defaultValue);
4127
26.9k
            }
args::ValueFlag<double, args::ValueReader>::GetDefaultString(args::HelpParams const&) const
Line
Count
Source
4125
26.9k
            {
4126
26.9k
                return detail::ToString(defaultValue);
4127
26.9k
            }
args::ValueFlag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::ValueReader>::GetDefaultString(args::HelpParams const&) const
Line
Count
Source
4125
27.2k
            {
4126
27.2k
                return detail::ToString(defaultValue);
4127
27.2k
            }
4128
4129
        private:
4130
            Reader reader;
4131
4132
        public:
4133
4134
36.3k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), value(defaultValue_), defaultValue(defaultValue_)
4135
36.3k
            {
4136
36.3k
                group_.Add(*this);
4137
36.3k
            }
args::ValueFlag<int, args::ValueReader>::ValueFlag(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, int const&, args::Options)
Line
Count
Source
4134
12.8k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), value(defaultValue_), defaultValue(defaultValue_)
4135
12.8k
            {
4136
12.8k
                group_.Add(*this);
4137
12.8k
            }
args::ValueFlag<double, args::ValueReader>::ValueFlag(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, double const&, args::Options)
Line
Count
Source
4134
8.52k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), value(defaultValue_), defaultValue(defaultValue_)
4135
8.52k
            {
4136
8.52k
                group_.Add(*this);
4137
8.52k
            }
args::ValueFlag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::ValueReader>::ValueFlag(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Options)
Line
Count
Source
4134
14.9k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), value(defaultValue_), defaultValue(defaultValue_)
4135
14.9k
            {
4136
14.9k
                group_.Add(*this);
4137
14.9k
            }
4138
4139
32.7k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_ = T(), const bool extraError_ = false): ValueFlag(group_, name_, help_, std::move(matcher_), defaultValue_, extraError_ ? Options::Single : Options::None)
4140
32.7k
            {
4141
32.7k
            }
args::ValueFlag<int, args::ValueReader>::ValueFlag(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, int const&, bool)
Line
Count
Source
4139
11.0k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_ = T(), const bool extraError_ = false): ValueFlag(group_, name_, help_, std::move(matcher_), defaultValue_, extraError_ ? Options::Single : Options::None)
4140
11.0k
            {
4141
11.0k
            }
args::ValueFlag<double, args::ValueReader>::ValueFlag(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, double const&, bool)
Line
Count
Source
4139
8.52k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_ = T(), const bool extraError_ = false): ValueFlag(group_, name_, help_, std::move(matcher_), defaultValue_, extraError_ ? Options::Single : Options::None)
4140
8.52k
            {
4141
8.52k
            }
args::ValueFlag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::ValueReader>::ValueFlag(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)
Line
Count
Source
4139
13.1k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_ = T(), const bool extraError_ = false): ValueFlag(group_, name_, help_, std::move(matcher_), defaultValue_, extraError_ ? Options::Single : Options::None)
4140
13.1k
            {
4141
13.1k
            }
4142
4143
1.80k
            ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_): ValueFlag(group_, name_, help_, std::move(matcher_), T(), options_)
4144
1.80k
            {
4145
1.80k
            }
4146
4147
14.9k
            virtual ~ValueFlag() {}
4148
4149
            virtual void ParseValue(const std::vector<std::string> &values_) override
4150
3.70k
            {
4151
3.70k
                const std::string &value_ = values_.at(0);
4152
4153
#ifdef ARGS_NOEXCEPT
4154
                if (!reader(name, value_, this->value))
4155
                {
4156
                    error = Error::Parse;
4157
                }
4158
#else
4159
3.70k
                reader(name, value_, this->value);
4160
3.70k
#endif
4161
3.70k
            }
args::ValueFlag<int, args::ValueReader>::ParseValue(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
Line
Count
Source
4150
499
            {
4151
499
                const std::string &value_ = values_.at(0);
4152
4153
#ifdef ARGS_NOEXCEPT
4154
                if (!reader(name, value_, this->value))
4155
                {
4156
                    error = Error::Parse;
4157
                }
4158
#else
4159
499
                reader(name, value_, this->value);
4160
499
#endif
4161
499
            }
args::ValueFlag<double, args::ValueReader>::ParseValue(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
Line
Count
Source
4150
278
            {
4151
278
                const std::string &value_ = values_.at(0);
4152
4153
#ifdef ARGS_NOEXCEPT
4154
                if (!reader(name, value_, this->value))
4155
                {
4156
                    error = Error::Parse;
4157
                }
4158
#else
4159
278
                reader(name, value_, this->value);
4160
278
#endif
4161
278
            }
args::ValueFlag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::ValueReader>::ParseValue(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
Line
Count
Source
4150
2.92k
            {
4151
2.92k
                const std::string &value_ = values_.at(0);
4152
4153
#ifdef ARGS_NOEXCEPT
4154
                if (!reader(name, value_, this->value))
4155
                {
4156
                    error = Error::Parse;
4157
                }
4158
#else
4159
2.92k
                reader(name, value_, this->value);
4160
2.92k
#endif
4161
2.92k
            }
4162
4163
            virtual void Reset() noexcept override
4164
61.1k
            {
4165
61.1k
                ValueFlagBase::Reset();
4166
61.1k
                value = defaultValue;
4167
61.1k
            }
args::ValueFlag<int, args::ValueReader>::Reset()
Line
Count
Source
4164
22.5k
            {
4165
22.5k
                ValueFlagBase::Reset();
4166
22.5k
                value = defaultValue;
4167
22.5k
            }
args::ValueFlag<double, args::ValueReader>::Reset()
Line
Count
Source
4164
12.1k
            {
4165
12.1k
                ValueFlagBase::Reset();
4166
12.1k
                value = defaultValue;
4167
12.1k
            }
args::ValueFlag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::ValueReader>::Reset()
Line
Count
Source
4164
26.5k
            {
4165
26.5k
                ValueFlagBase::Reset();
4166
26.5k
                value = defaultValue;
4167
26.5k
            }
4168
4169
            /** Get the value
4170
             */
4171
            T &Get() noexcept
4172
            {
4173
                return value;
4174
            }
4175
4176
            /** Get the value
4177
             */
4178
            T &operator *() noexcept
4179
543
            {
4180
543
                return value;
4181
543
            }
args::ValueFlag<int, args::ValueReader>::operator*()
Line
Count
Source
4179
150
            {
4180
150
                return value;
4181
150
            }
args::ValueFlag<double, args::ValueReader>::operator*()
Line
Count
Source
4179
33
            {
4180
33
                return value;
4181
33
            }
args::ValueFlag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::ValueReader>::operator*()
Line
Count
Source
4179
360
            {
4180
360
                return value;
4181
360
            }
4182
4183
            /** Get the value
4184
             */
4185
            const T &operator *() const noexcept
4186
            {
4187
                return value;
4188
            }
4189
4190
            /** Get the value
4191
             */
4192
            T *operator ->() noexcept
4193
            {
4194
                return &value;
4195
            }
4196
4197
            /** Get the value
4198
             */
4199
            const T *operator ->() const noexcept
4200
            {
4201
                return &value;
4202
            }
4203
4204
            /** Get the default value
4205
             */
4206
            const T &GetDefault() noexcept
4207
            {
4208
                return defaultValue;
4209
            }
4210
    };
4211
4212
    /** An optional argument-accepting flag class
4213
     *
4214
     * \tparam T the type to extract the argument as
4215
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4216
     */
4217
    template <
4218
        typename T,
4219
        typename Reader = ValueReader>
4220
    class ImplicitValueFlag : public ValueFlag<T, Reader>
4221
    {
4222
        protected:
4223
            T implicitValue;
4224
4225
        public:
4226
4227
            ImplicitValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &implicitValue_, const T &defaultValue_ = T(), Options options_ = {})
4228
1.80k
                : ValueFlag<T, Reader>(group_, name_, help_, std::move(matcher_), defaultValue_, options_), implicitValue(implicitValue_)
4229
1.80k
            {
4230
1.80k
            }
4231
4232
            ImplicitValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_ = T(), Options options_ = {})
4233
                : ValueFlag<T, Reader>(group_, name_, help_, std::move(matcher_), defaultValue_, options_), implicitValue(defaultValue_)
4234
            {
4235
            }
4236
4237
            ImplicitValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_)
4238
                : ValueFlag<T, Reader>(group_, name_, help_, std::move(matcher_), {}, options_), implicitValue()
4239
            {
4240
            }
4241
4242
            virtual ~ImplicitValueFlag() {}
4243
4244
            virtual Nargs NumberOfArguments() const noexcept override
4245
243
            {
4246
243
                return {0, 1};
4247
243
            }
4248
4249
            virtual void ParseValue(const std::vector<std::string> &value_) override
4250
243
            {
4251
243
                if (value_.empty())
4252
106
                {
4253
106
                    this->value = implicitValue;
4254
106
                } else
4255
137
                {
4256
137
                    ValueFlag<T, Reader>::ParseValue(value_);
4257
137
                }
4258
243
            }
4259
    };
4260
4261
    /** A boolean flag containing a retrievable constant.
4262
     * 
4263
     * \tparam T the type of the constant
4264
     */
4265
    template <typename T>
4266
    class ConstantFlag : public Flag
4267
    {
4268
        T value;
4269
4270
        public:
4271
4272
        ConstantFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_, const T& value_):
4273
        Flag(group_, name_, help_, std::move(matcher_), options_),
4274
        value(value_)
4275
        {}
4276
4277
        ConstantFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T& value_, bool extraError_ = false):
4278
        Flag(group_, name_, help_, std::move(matcher_), extraError_),
4279
        value(value_)
4280
        {}
4281
4282
        T operator * () const noexcept
4283
        {
4284
            return value;
4285
        }
4286
4287
        T Get() const noexcept
4288
        {
4289
            return value;
4290
        }
4291
4292
        const T *operator -> () const noexcept
4293
        {
4294
            return &value;
4295
        }
4296
    };
4297
4298
    /** A variadic arguments accepting flag class
4299
     *
4300
     * \tparam T the type to extract the argument as
4301
     * \tparam List the list type that houses the values
4302
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4303
     */
4304
    template <
4305
        typename T,
4306
        template <typename...> class List = detail::vector,
4307
        typename Reader = ValueReader>
4308
    class NargsValueFlag : public FlagBase
4309
    {
4310
        protected:
4311
4312
            List<T> values;
4313
            const List<T> defaultValues;
4314
            Nargs nargs;
4315
            Reader reader;
4316
4317
        public:
4318
4319
            typedef List<T> Container;
4320
            typedef T value_type;
4321
            typedef typename Container::allocator_type allocator_type;
4322
            typedef typename Container::pointer pointer;
4323
            typedef typename Container::const_pointer const_pointer;
4324
            typedef T& reference;
4325
            typedef const T& const_reference;
4326
            typedef typename Container::size_type size_type;
4327
            typedef typename Container::difference_type difference_type;
4328
            typedef typename Container::iterator iterator;
4329
            typedef typename Container::const_iterator const_iterator;
4330
            typedef std::reverse_iterator<iterator> reverse_iterator;
4331
            typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
4332
4333
            NargsValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Nargs nargs_, const List<T> &defaultValues_ = {}, Options options_ = {})
4334
1.80k
                : FlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_),nargs(nargs_)
4335
1.80k
            {
4336
1.80k
                group_.Add(*this);
4337
1.80k
            }
4338
4339
1.80k
            virtual ~NargsValueFlag() {}
4340
4341
            virtual Nargs NumberOfArguments() const noexcept override
4342
230
            {
4343
230
                return nargs;
4344
230
            }
4345
4346
            virtual void ParseValue(const std::vector<std::string> &values_) override
4347
230
            {
4348
230
                values.clear();
4349
4350
230
                for (const std::string &value : values_)
4351
192
                {
4352
192
                    T v {};
4353
#ifdef ARGS_NOEXCEPT
4354
                    if (!reader(name, value, v))
4355
                    {
4356
                        error = Error::Parse;
4357
                        return;
4358
                    }
4359
#else
4360
192
                    reader(name, value, v);
4361
192
#endif
4362
192
                    values.insert(std::end(values), v);
4363
192
                }
4364
230
            }
4365
4366
            List<T> &Get() noexcept
4367
            {
4368
                return values;
4369
            }
4370
4371
            /** Get the value
4372
             */
4373
            List<T> &operator *() noexcept
4374
            {
4375
                return values;
4376
            }
4377
4378
            /** Get the values
4379
             */
4380
            const List<T> &operator *() const noexcept
4381
            {
4382
                return values;
4383
            }
4384
4385
            /** Get the values
4386
             */
4387
            List<T> *operator ->() noexcept
4388
            {
4389
                return &values;
4390
            }
4391
4392
            /** Get the values
4393
             */
4394
            const List<T> *operator ->() const noexcept
4395
            {
4396
                return &values;
4397
            }
4398
4399
            iterator begin() noexcept
4400
92
            {
4401
92
                return values.begin();
4402
92
            }
4403
4404
            const_iterator begin() const noexcept
4405
            {
4406
                return values.begin();
4407
            }
4408
4409
            const_iterator cbegin() const noexcept
4410
            {
4411
                return values.cbegin();
4412
            }
4413
4414
            iterator end() noexcept
4415
92
            {
4416
92
                return values.end();
4417
92
            }
4418
4419
            const_iterator end() const noexcept 
4420
            {
4421
                return values.end();
4422
            }
4423
4424
            const_iterator cend() const noexcept
4425
            {
4426
                return values.cend();
4427
            }
4428
4429
            virtual void Reset() noexcept override
4430
5.40k
            {
4431
5.40k
                FlagBase::Reset();
4432
5.40k
                values = defaultValues;
4433
5.40k
            }
4434
4435
            virtual FlagBase *Match(const EitherFlag &arg) override
4436
2.70k
            {
4437
2.70k
                const bool wasMatched = Matched();
4438
2.70k
                auto me = FlagBase::Match(arg);
4439
2.70k
                if (me && !wasMatched)
4440
92
                {
4441
92
                    values.clear();
4442
92
                }
4443
2.70k
                return me;
4444
2.70k
            }
4445
    };
4446
4447
    /** An argument-accepting flag class that pushes the found values into a list
4448
     * 
4449
     * \tparam T the type to extract the argument as
4450
     * \tparam List the list type that houses the values
4451
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4452
     */
4453
    template <
4454
        typename T,
4455
        template <typename...> class List = detail::vector,
4456
        typename Reader = ValueReader>
4457
    class ValueFlagList : public ValueFlagBase
4458
    {
4459
        private:
4460
            using Container = List<T>;
4461
            Container values;
4462
            const Container defaultValues;
4463
            Reader reader;
4464
4465
        public:
4466
4467
            typedef T value_type;
4468
            typedef typename Container::allocator_type allocator_type;
4469
            typedef typename Container::pointer pointer;
4470
            typedef typename Container::const_pointer const_pointer;
4471
            typedef T& reference;
4472
            typedef const T& const_reference;
4473
            typedef typename Container::size_type size_type;
4474
            typedef typename Container::difference_type difference_type;
4475
            typedef typename Container::iterator iterator;
4476
            typedef typename Container::const_iterator const_iterator;
4477
            typedef std::reverse_iterator<iterator> reverse_iterator;
4478
            typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
4479
4480
            ValueFlagList(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Container &defaultValues_ = Container(), Options options_ = {}):
4481
8.52k
                ValueFlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_)
4482
8.52k
            {
4483
8.52k
                group_.Add(*this);
4484
8.52k
            }
args::ValueFlagList<int, args::detail::vector, args::ValueReader>::ValueFlagList(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, std::__1::vector<int, std::__1::allocator<int> > const&, args::Options)
Line
Count
Source
4481
1.80k
                ValueFlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_)
4482
1.80k
            {
4483
1.80k
                group_.Add(*this);
4484
1.80k
            }
args::ValueFlagList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::detail::vector, args::ValueReader>::ValueFlagList(args::Group&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, args::Matcher&&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, args::Options)
Line
Count
Source
4481
6.72k
                ValueFlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_)
4482
6.72k
            {
4483
6.72k
                group_.Add(*this);
4484
6.72k
            }
4485
4486
8.52k
            virtual ~ValueFlagList() {}
args::ValueFlagList<int, args::detail::vector, args::ValueReader>::~ValueFlagList()
Line
Count
Source
4486
1.80k
            virtual ~ValueFlagList() {}
args::ValueFlagList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::detail::vector, args::ValueReader>::~ValueFlagList()
Line
Count
Source
4486
6.72k
            virtual ~ValueFlagList() {}
4487
4488
            virtual void ParseValue(const std::vector<std::string> &values_) override
4489
592
            {
4490
592
                const std::string &value_ = values_.at(0);
4491
4492
592
                T v{};
4493
#ifdef ARGS_NOEXCEPT
4494
                if (!reader(name, value_, v))
4495
                {
4496
                    error = Error::Parse;
4497
                    return;
4498
                }
4499
#else
4500
592
                reader(name, value_, v);
4501
592
#endif
4502
592
                values.insert(std::end(values), v);
4503
592
            }
args::ValueFlagList<int, args::detail::vector, args::ValueReader>::ParseValue(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
Line
Count
Source
4489
216
            {
4490
216
                const std::string &value_ = values_.at(0);
4491
4492
216
                T v{};
4493
#ifdef ARGS_NOEXCEPT
4494
                if (!reader(name, value_, v))
4495
                {
4496
                    error = Error::Parse;
4497
                    return;
4498
                }
4499
#else
4500
216
                reader(name, value_, v);
4501
216
#endif
4502
216
                values.insert(std::end(values), v);
4503
216
            }
args::ValueFlagList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::detail::vector, args::ValueReader>::ParseValue(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
Line
Count
Source
4489
376
            {
4490
376
                const std::string &value_ = values_.at(0);
4491
4492
376
                T v{};
4493
#ifdef ARGS_NOEXCEPT
4494
                if (!reader(name, value_, v))
4495
                {
4496
                    error = Error::Parse;
4497
                    return;
4498
                }
4499
#else
4500
376
                reader(name, value_, v);
4501
376
#endif
4502
376
                values.insert(std::end(values), v);
4503
376
            }
4504
4505
            /** Get the values
4506
             */
4507
            Container &Get() noexcept
4508
            {
4509
                return values;
4510
            }
4511
4512
            /** Get the value
4513
             */
4514
            Container &operator *() noexcept
4515
            {
4516
                return values;
4517
            }
4518
4519
            /** Get the values
4520
             */
4521
            const Container &operator *() const noexcept
4522
            {
4523
                return values;
4524
            }
4525
4526
            /** Get the values
4527
             */
4528
            Container *operator ->() noexcept
4529
            {
4530
                return &values;
4531
            }
4532
4533
            /** Get the values
4534
             */
4535
            const Container *operator ->() const noexcept
4536
            {
4537
                return &values;
4538
            }
4539
4540
            virtual std::string Name() const override
4541
18.1k
            {
4542
18.1k
                return name + std::string("...");
4543
18.1k
            }
Unexecuted instantiation: args::ValueFlagList<int, args::detail::vector, args::ValueReader>::Name() const
args::ValueFlagList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::detail::vector, args::ValueReader>::Name() const
Line
Count
Source
4541
18.1k
            {
4542
18.1k
                return name + std::string("...");
4543
18.1k
            }
4544
4545
            virtual void Reset() noexcept override
4546
12.1k
            {
4547
12.1k
                ValueFlagBase::Reset();
4548
12.1k
                values = defaultValues;
4549
12.1k
            }
args::ValueFlagList<int, args::detail::vector, args::ValueReader>::Reset()
Line
Count
Source
4546
5.40k
            {
4547
5.40k
                ValueFlagBase::Reset();
4548
5.40k
                values = defaultValues;
4549
5.40k
            }
args::ValueFlagList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::detail::vector, args::ValueReader>::Reset()
Line
Count
Source
4546
6.72k
            {
4547
6.72k
                ValueFlagBase::Reset();
4548
6.72k
                values = defaultValues;
4549
6.72k
            }
4550
4551
            virtual FlagBase *Match(const EitherFlag &arg) override
4552
6.16k
            {
4553
6.16k
                const bool wasMatched = Matched();
4554
6.16k
                auto me = FlagBase::Match(arg);
4555
6.16k
                if (me && !wasMatched)
4556
450
                {
4557
450
                    values.clear();
4558
450
                }
4559
6.16k
                return me;
4560
6.16k
            }
args::ValueFlagList<int, args::detail::vector, args::ValueReader>::Match(args::EitherFlag const&)
Line
Count
Source
4552
2.92k
            {
4553
2.92k
                const bool wasMatched = Matched();
4554
2.92k
                auto me = FlagBase::Match(arg);
4555
2.92k
                if (me && !wasMatched)
4556
153
                {
4557
153
                    values.clear();
4558
153
                }
4559
2.92k
                return me;
4560
2.92k
            }
args::ValueFlagList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, args::detail::vector, args::ValueReader>::Match(args::EitherFlag const&)
Line
Count
Source
4552
3.23k
            {
4553
3.23k
                const bool wasMatched = Matched();
4554
3.23k
                auto me = FlagBase::Match(arg);
4555
3.23k
                if (me && !wasMatched)
4556
297
                {
4557
297
                    values.clear();
4558
297
                }
4559
3.23k
                return me;
4560
3.23k
            }
4561
4562
            iterator begin() noexcept
4563
153
            {
4564
153
                return values.begin();
4565
153
            }
4566
4567
            const_iterator begin() const noexcept
4568
            {
4569
                return values.begin();
4570
            }
4571
4572
            const_iterator cbegin() const noexcept
4573
            {
4574
                return values.cbegin();
4575
            }
4576
4577
            iterator end() noexcept
4578
153
            {
4579
153
                return values.end();
4580
153
            }
4581
4582
            const_iterator end() const noexcept 
4583
            {
4584
                return values.end();
4585
            }
4586
4587
            const_iterator cend() const noexcept
4588
            {
4589
                return values.cend();
4590
            }
4591
    };
4592
4593
    /** A mapping value flag class
4594
     * 
4595
     * \tparam K the type to extract the argument as
4596
     * \tparam T the type to store the result as
4597
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4598
     * \tparam Map The Map type.  Should operate like std::map or std::unordered_map
4599
     */
4600
    template <
4601
        typename K,
4602
        typename T,
4603
        typename Reader = ValueReader,
4604
        template <typename...> class Map = detail::unordered_map>
4605
    class MapFlag : public ValueFlagBase
4606
    {
4607
        private:
4608
            const Map<K, T> map;
4609
            T value;
4610
            const T defaultValue;
4611
            Reader reader;
4612
4613
        protected:
4614
            virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
4615
27.0k
            {
4616
27.0k
                return detail::MapKeysToStrings(map);
4617
27.0k
            }
4618
4619
        public:
4620
4621
11.0k
            MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), map(map_), value(defaultValue_), defaultValue(defaultValue_)
4622
11.0k
            {
4623
11.0k
                group_.Add(*this);
4624
11.0k
            }
4625
4626
11.0k
            MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const T &defaultValue_ = T(), const bool extraError_ = false): MapFlag(group_, name_, help_, std::move(matcher_), map_, defaultValue_, extraError_ ? Options::Single : Options::None)
4627
11.0k
            {
4628
11.0k
            }
4629
4630
            MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, Options options_): MapFlag(group_, name_, help_, std::move(matcher_), map_, T(), options_)
4631
            {
4632
            }
4633
4634
11.0k
            virtual ~MapFlag() {}
4635
4636
            virtual void ParseValue(const std::vector<std::string> &values_) override
4637
280
            {
4638
280
                const std::string &value_ = values_.at(0);
4639
4640
280
                K key{};
4641
#ifdef ARGS_NOEXCEPT
4642
                if (!reader(name, value_, key))
4643
                {
4644
                    error = Error::Parse;
4645
                    return;
4646
                }
4647
#else
4648
280
                reader(name, value_, key);
4649
280
#endif
4650
280
                auto it = map.find(key);
4651
280
                if (it == std::end(map))
4652
206
                {
4653
206
                    std::ostringstream problem;
4654
206
                    problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
4655
#ifdef ARGS_NOEXCEPT
4656
                    error = Error::Map;
4657
                    errorMsg = problem.str();
4658
#else
4659
206
                    throw MapError(problem.str());
4660
206
#endif
4661
206
                } else
4662
74
                {
4663
74
                    this->value = it->second;
4664
74
                }
4665
280
            }
4666
4667
            /** Get the value
4668
             */
4669
            T &Get() noexcept
4670
            {
4671
                return value;
4672
            }
4673
4674
            /** Get the value
4675
             */
4676
            T &operator *() noexcept
4677
53
            {
4678
53
                return value;
4679
53
            }
4680
4681
            /** Get the value
4682
             */
4683
            const T &operator *() const noexcept
4684
            {
4685
                return value;
4686
            }
4687
4688
            /** Get the value
4689
             */
4690
            T *operator ->() noexcept
4691
            {
4692
                return &value;
4693
            }
4694
4695
            /** Get the value
4696
             */
4697
            const T *operator ->() const noexcept
4698
            {
4699
                return &value;
4700
            }
4701
4702
            virtual void Reset() noexcept override
4703
17.1k
            {
4704
17.1k
                ValueFlagBase::Reset();
4705
17.1k
                value = defaultValue;
4706
17.1k
            }
4707
    };
4708
4709
    /** A mapping value flag list class
4710
     * 
4711
     * \tparam K the type to extract the argument as
4712
     * \tparam T the type to store the result as
4713
     * \tparam List the list type that houses the values
4714
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4715
     * \tparam Map The Map type.  Should operate like std::map or std::unordered_map
4716
     */
4717
    template <
4718
        typename K,
4719
        typename T,
4720
        template <typename...> class List = detail::vector,
4721
        typename Reader = ValueReader,
4722
        template <typename...> class Map = detail::unordered_map>
4723
    class MapFlagList : public ValueFlagBase
4724
    {
4725
        private:
4726
            using Container = List<T>;
4727
            const Map<K, T> map;
4728
            Container values;
4729
            const Container defaultValues;
4730
            Reader reader;
4731
4732
        protected:
4733
            virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
4734
0
            {
4735
0
                return detail::MapKeysToStrings(map);
4736
0
            }
4737
4738
        public:
4739
            typedef T value_type;
4740
            typedef typename Container::allocator_type allocator_type;
4741
            typedef typename Container::pointer pointer;
4742
            typedef typename Container::const_pointer const_pointer;
4743
            typedef T& reference;
4744
            typedef const T& const_reference;
4745
            typedef typename Container::size_type size_type;
4746
            typedef typename Container::difference_type difference_type;
4747
            typedef typename Container::iterator iterator;
4748
            typedef typename Container::const_iterator const_iterator;
4749
            typedef std::reverse_iterator<iterator> reverse_iterator;
4750
            typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
4751
4752
            MapFlagList(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const Container &defaultValues_ = Container(), Options options_ = {}):
4753
1.80k
                ValueFlagBase(name_, help_, std::move(matcher_), options_), map(map_), values(defaultValues_), defaultValues(defaultValues_)
4754
1.80k
            {
4755
1.80k
                group_.Add(*this);
4756
1.80k
            }
4757
4758
1.80k
            virtual ~MapFlagList() {}
4759
4760
            virtual void ParseValue(const std::vector<std::string> &values_) override
4761
237
            {
4762
237
                const std::string &value_ = values_.at(0);
4763
4764
237
                K key{};
4765
#ifdef ARGS_NOEXCEPT
4766
                if (!reader(name, value_, key))
4767
                {
4768
                    error = Error::Parse;
4769
                    return;
4770
                }
4771
#else
4772
237
                reader(name, value_, key);
4773
237
#endif
4774
237
                auto it = map.find(key);
4775
237
                if (it == std::end(map))
4776
18
                {
4777
18
                    std::ostringstream problem;
4778
18
                    problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
4779
#ifdef ARGS_NOEXCEPT
4780
                    error = Error::Map;
4781
                    errorMsg = problem.str();
4782
#else
4783
18
                    throw MapError(problem.str());
4784
18
#endif
4785
18
                } else
4786
219
                {
4787
219
                    detail::AppendMappedValue(values, it->second, 0);
4788
219
                }
4789
237
            }
4790
4791
            /** Get the value
4792
             */
4793
            Container &Get() noexcept
4794
            {
4795
                return values;
4796
            }
4797
4798
            /** Get the value
4799
             */
4800
            Container &operator *() noexcept
4801
            {
4802
                return values;
4803
            }
4804
4805
            /** Get the values
4806
             */
4807
            const Container &operator *() const noexcept
4808
            {
4809
                return values;
4810
            }
4811
4812
            /** Get the values
4813
             */
4814
            Container *operator ->() noexcept
4815
            {
4816
                return &values;
4817
            }
4818
4819
            /** Get the values
4820
             */
4821
            const Container *operator ->() const noexcept
4822
            {
4823
                return &values;
4824
            }
4825
4826
            virtual std::string Name() const override
4827
0
            {
4828
0
                return name + std::string("...");
4829
0
            }
4830
4831
            virtual void Reset() noexcept override
4832
5.40k
            {
4833
5.40k
                ValueFlagBase::Reset();
4834
5.40k
                values = defaultValues;
4835
5.40k
            }
4836
4837
            virtual FlagBase *Match(const EitherFlag &arg) override
4838
2.39k
            {
4839
2.39k
                const bool wasMatched = Matched();
4840
2.39k
                auto me = FlagBase::Match(arg);
4841
2.39k
                if (me && !wasMatched)
4842
53
                {
4843
53
                    values.clear();
4844
53
                }
4845
2.39k
                return me;
4846
2.39k
            }
4847
4848
            iterator begin() noexcept
4849
53
            {
4850
53
                return values.begin();
4851
53
            }
4852
4853
            const_iterator begin() const noexcept
4854
            {
4855
                return values.begin();
4856
            }
4857
4858
            const_iterator cbegin() const noexcept
4859
            {
4860
                return values.cbegin();
4861
            }
4862
4863
            iterator end() noexcept
4864
53
            {
4865
53
                return values.end();
4866
53
            }
4867
4868
            const_iterator end() const noexcept 
4869
            {
4870
                return values.end();
4871
            }
4872
4873
            const_iterator cend() const noexcept
4874
            {
4875
                return values.cend();
4876
            }
4877
    };
4878
4879
    /** A positional argument class
4880
     *
4881
     * \tparam T the type to extract the argument as
4882
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4883
     */
4884
    template <
4885
        typename T,
4886
        typename Reader = ValueReader>
4887
    class Positional : public PositionalBase
4888
    {
4889
        private:
4890
            T value;
4891
            const T defaultValue;
4892
            Reader reader;
4893
        public:
4894
11.1k
            Positional(Group &group_, const std::string &name_, const std::string &help_, const T &defaultValue_ = T(), Options options_ = {}): PositionalBase(name_, help_, options_), value(defaultValue_), defaultValue(defaultValue_)
4895
11.1k
            {
4896
11.1k
                group_.Add(*this);
4897
11.1k
            }
4898
4899
            Positional(Group &group_, const std::string &name_, const std::string &help_, Options options_): Positional(group_, name_, help_, T(), options_)
4900
            {
4901
            }
4902
4903
11.1k
            virtual ~Positional() {}
4904
4905
            virtual void ParseValue(const std::string &value_) override
4906
4.76k
            {
4907
#ifdef ARGS_NOEXCEPT
4908
                if (!reader(name, value_, this->value))
4909
                {
4910
                    error = Error::Parse;
4911
                    return;
4912
                }
4913
#else
4914
4.76k
                reader(name, value_, this->value);
4915
4.76k
#endif
4916
4.76k
                ready = false;
4917
4.76k
                matched = true;
4918
4.76k
            }
4919
4920
            /** Get the value
4921
             */
4922
            T &Get() noexcept
4923
            {
4924
                return value;
4925
            }
4926
4927
            /** Get the value
4928
             */
4929
            T &operator *() noexcept
4930
1.25k
            {
4931
1.25k
                return value;
4932
1.25k
            }
4933
4934
            /** Get the value
4935
             */
4936
            const T &operator *() const noexcept
4937
            {
4938
                return value;
4939
            }
4940
4941
            /** Get the value
4942
             */
4943
            T *operator ->() noexcept
4944
            {
4945
                return &value;
4946
            }
4947
4948
            /** Get the value
4949
             */
4950
            const T *operator ->() const noexcept
4951
            {
4952
                return &value;
4953
            }
4954
4955
            virtual void Reset() noexcept override
4956
17.2k
            {
4957
17.2k
                PositionalBase::Reset();
4958
17.2k
                value = defaultValue;
4959
17.2k
            }
4960
    };
4961
4962
    /** A positional argument class that pushes the found values into a list
4963
     * 
4964
     * \tparam T the type to extract the argument as
4965
     * \tparam List the list type that houses the values
4966
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
4967
     */
4968
    template <
4969
        typename T,
4970
        template <typename...> class List = detail::vector,
4971
        typename Reader = ValueReader>
4972
    class PositionalList : public PositionalBase
4973
    {
4974
        private:
4975
            using Container = List<T>;
4976
            Container values;
4977
            const Container defaultValues;
4978
            Reader reader;
4979
4980
        public:
4981
            typedef T value_type;
4982
            typedef typename Container::allocator_type allocator_type;
4983
            typedef typename Container::pointer pointer;
4984
            typedef typename Container::const_pointer const_pointer;
4985
            typedef T& reference;
4986
            typedef const T& const_reference;
4987
            typedef typename Container::size_type size_type;
4988
            typedef typename Container::difference_type difference_type;
4989
            typedef typename Container::iterator iterator;
4990
            typedef typename Container::const_iterator const_iterator;
4991
            typedef std::reverse_iterator<iterator> reverse_iterator;
4992
            typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
4993
4994
10.5k
            PositionalList(Group &group_, const std::string &name_, const std::string &help_, const Container &defaultValues_ = Container(), Options options_ = {}): PositionalBase(name_, help_, options_), values(defaultValues_), defaultValues(defaultValues_)
4995
10.5k
            {
4996
10.5k
                group_.Add(*this);
4997
10.5k
            }
4998
4999
1.83k
            PositionalList(Group &group_, const std::string &name_, const std::string &help_, Options options_): PositionalList(group_, name_, help_, {}, options_)
5000
1.83k
            {
5001
1.83k
            }
5002
5003
10.5k
            virtual ~PositionalList() {}
5004
5005
            virtual void ParseValue(const std::string &value_) override
5006
6.42k
            {
5007
6.42k
                T v{};
5008
#ifdef ARGS_NOEXCEPT
5009
                if (!reader(name, value_, v))
5010
                {
5011
                    error = Error::Parse;
5012
                    return;
5013
                }
5014
#else
5015
6.42k
                reader(name, value_, v);
5016
6.42k
#endif
5017
6.42k
                values.insert(std::end(values), v);
5018
6.42k
                matched = true;
5019
6.42k
            }
5020
5021
            virtual std::string Name() const override
5022
54.1k
            {
5023
54.1k
                return name + std::string("...");
5024
54.1k
            }
5025
5026
            /** Get the values
5027
             */
5028
            Container &Get() noexcept
5029
            {
5030
                return values;
5031
            }
5032
5033
            /** Get the value
5034
             */
5035
            Container &operator *() noexcept
5036
            {
5037
                return values;
5038
            }
5039
5040
            /** Get the values
5041
             */
5042
            const Container &operator *() const noexcept
5043
            {
5044
                return values;
5045
            }
5046
5047
            /** Get the values
5048
             */
5049
            Container *operator ->() noexcept
5050
158
            {
5051
158
                return &values;
5052
158
            }
5053
5054
            /** Get the values
5055
             */
5056
            const Container *operator ->() const noexcept
5057
            {
5058
                return &values;
5059
            }
5060
5061
            virtual void Reset() noexcept override
5062
15.9k
            {
5063
15.9k
                PositionalBase::Reset();
5064
15.9k
                values = defaultValues;
5065
15.9k
            }
5066
5067
            virtual PositionalBase *GetNextPositional() override
5068
6.42k
            {
5069
6.42k
                const bool wasMatched = Matched();
5070
6.42k
                auto me = PositionalBase::GetNextPositional();
5071
6.42k
                if (me && !wasMatched)
5072
1.58k
                {
5073
1.58k
                    values.clear();
5074
1.58k
                }
5075
6.42k
                return me;
5076
6.42k
            }
5077
5078
            iterator begin() noexcept
5079
146
            {
5080
146
                return values.begin();
5081
146
            }
5082
5083
            const_iterator begin() const noexcept
5084
            {
5085
                return values.begin();
5086
            }
5087
5088
            const_iterator cbegin() const noexcept
5089
            {
5090
                return values.cbegin();
5091
            }
5092
5093
            iterator end() noexcept
5094
146
            {
5095
146
                return values.end();
5096
146
            }
5097
5098
            const_iterator end() const noexcept 
5099
            {
5100
                return values.end();
5101
            }
5102
5103
            const_iterator cend() const noexcept
5104
            {
5105
                return values.cend();
5106
            }
5107
    };
5108
5109
    /** A positional argument mapping class
5110
     * 
5111
     * \tparam K the type to extract the argument as
5112
     * \tparam T the type to store the result as
5113
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
5114
     * \tparam Map The Map type.  Should operate like std::map or std::unordered_map
5115
     */
5116
    template <
5117
        typename K,
5118
        typename T,
5119
        typename Reader = ValueReader,
5120
        template <typename...> class Map = detail::unordered_map>
5121
    class MapPositional : public PositionalBase
5122
    {
5123
        private:
5124
            const Map<K, T> map;
5125
            T value;
5126
            const T defaultValue;
5127
            Reader reader;
5128
5129
        protected:
5130
            virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
5131
27
            {
5132
27
                return detail::MapKeysToStrings(map);
5133
27
            }
5134
5135
        public:
5136
5137
            MapPositional(Group &group_, const std::string &name_, const std::string &help_, const Map<K, T> &map_, const T &defaultValue_ = T(), Options options_ = {}):
5138
4.29k
                PositionalBase(name_, help_, options_), map(map_), value(defaultValue_), defaultValue(defaultValue_)
5139
4.29k
            {
5140
4.29k
                group_.Add(*this);
5141
4.29k
            }
5142
5143
4.29k
            virtual ~MapPositional() {}
5144
5145
            virtual void ParseValue(const std::string &value_) override
5146
1.24k
            {
5147
1.24k
                K key{};
5148
#ifdef ARGS_NOEXCEPT
5149
                if (!reader(name, value_, key))
5150
                {
5151
                    error = Error::Parse;
5152
                    return;
5153
                }
5154
#else
5155
1.24k
                reader(name, value_, key);
5156
1.24k
#endif
5157
1.24k
                auto it = map.find(key);
5158
1.24k
                if (it == std::end(map))
5159
1.19k
                {
5160
1.19k
                    std::ostringstream problem;
5161
1.19k
                    problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
5162
#ifdef ARGS_NOEXCEPT
5163
                    error = Error::Map;
5164
                    errorMsg = problem.str();
5165
#else
5166
1.19k
                    throw MapError(problem.str());
5167
1.19k
#endif
5168
1.19k
                } else
5169
56
                {
5170
56
                    this->value = it->second;
5171
56
                    ready = false;
5172
56
                    matched = true;
5173
56
                }
5174
1.24k
            }
5175
5176
            /** Get the value
5177
             */
5178
            T &Get() noexcept
5179
            {
5180
                return value;
5181
            }
5182
5183
            /** Get the value
5184
             */
5185
            T &operator *() noexcept
5186
34
            {
5187
34
                return value;
5188
34
            }
5189
5190
            /** Get the value
5191
             */
5192
            const T &operator *() const noexcept
5193
            {
5194
                return value;
5195
            }
5196
5197
            /** Get the value
5198
             */
5199
            T *operator ->() noexcept
5200
            {
5201
                return &value;
5202
            }
5203
5204
            /** Get the value
5205
             */
5206
            const T *operator ->() const noexcept
5207
            {
5208
                return &value;
5209
            }
5210
5211
            virtual void Reset() noexcept override
5212
10.3k
            {
5213
10.3k
                PositionalBase::Reset();
5214
10.3k
                value = defaultValue;
5215
10.3k
            }
5216
    };
5217
5218
    /** A positional argument mapping list class
5219
     * 
5220
     * \tparam K the type to extract the argument as
5221
     * \tparam T the type to store the result as
5222
     * \tparam List the list type that houses the values
5223
     * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
5224
     * \tparam Map The Map type.  Should operate like std::map or std::unordered_map
5225
     */
5226
    template <
5227
        typename K,
5228
        typename T,
5229
        template <typename...> class List = detail::vector,
5230
        typename Reader = ValueReader,
5231
        template <typename...> class Map = detail::unordered_map>
5232
    class MapPositionalList : public PositionalBase
5233
    {
5234
        private:
5235
            using Container = List<T>;
5236
5237
            const Map<K, T> map;
5238
            Container values;
5239
            const Container defaultValues;
5240
            Reader reader;
5241
5242
        protected:
5243
            virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
5244
            {
5245
                return detail::MapKeysToStrings(map);
5246
            }
5247
5248
        public:
5249
            typedef T value_type;
5250
            typedef typename Container::allocator_type allocator_type;
5251
            typedef typename Container::pointer pointer;
5252
            typedef typename Container::const_pointer const_pointer;
5253
            typedef T& reference;
5254
            typedef const T& const_reference;
5255
            typedef typename Container::size_type size_type;
5256
            typedef typename Container::difference_type difference_type;
5257
            typedef typename Container::iterator iterator;
5258
            typedef typename Container::const_iterator const_iterator;
5259
            typedef std::reverse_iterator<iterator> reverse_iterator;
5260
            typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
5261
5262
            MapPositionalList(Group &group_, const std::string &name_, const std::string &help_, const Map<K, T> &map_, const Container &defaultValues_ = Container(), Options options_ = {}):
5263
                PositionalBase(name_, help_, options_), map(map_), values(defaultValues_), defaultValues(defaultValues_)
5264
            {
5265
                group_.Add(*this);
5266
            }
5267
5268
            virtual ~MapPositionalList() {}
5269
5270
            virtual void ParseValue(const std::string &value_) override
5271
            {
5272
                K key{};
5273
#ifdef ARGS_NOEXCEPT
5274
                if (!reader(name, value_, key))
5275
                {
5276
                    error = Error::Parse;
5277
                    return;
5278
                }
5279
#else
5280
                reader(name, value_, key);
5281
#endif
5282
                auto it = map.find(key);
5283
                if (it == std::end(map))
5284
                {
5285
                    std::ostringstream problem;
5286
                    problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
5287
#ifdef ARGS_NOEXCEPT
5288
                    error = Error::Map;
5289
                    errorMsg = problem.str();
5290
#else
5291
                    throw MapError(problem.str());
5292
#endif
5293
                } else
5294
                {
5295
                    detail::AppendMappedValue(values, it->second, 0);
5296
                    matched = true;
5297
                }
5298
            }
5299
5300
            /** Get the value
5301
             */
5302
            Container &Get() noexcept
5303
            {
5304
                return values;
5305
            }
5306
5307
            /** Get the value
5308
             */
5309
            Container &operator *() noexcept
5310
            {
5311
                return values;
5312
            }
5313
5314
            /** Get the values
5315
             */
5316
            const Container &operator *() const noexcept
5317
            {
5318
                return values;
5319
            }
5320
5321
            /** Get the values
5322
             */
5323
            Container *operator ->() noexcept
5324
            {
5325
                return &values;
5326
            }
5327
5328
            /** Get the values
5329
             */
5330
            const Container *operator ->() const noexcept
5331
            {
5332
                return &values;
5333
            }
5334
5335
            virtual std::string Name() const override
5336
            {
5337
                return name + std::string("...");
5338
            }
5339
5340
            virtual void Reset() noexcept override
5341
            {
5342
                PositionalBase::Reset();
5343
                values = defaultValues;
5344
            }
5345
5346
            virtual PositionalBase *GetNextPositional() override
5347
            {
5348
                const bool wasMatched = Matched();
5349
                auto me = PositionalBase::GetNextPositional();
5350
                if (me && !wasMatched)
5351
                {
5352
                    values.clear();
5353
                }
5354
                return me;
5355
            }
5356
5357
            iterator begin() noexcept
5358
            {
5359
                return values.begin();
5360
            }
5361
5362
            const_iterator begin() const noexcept
5363
            {
5364
                return values.begin();
5365
            }
5366
5367
            const_iterator cbegin() const noexcept
5368
            {
5369
                return values.cbegin();
5370
            }
5371
5372
            iterator end() noexcept
5373
            {
5374
                return values.end();
5375
            }
5376
5377
            const_iterator end() const noexcept 
5378
            {
5379
                return values.end();
5380
            }
5381
5382
            const_iterator cend() const noexcept
5383
            {
5384
                return values.cend();
5385
            }
5386
    };
5387
}
5388
5389
#pragma pop_macro("min")
5390
#pragma pop_macro("max")
5391
#endif