File: inter_argument_order.cpp

package info (click to toggle)
cli11 2.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,120 kB
  • sloc: cpp: 23,299; python: 129; sh: 64; makefile: 11; ruby: 7
file content (51 lines) | stat: -rw-r--r-- 1,532 bytes parent folder | download
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
// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause

#include <CLI/CLI.hpp>
#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>

int main(int argc, char **argv) {
    CLI::App app{"An app to practice mixing unlimited arguments, but still recover the original order."};

    std::vector<int> foos;
    auto *foo = app.add_option("--foo,-f", foos, "Some unlimited argument");

    std::vector<int> bars;
    auto *bar = app.add_option("--bar", bars, "Some unlimited argument");

    app.add_flag("--z,--x", "Random other flags");

    // Standard parsing lines (copy and paste in, or use CLI11_PARSE)
    try {
        app.parse(argc, argv);
    } catch(const CLI::ParseError &e) {
        return app.exit(e);
    }

    // I prefer using the back and popping
    std::reverse(std::begin(foos), std::end(foos));
    std::reverse(std::begin(bars), std::end(bars));

    std::vector<std::pair<std::string, int>> keyval;
    for(auto *option : app.parse_order()) {
        if(option == foo) {
            keyval.emplace_back("foo", foos.back());
            foos.pop_back();
        }
        if(option == bar) {
            keyval.emplace_back("bar", bars.back());
            bars.pop_back();
        }
    }

    // Prove the vector is correct
    for(auto &pair : keyval) {
        std::cout << pair.first << " : " << pair.second << '\n';
    }
}