aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/programoptions.cpp
blob: 9ea7c1648d14d6457dad69cc80d6890761365d2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "programoptions.h"
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/exceptions.h>
#include <boost/lexical_cast.hpp>
#include <cassert>

#include <vespa/log/log.h>
LOG_SETUP(".programoptions");

namespace vespalib {

VESPA_IMPLEMENT_EXCEPTION(InvalidCommandLineArgumentsException, Exception);

namespace {

    std::string UNSET_TOKEN = "-_-/#UNSET#\\-_-";

    // Use tokenizer instead when moved from document
    std::vector<std::string> splitString(const std::string& source, char split)
    {
        std::vector<std::string> target;
        std::string::size_type start = 0;
        std::string::size_type stop = source.find(split);
        while (stop != std::string::npos) {
            target.push_back(source.substr(start, stop - start));
            start = stop + 1;
            stop = source.find(split, start);
        }
        target.push_back(source.substr(start));
        return target;
    }

}

template<typename Number>
std::string ProgramOptions::NumberOptionParser<Number>::getStringValue(Number n)
{
    std::ostringstream ost;
    ost << n;
    return ost.str();
}

template<typename Number>
void ProgramOptions::NumberOptionParser<Number>::set(const std::vector<std::string>& arguments)
{
    try{
        _number = boost::lexical_cast<Number>(arguments[0]);
    } catch (const boost::bad_lexical_cast& e) {
        std::ostringstream ost;
        ost << "The argument '" << arguments[0]
            << "' can not be interpreted as a number of type "
            << getTypeName<Number>() << ".";
        throw InvalidCommandLineArgumentsException(
                ost.str(), VESPA_STRLOC);
    }
}

ProgramOptions::FlagOptionParser::~FlagOptionParser() = default;

ProgramOptions::StringOptionParser::~StringOptionParser() = default;

ProgramOptions::OptionParser::OptionParser(
        const std::string& nameList, uint32_t argCount, const std::string& desc)
    : _names(splitString(nameList, ' ')),
      _hiddenNames(),
      _argCount(argCount),
      _argTypes(argCount),
      _hasDefault(false),
      _invalidDefault(false),
      _defaultString(),
      _description(desc)
{
    if (nameList == "") _names.clear();
}

ProgramOptions::OptionParser::OptionParser(
        const std::string& nameList, uint32_t argCount,
        const std::string& defString, const std::string& desc)
    : _names(splitString(nameList, ' ')),
      _hiddenNames(),
      _argCount(argCount),
      _argTypes(argCount),
      _hasDefault(true),
      _invalidDefault(false),
      _defaultString(defString),
      _description(desc)
{ }

ProgramOptions::OptionParser::~OptionParser() { }

void
ProgramOptions::OptionParser::setInvalidDefault()
{
    _invalidDefault = true;
}

std::string ProgramOptions::OptionParser::getOptSyntaxString() const
{
    std::ostringstream ost;
    for (uint32_t i=0; i<_names.size(); ++i) {
        ost << (_names[i].size() == 1 ? " -" : " --");
        ost << _names[i];
    }
    for (uint32_t i=0; i<_argCount; ++i) {
        std::string type = (_argTypes[i] != "" ? _argTypes[i] : getArgType(i));
        ost << " <" << type << ">";
    }
    return ost.str();
}

ProgramOptions::ProgramOptions()
    : _argc(0),
      _argv(0),
      _options(),
      _optionMap(),
      _setOptions(),
      _syntaxMessage(),
      _maxLeftColumnSize(30),
      _defaultsSet(false)
{ }

ProgramOptions::ProgramOptions(int argc, const char* const* argv)
    : _argc(argc),
      _argv(argv),
      _options(),
      _optionMap(),
      _setOptions(),
      _syntaxMessage(),
      _maxLeftColumnSize(30),
      _defaultsSet(false)
{ }

ProgramOptions::~ProgramOptions() { }

void
ProgramOptions::clear() {
    _configurables.clear();
    _options.clear();
    _optionMap.clear();
    _setOptions.clear();
    _arguments.clear();
}

void
ProgramOptions::setCommandLineArguments(int argc, const char* const* argv)
{
    _argc = argc;
    _argv = argv;
}

void
ProgramOptions::setSyntaxMessage(const std::string& msg)
{
    _syntaxMessage = msg;
}

void
ProgramOptions::addHiddenIdentifiers(const std::string& optionNameList)
{
    if (_options.size() == 0) {
        throw InvalidCommandLineArgumentsException(
                "Cannot add hidden identifier to last "
                "option as no option has been added yet.", VESPA_STRLOC);
    }
    OptionParser::SP opt = _options.back();
    if (opt->isHeader()) {
        throw InvalidCommandLineArgumentsException(
                "Cannot add option arguments to option header.", VESPA_STRLOC);
    }
    std::vector<std::string> newIds(splitString(optionNameList, ' '));
    for (uint32_t i=0; i<newIds.size(); ++i) {
        std::map<std::string, OptionParser::SP>::const_iterator it(
                _optionMap.find(newIds[i]));
        if (it != _optionMap.end()) {
            throw InvalidCommandLineArgumentsException(
                    "Option '" + newIds[i] + "' is already registered.",
                    VESPA_STRLOC);
        }
    }
    for (uint32_t i=0; i<newIds.size(); ++i) {
        _optionMap[newIds[i]] = opt;
        opt->_hiddenNames.push_back(newIds[i]);
    }
}

void
ProgramOptions::setArgumentTypeName(const std::string& name, uint32_t index)
{
    if (_options.size() == 0) {
        throw InvalidCommandLineArgumentsException(
                "Cannot add hidden identifier to last "
                "option as no option has been added yet.", VESPA_STRLOC);
    }
    OptionParser::SP opt = _options.back();
    if (opt->isHeader()) {
        throw InvalidCommandLineArgumentsException(
                "Cannot add option arguments to option header.", VESPA_STRLOC);
    }
    opt->_argTypes[index] = name;
}

void
ProgramOptions::addOptionHeader(const std::string& description)
{
    _options.push_back(OptionParser::SP(new OptionHeader(description)));
}

namespace {
    bool isNumber(const std::string& arg) {
        if (arg.size() > 1 && arg[0] == '-'
            && arg[1] >= '0' && arg[1] <= '9')
        {
            return true;
        }
        return false;
    }
}

void
ProgramOptions::parse()
{
    try{
        std::ostringstream ost;
        ost << "Parsing options:\n";
        for (int i=0; i<_argc; ++i) {
            ost << "  " << i << ": '" << _argv[i] << "'\n";
        }
        LOG(debug, "%s", ost.str().c_str());
        uint32_t argPos = 0;
        uint32_t optPos = 1;
        for (; optPos < static_cast<uint32_t>(_argc); ++optPos) {
            std::string s(_argv[optPos]);
                // Skip arguments
            if (s.size() < 2 || s[0] != '-' || s == "--" || isNumber(s)) {
                if (argPos <= optPos) { // No more options to parse
                    break;
                } else { // This has already been consumed as argument
                    continue;
                }
            }
            if (argPos <= optPos) { argPos = optPos + 1; }
            if (s.substr(0, 2) == "--") {
                std::string id(s.substr(2));
                LOG(debug, "Parsing long option %s at pos %u, arg pos is now "
                           "%u", id.c_str(), optPos, argPos);
                std::map<std::string, OptionParser::SP>::const_iterator it(
                        _optionMap.find(id));
                if (it == _optionMap.end()) {
                    throw InvalidCommandLineArgumentsException(
                            "Invalid option '" + id + "'.", VESPA_STRLOC);
                }
                parseOption(id, *it->second, argPos);
                _setOptions.insert(it->second);
            } else {
                LOG(debug, "Parsing short options %s at pos %u, arg pos is now "
                           "%u.", s.c_str() + 1, optPos, argPos);
                for (uint32_t shortPos = 1; shortPos < s.size(); ++shortPos) {
                    std::string id(s.substr(shortPos, 1));
                    LOG(debug, "Parsing short option %s, arg pos is %u.",
                               id.c_str(), argPos);
                    std::map<std::string, OptionParser::SP>::const_iterator it(
                            _optionMap.find(id));
                    if (it == _optionMap.end()) {
                        throw InvalidCommandLineArgumentsException(
                                "Invalid option '" + id + "'.", VESPA_STRLOC);
                    }
                    parseOption(id, *it->second, argPos);
                    _setOptions.insert(it->second);
                }
            }
        }
        if (!_defaultsSet) setDefaults(true);
        for (uint32_t i=0; i<_arguments.size(); ++i) {
            OptionParser& opt(*_arguments[i]);
            if (opt._argCount == 0) {
                LOG(debug, "Parsing list argument %s. Pos is %u.",
                        opt.getArgName().c_str(), optPos);
                std::vector<std::string> arguments;
                for (uint32_t j=optPos; j<static_cast<uint32_t>(_argc); ++j) {
                    arguments.push_back(_argv[j]);
                }
                opt.set(arguments);
                optPos = _argc;
                LOG(debug, "Done. Pos is now %u.", optPos);
            } else if (optPos + opt._argCount > static_cast<uint32_t>(_argc)) {
                if (!opt.isRequired()) {
                    LOG(debug, "Setting default for argument %u.", i);
                    opt.setDefault();
                } else {
                    throw InvalidCommandLineArgumentsException(
                        "Insufficient data is given to set required argument '"
                        + opt.getArgName() + "'.", VESPA_STRLOC);
                }
            } else {
                parseArgument(opt, optPos);
            }
        }
    } catch (const vespalib::Exception& e) {
        throw;
    }
    for (uint32_t i=0; i<_configurables.size(); ++i) {
        _configurables[i]->finalizeOptions();
    }
}

void
ProgramOptions::setDefaults(bool failUnsetRequired)
{
    for (uint32_t i=0; i<_options.size(); ++i) {
        OptionParser::SP opt(_options[i]);
        if (opt->isHeader()) {
            continue;
        }
        std::set<OptionParser::SP>::const_iterator it(_setOptions.find(opt));
        if (it == _setOptions.end()) {
            if (opt->_hasDefault) {
                opt->setDefault();
            } else if (failUnsetRequired) {
                throw InvalidCommandLineArgumentsException(
                    "Option '" + opt->_names[0]
                    + "' has no default and must be set.", VESPA_STRLOC);
            }
        }
    }
    _defaultsSet = true;
}

void
ProgramOptions::parseOption(
        const std::string& id, OptionParser& opt, uint32_t& argPos)
{
    LOG(debug, "Parsing option %s. Argpos is %u.", id.c_str(), argPos);
    std::vector<std::string> arguments;
    for (; arguments.size() != opt._argCount; ++argPos) {
        if (argPos >= static_cast<uint32_t>(_argc)) {
            throw InvalidCommandLineArgumentsException(
                    vespalib::make_string(
                        "Option '%s' needs %u arguments. Only %u available.",
                        id.c_str(), opt._argCount, (uint32_t) arguments.size()),
                    VESPA_STRLOC);
        }
        if (strlen(_argv[argPos]) >= 2 && _argv[argPos][0] == '-'
            && !isNumber(_argv[argPos]))
        {
            continue;
        }
        arguments.push_back(_argv[argPos]);
    }
    opt.set(arguments);
    LOG(debug, "Done. Argpos is now %u.", argPos);
}

void
ProgramOptions::parseArgument(OptionParser& opt, uint32_t& pos)
{
    LOG(debug, "Parsing argument %s. Pos is %u.",
        opt.getArgName().c_str(), pos);
    std::vector<std::string> arguments;
    for (; arguments.size() != opt._argCount; ++pos) {
        assert(pos < static_cast<uint32_t>(_argc));
        arguments.push_back(_argv[pos]);
    }
    opt.set(arguments);
    LOG(debug, "Done. Pos is now %u.", pos);
}

namespace {
    std::vector<std::string> breakText(const std::vector<std::string>& source,
                                       uint32_t maxLen,
                                       int preserveWordSpaceLimit = -1)
    {
        if (preserveWordSpaceLimit < 0) {
            preserveWordSpaceLimit = maxLen / 5;
        }
        std::vector<std::string> result;
        for (uint32_t i=0; i<source.size(); ++i) {
            std::vector<std::string> split(splitString(source[i], '\n'));
            for (uint32_t j=0; j<split.size(); ++j) {
                    // Process each line of input here to possible break
                    // it.
                std::string line = split[j];
                while (true) {
                        // If the line is already short enough, we're done.
                    if (line.size() <= maxLen) {
                        result.push_back(line);
                        break;
                    }
                        // Otherwise, find the last space before max len
                    std::string::size_type pos(
                            line.rfind(" ", maxLen));
                    if (pos != std::string::npos
                        && pos > maxLen - preserveWordSpaceLimit)
                    {
                            // If the space comes late enough, add the line up
                            // to that point, and let the remainder go to a new
                            // line.
                        result.push_back(line.substr(0, pos));
                        line = line.substr(pos+1);
                    } else {
                            // If the space is not late enough, force break
                            // inside a word and add a dash.
                        result.push_back(line.substr(0, maxLen - 1) + "-");
                        line = line.substr(maxLen - 1);
                    }
                }
            }
        }
        return result;
    }

    std::vector<std::string> breakText(const std::string& source,
                                       uint32_t maxLen,
                                       int preserveWordSpaceLimit = -1)
    {
        std::vector<std::string> v(1);
        v[0] = source;
        return breakText(v, maxLen, preserveWordSpaceLimit);
    }
}

void
ProgramOptions::writeSyntaxPage(std::ostream& out, bool showDefaults)
{
    bool hasOptions = false;
    for(uint32_t i=0; i<_options.size(); ++i) {
        if (!_options[i]->isHeader() && !_options[i]->hideFromSyntaxPage()) {
            hasOptions = true;
        }
    }
    if (!_syntaxMessage.empty()) {
        out << "\n";
        std::vector<std::string> text(breakText(_syntaxMessage, 80));
        for (uint32_t i=0; i<text.size(); ++i) {
            out << text[i] << "\n";
        }
    }
    if (_argc > 0) {
        std::string progName = _argv[0];
        out << "\nUsage: ";
        std::string::size_type pos = progName.rfind("/");
        if (pos != std::string::npos) {
            out << progName.substr(pos+1);
        } else {
            out << progName;
        }
        if (hasOptions) {
            out << " [options]";
        }
        for (uint32_t i=0; i<_arguments.size(); ++i) {
            OptionParser& opt(*_arguments[i]);
            out << (opt.isRequired() ? " <" : " [")
                << opt.getArgName();
            if (opt._argCount == 0) out << "...";
            out << (opt.isRequired() ? ">" : "]");
        }
        out << "\n";
    }
    if (_arguments.size() > 0) {
        out << "\nArguments:\n";
            // To reuse option parser objects, argument names get split on
            // whitespace. Concatenating to show nice text
        std::vector<std::string> argNames(_arguments.size());
        uint32_t argSize = 10;
        for(uint32_t i=0; i<_arguments.size(); ++i) {
            OptionParser& opt(*_arguments[i]);
            argNames[i] = opt.getArgName()
                        + " (" + opt.getArgType(0) + ")";
            if (argNames[i].size() <= _maxLeftColumnSize) {
                argSize = std::max(argSize,
                                   static_cast<uint32_t>(argNames[i].size()));
            }
        }
            // Calculate indent used for extra lines
        std::string indent = "    "; // 1 space indent + " : " in between.
        for (uint32_t i=0; i<argSize; ++i) indent += ' ';

        for(uint32_t i=0; i<_arguments.size(); ++i) {
            OptionParser& opt(*_arguments[i]);
            out << " " << argNames[i];
            if (argNames[i].size() > _maxLeftColumnSize) {
                out << "\n ";
                for (uint32_t j=0; j<argSize; ++j) {
                    out << " ";
                }
            } else {
                for (uint32_t j=argNames[i].size(); j<argSize; ++j) {
                    out << " ";
                }
            }
            std::vector<std::string> message(
                    breakText(opt._description, 80 - indent.size()));
            if (opt._hasDefault) {
                if (message.back().size() + indent.size() + 11 <= 80) {
                    message.back() += " (optional)";
                } else {
                    message.push_back("(optional)");
                }
            }
            for (uint32_t j=0; j<message.size(); ++j) {
                out << (j == 0 ? " : " : indent) << message[j] << "\n";
            }
        }
    }
    if (hasOptions) {
        if (!_options[0]->isHeader()) {
            out << "\nOptions:\n";
        }
        uint32_t argSize = 10;
        for(uint32_t i=0; i<_options.size(); ++i) {
            OptionParser& opt(*_options[i]);
            if (opt.isHeader() || opt.hideFromSyntaxPage()) continue;
            argSize = std::max(argSize, static_cast<uint32_t>(
                                    opt.getOptSyntaxString().size()));
        }
            // If too much space is used in first colo, calculate inset again,
            // such that we don't need to push to max just because one is
            // too long
        if (argSize > _maxLeftColumnSize) {
            argSize = 10;
            for(uint32_t i=0; i<_options.size(); ++i) {
                OptionParser& opt(*_options[i]);
                if (opt.isHeader() || opt.hideFromSyntaxPage()) continue;
                uint32_t size = static_cast<uint32_t>(
                        opt.getOptSyntaxString().size());
                if (size <= _maxLeftColumnSize) {
                    argSize = std::max(argSize, size);
                }
            }
        }
        std::string indent = "   ";
        for (uint32_t i=0; i<argSize; ++i) indent += ' ';
        for(uint32_t i=0; i<_options.size(); ++i) {
            OptionParser& opt(*_options[i]);
            if (opt.isHeader()) {
                out << "\n" << opt._description << ":\n";
                continue;
            }
            if (opt.hideFromSyntaxPage()) continue;
            std::string optStr = opt.getOptSyntaxString();
            out << optStr;
            for (uint32_t j=optStr.size(); j<argSize; ++j) {
                out << " ";
            }
            std::vector<std::string> message(
                    breakText(opt._description, 80 - indent.size()));
            if (showDefaults) {
                std::string s;
                if (!opt._hasDefault) {
                    s = "(required)";
                } else if (!opt._invalidDefault
                           && opt._defaultString != UNSET_TOKEN)
                {
                    s = "(default " + opt._defaultString + ")";
                }
                if (s.size() > 0) {
                    if (message.back().size() + indent.size()
                            + 1 + s.size() <= 80)
                    {
                        message.back() += " " + s;
                    } else {
                        message.push_back(s);
                    }
                }
            }
            for (uint32_t j=0; j<message.size(); ++j) {
                out << (j == 0 ? " : " : indent) << message[j] << "\n";
            }
        }
    }
}

ProgramOptions::OptionParser&
ProgramOptions::addOption(OptionParser::SP && opt)
{
    for (uint32_t i=0; i<opt->_names.size(); ++i) {
        std::map<std::string, OptionParser::SP>::const_iterator it(
                _optionMap.find(opt->_names[i]));
        if (it != _optionMap.end()) {
            throw InvalidCommandLineArgumentsException(
                    "Option '" + opt->_names[i] + "' is already registered.",
                    VESPA_STRLOC);
        }
    }
    _options.push_back(opt);
    for (uint32_t i=0; i<opt->_names.size(); ++i) {
        _optionMap[opt->_names[i]] = opt;
    }
    return *opt;
}

ProgramOptions::OptionParser&
ProgramOptions::addArgument(std::shared_ptr<OptionParser> arg)
{
    if (!_arguments.empty() && !_arguments.back()->isRequired()) {
        if (arg->isRequired()) {
            throw InvalidCommandLineArgumentsException(
                    "Argument '" + arg->_names[0] + "' is required and cannot "
                    "follow an optional argument.", VESPA_STRLOC);
        }
    }
    if (!_arguments.empty() && _arguments.back()->_argCount == 0) {
        throw InvalidCommandLineArgumentsException(
                "Argument '" + arg->_names[0] + "' cannot follow a list "
                "argument that will consume all remaining arguments.",
                VESPA_STRLOC);
        
    }
    _arguments.push_back(arg);
    return *arg;
}

ProgramOptions::OptionParser&
ProgramOptions::getOptionParser(const std::string& id)
{
    std::map<std::string, OptionParser::SP>::const_iterator it(
            _optionMap.find(id));
    if (it == _optionMap.end()) {
        throw InvalidCommandLineArgumentsException(
                "No option registered with id '" + id + "'.", VESPA_STRLOC);
    }
    return *it->second;
}

ProgramOptions::OptionParser&
ProgramOptions::getArgumentParser(uint32_t argIndex)
{
    if (argIndex >= _arguments.size()) {
        std::ostringstream ost;
        ost << "Only " << _arguments.size()
            << " arguments registered. Thus argument " << argIndex
            << " does not exist.";
        throw InvalidCommandLineArgumentsException(ost.str(), VESPA_STRLOC);
    }
    return *_arguments[argIndex];
}

ProgramOptions::BoolOptionParser::BoolOptionParser(
        const std::string& nameList, bool& value, const std::string& desc)
    : OptionParser(nameList, 0, UNSET_TOKEN, desc),
      _value(value),
      _defaultValue(false)
{
}

ProgramOptions::FlagOptionParser::FlagOptionParser(
        const std::string& nameList, bool& value, const std::string& desc)
    : OptionParser(nameList, 0, UNSET_TOKEN, desc),
      _value(value),
      _unsetValue(false)
{
    _invalidDefault = true;
}

ProgramOptions::FlagOptionParser::FlagOptionParser(
        const std::string& nameList, bool& value, const bool& unsetValue,
        const std::string& desc)
    : OptionParser(nameList, 0, unsetValue ? "true" : "false", desc),
      _value(value),
      _unsetValue(unsetValue)
{
    _invalidDefault = true;
}

ProgramOptions::StringOptionParser::StringOptionParser(
        const std::string& nameList, std::string& val, const std::string& desc)
    : OptionParser(nameList, 1, desc),
      _value(val),
      _defaultValue()
{
}

ProgramOptions::StringOptionParser::StringOptionParser(
        const std::string& nameList, std::string& value,
        const std::string& defValue, const std::string& desc)
    : OptionParser(nameList, 1, '"' + defValue + '"', desc),
      _value(value),
      _defaultValue(defValue)
{
}

ProgramOptions::MapOptionParser::MapOptionParser(
        const std::string& nameList, std::map<std::string, std::string>& value,
        const std::string& description)
    : OptionParser(nameList, 2, "empty", description),
      _value(value)
{
}

#define VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(type, parsertype) \
template<> \
ProgramOptions::OptionParser& \
ProgramOptions::addOption(const std::string& optionNameList, \
                          type& value, const std::string& desc) \
{ \
    return addOption(OptionParser::SP( \
            new parsertype(optionNameList, value, desc))); \
}

VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(bool, FlagOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(std::string, StringOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(int32_t, NumberOptionParser<int32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(uint32_t, NumberOptionParser<uint32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(int64_t, NumberOptionParser<int64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(uint64_t, NumberOptionParser<uint64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(float, NumberOptionParser<float>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(double, NumberOptionParser<double>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDOPTION(MapOptionParser::MapType, MapOptionParser);

#define VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(type, parsertype) \
template<> \
ProgramOptions::OptionParser& \
ProgramOptions::addOption(const std::string& optionNameList, \
                          type& value, const type& defVal, \
                          const std::string& desc) \
{ \
    return addOption(OptionParser::SP( \
            new parsertype(optionNameList, value, defVal, desc))); \
}

VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(bool, FlagOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(std::string, StringOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(int32_t, NumberOptionParser<int32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(uint32_t, NumberOptionParser<uint32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(int64_t, NumberOptionParser<int64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(uint64_t, NumberOptionParser<uint64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(float, NumberOptionParser<float>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDOPTION(double, NumberOptionParser<double>);

#define VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(type, parsertype) \
template<> \
ProgramOptions::OptionParser& \
ProgramOptions::addArgument(const std::string& name, \
                            type& value, \
                            const std::string& desc) \
{ \
    return addArgument(OptionParser::SP( \
                new parsertype(name, value, desc))); \
}

VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(bool, BoolOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(std::string, StringOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(int32_t, NumberOptionParser<int32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(uint32_t, NumberOptionParser<uint32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(int64_t, NumberOptionParser<int64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(uint64_t, NumberOptionParser<uint64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(float, NumberOptionParser<float>);
VESPALIB_PROGRAMOPTIONS_IMPL_NODEF_ADDARGUMENT(double, NumberOptionParser<double>);

#define VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(type, parsertype) \
template<> \
ProgramOptions::OptionParser& \
ProgramOptions::addArgument(const std::string& name, \
                            type& value, const type& defVal, \
                            const std::string& desc) \
{ \
    return addArgument(OptionParser::SP( \
                new parsertype(name, value, defVal, desc))); \
}

VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(std::string, StringOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(int32_t, NumberOptionParser<int32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(uint32_t, NumberOptionParser<uint32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(int64_t, NumberOptionParser<int64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(uint64_t, NumberOptionParser<uint64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(float, NumberOptionParser<float>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDARGUMENT(double, NumberOptionParser<double>);

#define VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(type, parsertype) \
template<> \
ProgramOptions::OptionParser& \
ProgramOptions::addListArgument(const std::string& name, \
                                std::vector<type>& value, \
                                const std::string& desc) \
{ \
    ListOptionParser<type>* listParser( \
            new ListOptionParser<type>(name, value, desc)); \
    OptionParser::UP entryParser( \
            new parsertype(name, listParser->getSingleValue(), desc)); \
    listParser->setEntryParser(std::move(entryParser)); \
    return addArgument(OptionParser::SP(listParser)); \
}

VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(std::string, StringOptionParser);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(int32_t, NumberOptionParser<int32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(uint32_t, NumberOptionParser<uint32_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(int64_t, NumberOptionParser<int64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(uint64_t, NumberOptionParser<uint64_t>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(float, NumberOptionParser<float>);
VESPALIB_PROGRAMOPTIONS_IMPL_ADDLISTARGUMENT(double, NumberOptionParser<double>);

template struct ProgramOptions::NumberOptionParser<int32_t>;
template struct ProgramOptions::NumberOptionParser<uint32_t>;
template struct ProgramOptions::NumberOptionParser<int64_t>;
template struct ProgramOptions::NumberOptionParser<uint64_t>;
template struct ProgramOptions::NumberOptionParser<float>;
template struct ProgramOptions::NumberOptionParser<double>;

} // vespalib