summaryrefslogtreecommitdiff
path: root/src/mongo/util/options_parser/option_section.cpp
blob: 8eebd290d742de7f6e8587752bfcf28ea5a6ddb4 (plain)
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

/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/util/options_parser/option_section.h"

#include <algorithm>
#include <iostream>
#include <sstream>

#include "mongo/bson/util/builder.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/options_parser/value.h"

namespace mongo {
namespace optionenvironment {

using std::shared_ptr;

// Registration interface

// TODO: Make sure the section we are adding does not have duplicate options
Status OptionSection::addSection(const OptionSection& subSection) {
    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = subSection._options.begin(); oditerator != subSection._options.end();
         oditerator++) {
        if (oditerator->_positionalStart != -1) {
            StringBuilder sb;
            sb << "Attempted to add subsection with positional option: " << oditerator->_dottedName;
            return Status(ErrorCodes::InternalError, sb.str());
        }
    }
    _subSections.push_back(subSection);
    return Status::OK();
}

OptionDescription& OptionSection::addOptionChaining(
    const std::string& dottedName,
    const std::string& singleName,
    const OptionType type,
    const std::string& description,
    const std::vector<std::string>& deprecatedDottedNames,
    const std::vector<std::string>& deprecatedSingleNames) {
    OptionDescription option(
        dottedName, singleName, type, description, deprecatedDottedNames, deprecatedSingleNames);
    // Verify deprecated dotted names.
    // No empty deprecated dotted names.
    if (std::count(deprecatedDottedNames.begin(), deprecatedDottedNames.end(), "")) {
        StringBuilder sb;
        sb << "Attempted to register option with empty string for deprecatedDottedName";
        uasserted(ErrorCodes::InternalError, sb.str());
    }

    // Should not be the same as dottedName.
    if (std::count(deprecatedDottedNames.begin(), deprecatedDottedNames.end(), dottedName)) {
        StringBuilder sb;
        sb << "Attempted to register option with conflict between dottedName and "
           << "deprecatedDottedName: " << dottedName;
        uasserted(ErrorCodes::InternalError, sb.str());
    }

    // Verify deprecated single names.
    // No empty deprecated single names.
    if (std::count(deprecatedSingleNames.begin(), deprecatedSingleNames.end(), "")) {
        StringBuilder sb;
        sb << "Attempted to register option with empty string for deprecatedSingleName";
        uasserted(ErrorCodes::InternalError, sb.str());
    }

    // Should not be the same as singleName.
    if (std::count(deprecatedSingleNames.begin(), deprecatedSingleNames.end(), singleName)) {
        StringBuilder sb;
        sb << "Attempted to register option with conflict between singleName and "
           << "deprecatedSingleName: " << singleName;
        uasserted(ErrorCodes::InternalError, sb.str());
    }

    for (const OptionDescription& od : _options) {
        if (option._dottedName == od._dottedName) {
            StringBuilder sb;
            sb << "Attempted to register option with duplicate dottedName: " << dottedName;
            uasserted(ErrorCodes::InternalError, sb.str());
        }

        // Allow options with empty singleName since some options are not allowed on the command
        // line
        if (!option._singleName.empty() && option._singleName == od._singleName) {
            StringBuilder sb;
            sb << "Attempted to register option with duplicate singleName: " << singleName;
            uasserted(ErrorCodes::InternalError, sb.str());
        }

        if (std::count(
                od._deprecatedDottedNames.begin(), od._deprecatedDottedNames.end(), dottedName)) {
            StringBuilder sb;
            sb << "Attempted to register option with conflict between dottedName and existing "
                  "deprecatedDottedName: "
               << dottedName;
            uasserted(ErrorCodes::InternalError, sb.str());
        }

        if (std::count(
                od._deprecatedSingleNames.begin(), od._deprecatedSingleNames.end(), singleName)) {
            StringBuilder sb;
            sb << "Attempted to register option with conflict between singleName and existing "
                  "deprecatedSingleName: "
               << singleName;
            uasserted(ErrorCodes::InternalError, sb.str());
        }

        for (const std::string& deprecatedDottedName : deprecatedDottedNames) {
            if (deprecatedDottedName == od._dottedName) {
                StringBuilder sb;
                sb << "Attempted to register option with conflict between deprecatedDottedName "
                      "and existing dottedName: "
                   << dottedName;
                uasserted(ErrorCodes::InternalError, sb.str());
            }

            if (std::count(od._deprecatedDottedNames.begin(),
                           od._deprecatedDottedNames.end(),
                           deprecatedDottedName)) {
                StringBuilder sb;
                sb << "Attempted to register option with duplicate deprecatedDottedName: "
                   << deprecatedDottedName;
                uasserted(ErrorCodes::InternalError, sb.str());
            }
        }

        for (const std::string& deprecatedSingleName : deprecatedSingleNames) {
            if (deprecatedSingleName == od._singleName) {
                StringBuilder sb;
                sb << "Attempted to register option with conflict between deprecatedSingleName "
                      "and existing singleName: "
                   << singleName;
                uasserted(ErrorCodes::InternalError, sb.str());
            }

            if (std::count(od._deprecatedSingleNames.begin(),
                           od._deprecatedSingleNames.end(),
                           deprecatedSingleName)) {
                StringBuilder sb;
                sb << "Attempted to register option with duplicate deprecatedSingleName: "
                   << deprecatedSingleName;
                uasserted(ErrorCodes::InternalError, sb.str());
            }
        }
    }

    _options.push_back(option);

    return _options.back();
}

// Stuff for dealing with Boost

namespace {

/*
 * Helper for option types that should be interpreted as a string by boost.  We do this to
 * take the responsibility away from boost for handling type conversions, since sometimes
 * those conversions are inconsistent with our own.  See SERVER-14110 For an example.
 */
template <typename Type>
Status typeToBoostStringType(std::unique_ptr<po::value_semantic>* boostType,
                             const Value defaultValue = Value(),
                             const Value implicitValue = Value()) {
    std::unique_ptr<po::typed_value<std::string>> boostTypeBuilder(po::value<std::string>());

    if (!implicitValue.isEmpty()) {
        Type implicitValueType;
        Status ret = implicitValue.get(&implicitValueType);
        if (!ret.isOK()) {
            StringBuilder sb;
            sb << "Error getting implicit value: " << ret.toString();
            return Status(ErrorCodes::InternalError, sb.str());
        }
        StringBuilder sb;
        sb << implicitValueType;
        boostTypeBuilder->implicit_value(sb.str());
    }

    if (!defaultValue.isEmpty()) {
        Type defaultValueType;
        Status ret = defaultValue.get(&defaultValueType);
        if (!ret.isOK()) {
            StringBuilder sb;
            sb << "Error getting default value: " << ret.toString();
            return Status(ErrorCodes::InternalError, sb.str());
        }
        StringBuilder sb;
        sb << defaultValueType;
        boostTypeBuilder->default_value(sb.str());
    }

    *boostType = std::move(boostTypeBuilder);

    return Status::OK();
}

/** Helper function to convert the values of our OptionType enum into the classes that
 *  boost::program_option uses to pass around this information
 */
Status typeToBoostType(std::unique_ptr<po::value_semantic>* boostType,
                       OptionType type,
                       const Value defaultValue = Value(),
                       const Value implicitValue = Value(),
                       bool getSwitchAsBool = false) {
    switch (type) {
        case StringVector: {
            *boostType = std::unique_ptr<po::value_semantic>(po::value<std::vector<std::string>>());

            if (!implicitValue.isEmpty()) {
                StringBuilder sb;
                sb << "Implicit value not supported for string vector";
                return Status(ErrorCodes::InternalError, sb.str());
            }

            if (!defaultValue.isEmpty()) {
                StringBuilder sb;
                sb << "Default value not supported for string vector";
                return Status(ErrorCodes::InternalError, sb.str());
            }

            return Status::OK();
        }
        case StringMap: {
            // Boost doesn't support maps, so we just register a vector parameter and
            // parse it as "key=value" strings
            *boostType = std::unique_ptr<po::value_semantic>(po::value<std::vector<std::string>>());

            if (!implicitValue.isEmpty()) {
                StringBuilder sb;
                sb << "Implicit value not supported for string map";
                return Status(ErrorCodes::InternalError, sb.str());
            }

            if (!defaultValue.isEmpty()) {
                StringBuilder sb;
                sb << "Default value not supported for string map";
                return Status(ErrorCodes::InternalError, sb.str());
            }

            return Status::OK();
        }
        case Switch: {
            // In boost, switches default to false which makes it impossible to tell if
            // a switch in a config file is not present or was explicitly set to false.
            //
            // Because of this, and because of the fact that we use the same set of
            // options for the legacy key=value config file, we need a way to control
            // whether we are telling boost that an option is a switch type or that an
            // option is a bool type.
            if (!getSwitchAsBool) {
                *boostType = std::unique_ptr<po::value_semantic>(po::bool_switch());
                return Status::OK();
            } else {
                // Switches should be true if they are present with no explicit value.
                *boostType =
                    std::unique_ptr<po::typed_value<bool>>(po::value<bool>()->implicit_value(true));
                return Status::OK();
            }
        }
        case Bool: {
            std::unique_ptr<po::typed_value<bool>> boostTypeBuilder(po::value<bool>());

            if (!implicitValue.isEmpty()) {
                bool implicitValueType;
                Status ret = implicitValue.get(&implicitValueType);
                if (!ret.isOK()) {
                    StringBuilder sb;
                    sb << "Error getting implicit value: " << ret.toString();
                    return Status(ErrorCodes::InternalError, sb.str());
                }
                boostTypeBuilder->implicit_value(implicitValueType);
            }

            if (!defaultValue.isEmpty()) {
                bool defaultValueType;
                Status ret = defaultValue.get(&defaultValueType);
                if (!ret.isOK()) {
                    StringBuilder sb;
                    sb << "Error getting default value: " << ret.toString();
                    return Status(ErrorCodes::InternalError, sb.str());
                }
                boostTypeBuilder->default_value(defaultValueType);
            }

            *boostType = std::move(boostTypeBuilder);

            return Status::OK();
        }
        case String:
            return typeToBoostStringType<std::string>(boostType, defaultValue, implicitValue);
        case Double:
            return typeToBoostStringType<double>(boostType, defaultValue, implicitValue);
        case Int:
            return typeToBoostStringType<int>(boostType, defaultValue, implicitValue);
        case Long:
            return typeToBoostStringType<long>(boostType, defaultValue, implicitValue);
        case UnsignedLongLong:
            return typeToBoostStringType<unsigned long long>(
                boostType, defaultValue, implicitValue);
        case Unsigned:
            return typeToBoostStringType<unsigned>(boostType, defaultValue, implicitValue);
        default: {
            StringBuilder sb;
            sb << "Unrecognized option type: " << type;
            return Status(ErrorCodes::InternalError, sb.str());
        }
    }
}
}  // namespace

Status OptionSection::getBoostOptions(po::options_description* boostOptions,
                                      bool visibleOnly,
                                      bool includeDefaults,
                                      OptionSources sources,
                                      bool getEmptySections) const {
    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = _options.begin(); oditerator != _options.end(); oditerator++) {
        // Only include this option if it matches the sources we specified and the option is
        // either visible or we are requesting hidden options
        if ((!visibleOnly || (oditerator->_isVisible)) && (oditerator->_sources & sources)) {
            std::unique_ptr<po::value_semantic> boostType;
            Status ret = typeToBoostType(&boostType,
                                         oditerator->_type,
                                         includeDefaults ? oditerator->_default : Value(),
                                         oditerator->_implicit,
                                         !(sources & SourceCommandLine));
            if (!ret.isOK()) {
                StringBuilder sb;
                sb << "Error getting boost type for option \"" << oditerator->_dottedName
                   << "\": " << ret.toString();
                return Status(ErrorCodes::InternalError, sb.str());
            }

            if (oditerator->_singleName.empty()) {
                StringBuilder sb;
                sb << "Single name is empty for option \"" << oditerator->_dottedName
                   << "\", but trying to use it on the command line "
                   << "or INI config file.  Only options that are exclusive to the YAML config "
                   << "file can have an empty single name";
                return Status(ErrorCodes::InternalError, sb.str());
            }

            boostOptions->add_options()(oditerator->_singleName.c_str(),
                                        boostType.release(),
                                        oditerator->_description.c_str());

            if (!visibleOnly) {
                for (const std::string& depreatedSingleName : oditerator->_deprecatedSingleNames) {
                    std::unique_ptr<po::value_semantic> boostTypeDep;
                    Status retDep =
                        typeToBoostType(&boostTypeDep,
                                        oditerator->_type,
                                        includeDefaults ? oditerator->_default : Value(),
                                        oditerator->_implicit,
                                        !(sources & SourceCommandLine));
                    if (!retDep.isOK()) {
                        return retDep;
                    }
                    boostOptions->add_options()(depreatedSingleName.c_str(),
                                                boostTypeDep.release(),
                                                oditerator->_description.c_str());
                }
            }
        }
    }

    std::list<OptionSection>::const_iterator ositerator;
    for (ositerator = _subSections.begin(); ositerator != _subSections.end(); ositerator++) {
        po::options_description subGroup = ositerator->_name.empty()
            ? po::options_description()
            : po::options_description(ositerator->_name.c_str());

        // Do not add empty sections to our option_description unless we specifically requested.
        int numOptions;
        Status ret = ositerator->countOptions(&numOptions, visibleOnly, sources);
        if (!ret.isOK()) {
            return ret;
        }
        if (numOptions == 0 && getEmptySections == false) {
            continue;
        }

        ret = ositerator->getBoostOptions(
            &subGroup, visibleOnly, includeDefaults, sources, getEmptySections);
        if (!ret.isOK()) {
            return ret;
        }
        boostOptions->add(subGroup);
    }

    return Status::OK();
}

/*
 * The way we specify positional options in our interface differs from the way boost does it, so
 * we have to convert them here.
 *
 * For example, to specify positionals such that you can run "./exec [pos1] [pos2] [pos2]":
 *
 * Our interface:
 *
 * options.addOptionChaining("pos2", "pos2", moe::StringVector, "Pos2")
 *                          .hidden() <- doesn't show up in help
 *                          .sources(moe::SourceCommandLine) <- only allowed on command line
 *                          .positional(2, <- start position
 *                          3); <- end position
 * options.addOptionChaining("pos1", "pos1", moe::String, "Pos1")
 *                          .hidden() <- doesn't show up in help
 *                          .sources(moe::SourceCommandLine) <- only allowed on command line
 *                          .positional(1, <- start position
 *                          1); <- end position
 * // Note that order doesn't matter
 *
 * Boost's interface:
 *
 * boostHiddenOptions->add_options()("pos1", po::value<std::string>(), "Pos1")
 *                                  ("pos2", po::value<std::string>(), "Pos2")
 *
 * boostPositionalOptions->add("pos1", 1); <- count of option (number of times it appears)
 * boostPositionalOptions->add("pos2", 2); <- count of option (number of times it appears)
 * // Note that order does matter
 *
 * Because of this, we have to perform the conversion in this function.  The tasks performed by
 * this function are:
 *
 * 1. Making sure the ranges are valid as a whole (no overlap or holes)
 * 2. Convert to the boost options and add them in the correct order
 */
Status OptionSection::getBoostPositionalOptions(
    po::positional_options_description* boostPositionalOptions) const {
    std::list<OptionDescription> positionalOptions;

    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = _options.begin(); oditerator != _options.end(); oditerator++) {
        // Check if this is a positional option, and extract it if it is
        if (oditerator->_positionalStart != -1) {
            positionalOptions.push_back(*oditerator);
        }
    }

    int nextPosition = 1;
    bool foundAtPosition = false;
    while (!positionalOptions.empty()) {
        foundAtPosition = false;
        std::list<OptionDescription>::iterator poditerator;
        for (poditerator = positionalOptions.begin(); poditerator != positionalOptions.end();) {
            if (poditerator->_positionalStart < nextPosition) {
                StringBuilder sb;
                sb << "Found option with overlapping positional range: "
                   << "  Expected next option at position: " << nextPosition << ", but \""
                   << poditerator->_dottedName
                   << "\" starts at position: " << poditerator->_positionalStart;
                return Status(ErrorCodes::InternalError, sb.str());
            }

            if (poditerator->_positionalStart == nextPosition) {
                foundAtPosition = true;

                int count;
                if (poditerator->_positionalEnd == -1) {
                    count = -1;
                    if (positionalOptions.size() != 1) {
                        StringBuilder sb;
                        sb << "Found positional option with infinite count, but still have "
                           << "more positional options registered";
                        return Status(ErrorCodes::InternalError, sb.str());
                    }
                } else {
                    count = (poditerator->_positionalEnd + 1) - poditerator->_positionalStart;
                }

                boostPositionalOptions->add(poditerator->_dottedName.c_str(), count);
                nextPosition += count;
                std::list<OptionDescription>::iterator old_poditerator = poditerator;
                poditerator++;
                positionalOptions.erase(old_poditerator);
            } else {
                poditerator++;
            }
        }
        if (!foundAtPosition) {
            StringBuilder sb;
            sb << "Did not find option at position: " << nextPosition;
            return Status(ErrorCodes::InternalError, sb.str());
        }
    }

    // XXX: Right now only the top level section can have positional options

    return Status::OK();
}

// Get options for iterating
// TODO: should I make this an iterator?

Status OptionSection::getAllOptions(std::vector<OptionDescription>* options) const {
    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = _options.begin(); oditerator != _options.end(); oditerator++) {
        // We need to check here that we didn't register an option with an empty single name
        // that is allowed on the command line or in an old style config, since we don't have
        // this information available all at once when the option is registered
        if (oditerator->_singleName.empty() && oditerator->_sources & SourceAllLegacy) {
            StringBuilder sb;
            sb << "Found option allowed on the command line with an empty singleName: "
               << oditerator->_dottedName;
            return Status(ErrorCodes::InternalError, sb.str());
        }

        options->push_back(*oditerator);
    }

    std::list<OptionSection>::const_iterator ositerator;
    for (ositerator = _subSections.begin(); ositerator != _subSections.end(); ositerator++) {
        ositerator->getAllOptions(options).transitional_ignore();
    }

    return Status::OK();
}

Status OptionSection::getDefaults(std::map<Key, Value>* values) const {
    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = _options.begin(); oditerator != _options.end(); oditerator++) {
        if (!oditerator->_default.isEmpty()) {
            (*values)[oditerator->_dottedName] = oditerator->_default;
        }
    }

    std::list<OptionSection>::const_iterator ositerator;
    for (ositerator = _subSections.begin(); ositerator != _subSections.end(); ositerator++) {
        ositerator->getDefaults(values).transitional_ignore();
    }

    return Status::OK();
}

Status OptionSection::countOptions(int* numOptions, bool visibleOnly, OptionSources sources) const {
    *numOptions = 0;

    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = _options.begin(); oditerator != _options.end(); oditerator++) {
        // Only count this option if it matches the sources we specified and the option is
        // either visible or we are requesting hidden options
        if ((!visibleOnly || (oditerator->_isVisible)) && (oditerator->_sources & sources)) {
            (*numOptions)++;
        }
    }

    std::list<OptionSection>::const_iterator ositerator;
    for (ositerator = _subSections.begin(); ositerator != _subSections.end(); ositerator++) {
        int numSubOptions = 0;
        ositerator->countOptions(&numSubOptions, visibleOnly, sources).transitional_ignore();
        *numOptions += numSubOptions;
    }

    return Status::OK();
}

Status OptionSection::getConstraints(std::vector<std::shared_ptr<Constraint>>* constraints) const {
    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = _options.begin(); oditerator != _options.end(); oditerator++) {
        std::vector<std::shared_ptr<Constraint>>::const_iterator citerator;
        for (citerator = oditerator->_constraints.begin();
             citerator != oditerator->_constraints.end();
             citerator++) {
            constraints->push_back(*citerator);
        }
    }

    std::list<OptionSection>::const_iterator ositerator;
    for (ositerator = _subSections.begin(); ositerator != _subSections.end(); ositerator++) {
        ositerator->getConstraints(constraints).transitional_ignore();
    }

    return Status::OK();
}

std::string OptionSection::positionalHelpString(const std::string& execName) const {
    po::positional_options_description boostPositionalOptions;
    Status ret = getBoostPositionalOptions(&boostPositionalOptions);
    if (!ret.isOK()) {
        StringBuilder sb;
        sb << "Error constructing help string: " << ret.toString();
        return sb.str();
    }

    StringBuilder posHelpStringBuilder;
    posHelpStringBuilder << execName;

    // If we can have unlimited positional options, this returns
    // std::numeric_limits<unsigned>::max().  Check here for that case and record what name to
    // look for.
    unsigned int numPositional = boostPositionalOptions.max_total_count();
    std::string trailingPositionName;
    if (numPositional == std::numeric_limits<unsigned>::max()) {
        trailingPositionName = boostPositionalOptions.name_for_position(numPositional - 1);
    }

    unsigned int position;
    std::string positionName;
    for (position = 0; position < numPositional; position++) {
        positionName = boostPositionalOptions.name_for_position(position);
        if (!trailingPositionName.empty() && trailingPositionName == positionName) {
            // If we have a trailing position, we break when we see it the first time.
            posHelpStringBuilder << " [" << trailingPositionName << " ... ]";
            break;
        }
        posHelpStringBuilder << " [" << positionName << "]";
    }

    return posHelpStringBuilder.str();
}

std::string OptionSection::helpString() const {
    po::options_description boostOptions =
        _name.empty() ? po::options_description() : po::options_description(_name.c_str());
    Status ret = getBoostOptions(&boostOptions,
                                 true, /* visibleOnly */
                                 true, /* includeDefaults */
                                 SourceAllLegacy,
                                 false); /* getEmptySections */
    if (!ret.isOK()) {
        StringBuilder sb;
        sb << "Error constructing help string: " << ret.toString();
        return sb.str();
    }

    // Can't use a StringBuilder here because boost::program_options only has functions that
    // output to std::ostream
    std::ostringstream os;
    os << boostOptions;
    return os.str();
}

/* Debugging */
void OptionSection::dump() const {
    std::list<OptionDescription>::const_iterator oditerator;
    for (oditerator = _options.begin(); oditerator != _options.end(); oditerator++) {
        std::cout << " _dottedName: " << oditerator->_dottedName
                  << " _singleName: " << oditerator->_singleName << " _type: " << oditerator->_type
                  << " _description: " << oditerator->_description
                  << " _isVisible: " << oditerator->_isVisible << std::endl;
    }

    std::list<OptionSection>::const_iterator ositerator;
    for (ositerator = _subSections.begin(); ositerator != _subSections.end(); ositerator++) {
        std::cout << "Section Name: " << ositerator->_name << std::endl;
        ositerator->dump();
    }
}

}  // namespace optionenvironment
}  // namespace mongo