Options.h 3 KB
Newer Older
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include <iostream>
#include <sstream>
#include <string>

class Options {
  protected:
    char **begin;
    char **end;

    /**
     * @brief Get the char* value of the cmd line argument.
     * @param option the argument in cmd.
     * @return char*
     */
    char *get_cmd_option(const std::string &option) {
        char **itr = std::find(begin, end, option);
        if (itr != end && ++itr != end) {
            return *itr;
        }
        return 0;
    }

    /**
     * @brief Get the int type value of cmd line argument.
     * @param option the cmd line argument.
     * @param defaults the default value.
     * @return int the int type value of cmd line argument 'option'.
     */
    int get_cmd_line_argument_int(const std::string &option, int defaults) {
        if (char *value = get_cmd_option(option)) {
            try {
                return std::stoi(value);
            } catch (const std::exception &e) {
                std::cerr << "Error: Invalid argument - " << option << " should be INT " << e.what() << '\n';
                exit(1);
            }
        }
        return defaults;
    }

    /**
     * @brief Get the string type value of cmd line argument.
     * @param  option the cmd line argument.
     * @return std::string the int type value of cmd line argument 'option'.
     */
    std::string get_cmd_line_argument_string(const std::string &option) {
        if (char *value = get_cmd_option(option)) {
            return std::string(value);
        }
        return "";
    }

    /**
     * @brief Get the boolean type value of cmd line argument.
     * @param  option the cmd line argument.
     * @return bool the boolean value.
     */
    bool get_cmd_line_argument_bool(const std::string &option) {
        if (cmd_option_exists(option)) {
            return true;
        }
        return false;
    }

    /**
     * @brief Check if a argument exists.
     * @param  option the cmd line argument.
     * @return bool if a argument exists.
     */
    bool cmd_option_exists(const std::string &option) { return std::find(begin, end, option) != end; }

    /**
     * @brief Get the option usage.
     */
    virtual void get_option_usage(){};

    /**
     * @brief Parse the arguments.
     */
    virtual void parse_arguments(){};

  public:
    /**
     * @brief Construct a new Command Line object.
     * @param argc the number of command line arguments.
     * @param argv the string array of comamnd line arguments.
     */
    Options(int argc, char *argv[]) {
        begin = argv;
        end = argv + argc;
    }

    /**
     * @brief Init and parse the arguments.
     */
    virtual void init() {
        if (cmd_option_exists("--help")) {
            get_option_usage();
            exit(0);
        }
        try {
            parse_arguments();
        } catch (const std::exception &e) {
            std::cerr << "Error: Invalid argument - " << e.what() << '\n';
            exit(1);
        }
    };
};