Options.h 4.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

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

10
11
12
13
14
15
struct UInt3 {
    unsigned int x;
    unsigned int y;
    unsigned int z;
};

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
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;
    }

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
    /**
     * @brief Get the unsigned long long type value of cmd line argument.
     * @param option the cmd line argument.
     * @param defaults the default value.
     * @return unsigned long long the unsigned long long type value of cmd line argument 'option'.
     */
    std::vector<unsigned int> splitAndConvertToInt(const std::string &str) {
        std::vector<unsigned int> result;
        std::stringstream ss(str);
        std::string token;

        while (std::getline(ss, token, ',')) {
            try {
                result.push_back(std::stoul(token));
            } catch (std::invalid_argument &e) {
                throw std::invalid_argument("Invalid argument: " + token + e.what());
            }
        }
        return result;
    }

    /**
     * @brief Get the unsigned int type value of cmd line argument.
     * @param option the cmd line argument.
     * @param defaults the default value.
     * @return unsigned int the unsigned int type value of cmd line argument 'option'.
     */
    UInt3 get_cmd_line_argument_uint3(const std::string &option, const UInt3 &defaults) {
        if (char *value = get_cmd_option(option)) {
            try {
                std::vector<unsigned int> values = splitAndConvertToInt(value);
                if (values.size() != 3) {
                    std::cout << "Error: Invalid argument - " << option << " should be unsigned int3" << '\n';
                    exit(1);
                }
                return {values[0], values[1], values[2]};

            } catch (const std::exception &e) {
                std::cout << "Error: Invalid argument - " << option << " should be unsigned int3" << e.what() << '\n';
                exit(1);
            }
        }
        return defaults;
    }

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
    /**
     * @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);
        }
    };
};