simple_args.h 4.97 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
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
#pragma once

#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
#include <assert.h>

struct arg_content_t
{
    std::string name; // key
    std::string value;
    std::string help_text;
};

class simple_args_t
{
    public:
    simple_args_t() {}
    simple_args_t& insert(const std::string& name_,
                          const std::string& default_value_,
                          const std::string& help_text_)
    {
        arg_content_t arg{name_, default_value_, help_text_};

        if(arg_map.count(arg.name) != 0)
        {
            std::cout << "arg:" << arg.name << "already exist" << std::endl;
        }
        else
        {
            arg_map[arg.name] = arg;
        }
        return *this;
    }
    void usage()
    {
        for(auto& content : arg_map)
        {
            std::vector<std::string> help_text_lines;
            size_t pos = 0;
            for(size_t next_pos = content.second.help_text.find('\n', pos);
                next_pos != std::string::npos;)
            {
                help_text_lines.push_back(
                    std::string(content.second.help_text.begin() + pos,
                                content.second.help_text.begin() + next_pos++));
                pos      = next_pos;
                next_pos = content.second.help_text.find('\n', pos);
            }
            help_text_lines.push_back(std::string(content.second.help_text.begin() + pos,
                                                  content.second.help_text.end()));

            int arg_name_width = 16 - content.second.name.length();
            arg_name_width     = arg_name_width > 0 ? arg_name_width : 2;
            std::cout << std::setw(4) << "-" << content.second.name << std::setw(arg_name_width)
                      << " " << help_text_lines[0] << std::endl;

            for(auto help_next_line = std::next(help_text_lines.begin());
                help_next_line != help_text_lines.end();
                ++help_next_line)
            {
                std::cout << std::setw(28) << " " << *help_next_line << std::endl;
            }
        }
    }
    bool parse(int argc, char* argv[], int start_index = 1)
    {
        if(argc <= start_index)
        {
            // std::cout << "not enough args (" << argc << ") with starting index " << start_index
            // << std::endl;
            return true;
        }
        for(int i = start_index; i < argc; i++)
        {
            std::string cur_arg = std::string(argv[i]);
            if(cur_arg[0] != '-')
            {
                std::cout << "illegal input" << std::endl;
                usage();
                return false;
            }
            else if(cur_arg[0] == '-' && cur_arg[1] == '?')
            {
                usage();
                return false;
            }
            else
            {
                size_t found_equal = cur_arg.find('=');
                if(found_equal == std::string::npos || found_equal == (cur_arg.length() - 1))
                {
                    std::cout << "failed while parsing \"" << cur_arg << "\", "
                              << "arg must be in the form \"-name=value\"" << std::endl;
                    return false;
                }
                std::string arg_name  = cur_arg.substr(1, found_equal - 1);
                std::string arg_value = cur_arg.substr(found_equal + 1);
                if(arg_map.count(arg_name) == 0)
                {
                    std::cout << "no such arg \"" << arg_name << "\" registered" << std::endl;
                    return false;
                }
                arg_map[arg_name].value = arg_value;
            }
        }
        return true;
    }

    std::string get(const std::string& name) const { return get_str(name); }

    std::string get_str(const std::string& name) const
    {
        assert(arg_map.count(name) != 0);
        std::string value = arg_map.at(name).value;
        return value;
    }

    int get_int(const std::string& name) const
    {
        assert(arg_map.count(name) != 0);
        int value = atoi(arg_map.at(name).value.c_str());
        return value;
    }

    uint32_t get_uint32(const std::string& name) const
    {
        assert(arg_map.count(name) != 0);
        uint32_t value = strtoul(arg_map.at(name).value.c_str(), nullptr, 10);
        return value;
    }

    uint64_t get_uint64(const std::string& name) const
    {
        assert(arg_map.count(name) != 0);
        uint64_t value = strtoull(arg_map.at(name).value.c_str(), nullptr, 10);
        return value;
    }

    double get_double(const std::string& name) const
    {
        assert(arg_map.count(name) != 0);
        double value = atof(arg_map.at(name).value.c_str());
        return value;
    }

    float get_float(const std::string& name) const
    {
        assert(arg_map.count(name) != 0);
        float value = atof(arg_map.at(name).value.c_str());
        return value;
    }

    private:
    std::unordered_map<std::string, arg_content_t> arg_map;
};