aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/command_line.hpp
blob: 9fe6b985a148c5579d62f467e0ec74383a05f496 (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
// Copyright (c) 2017 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "PDB repository" project.
// For details, see https://github.com/egor-tensin/pdb-repo.
// Distributed under the MIT License.

#pragma once

#include <boost/filesystem.hpp>
#include <boost/nowide/args.hpp>
#include <boost/nowide/filesystem.hpp>
#include <boost/nowide/iostream.hpp>
#include <boost/program_options.hpp>

#include <exception>
#include <iostream>
#include <ostream>
#include <string>

class Args {
public:
    Args(int argc, char** argv) : argc{argc}, argv{argv}, impl{this->argc, this->argv} {
        boost::nowide::nowide_filesystem();
    }

    int argc;
    char** argv;

private:
    const boost::nowide::args impl;
};

class SettingsParser {
public:
    explicit SettingsParser(int argc, char** argv)
        : args{argc, argv}, prog_name{extract_filename(args.argv[0])} {
        visible.add_options()("help,h", "show this message and exit");
    }

    virtual ~SettingsParser() = default;

    virtual const char* get_short_description() const { return "[--option VALUE]..."; }

    virtual void parse(int argc, char* argv[]) {
        boost::program_options::options_description all;
        all.add(hidden).add(visible);
        boost::program_options::variables_map vm;
        boost::program_options::store(boost::program_options::command_line_parser{argc, argv}
                                          .options(all)
                                          .positional(positional)
                                          .run(),
                                      vm);
        if (vm.count("help"))
            exit_with_usage = true;
        else
            boost::program_options::notify(vm);
    }

    bool exit_with_usage = false;

    void usage() const { boost::nowide::cout << *this; }

    void usage_error(const std::exception& e) const {
        boost::nowide::cerr << "usage error: " << e.what() << '\n';
        boost::nowide::cerr << *this;
    }

protected:
    boost::program_options::options_description hidden;
    boost::program_options::options_description visible;
    boost::program_options::positional_options_description positional;

private:
    static std::string extract_filename(char* path) {
        return boost::filesystem::path{path}.filename().string();
    }

    const Args args;
    const std::string prog_name;

    friend std::ostream& operator<<(std::ostream& os, const SettingsParser& parser) {
        const auto short_descr = parser.get_short_description();
        os << "usage: " << parser.prog_name << ' ' << short_descr << '\n';
        os << parser.visible;
        return os;
    }
};