arg_parser.hpp Source File

arg_parser.hpp Source File#

Composable Kernel: arg_parser.hpp Source File
arg_parser.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
6#include <string>
7
8#include <iomanip>
9#include <iostream>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string>
13#include <unordered_map>
14#include <vector>
15
16namespace ck_tile {
17/*
18 * a host side utility, arg parser for, either
19 * -[key0] = [value0, value1, value2]
20 * or
21 * -[key0]=[value0] -[key1]=[value1] ...
22 */
24{
25
26 public:
27 class Arg
28 {
29 public:
30 std::string name;
31 std::string value;
32 std::string help_text;
33 };
34
36 ArgParser& insert(const std::string& _name,
37 const std::string& _default_value,
38 const std::string& _help_text)
39 {
40 Arg in;
41 in.name = _name;
42 in.value = _default_value;
43 in.help_text = _help_text;
44
45 if(input_map.count(_name) != 0)
46 {
47 printf("arg:%s already exist\n", _name.c_str());
48 }
49 else
50 {
51 input_map[_name] = in;
52 keys.push_back(_name);
53 }
54 return *this;
55 }
56 void print() const
57 {
58 // find max key length
59 std::string::size_type max_key_length = 11;
60 for(auto& key : keys)
61 {
62 if(max_key_length < key.length())
63 {
64 max_key_length = key.length();
65 }
66 }
67
68 printf("args:\n");
69 for(auto& key : keys)
70 {
71 auto value = input_map.at(key);
72 std::vector<std::string> help_text_lines;
73 size_t pos = 0;
74 for(size_t next_pos = value.help_text.find('\n', pos); next_pos != std::string::npos;)
75 {
76 help_text_lines.push_back(std::string(value.help_text.begin() + pos,
77 value.help_text.begin() + next_pos++));
78 pos = next_pos;
79 next_pos = value.help_text.find('\n', pos);
80 }
81 help_text_lines.push_back(
82 std::string(value.help_text.begin() + pos, value.help_text.end()));
83
84 std::string default_value = std::string("(default:") + value.value + std::string(")");
85 std::cout << std::setw(1 + max_key_length - value.name.length()) << "-" << key
86 << std::setw(4) << " " << help_text_lines[0] << " " << default_value
87 << std::endl;
88
89 for(auto help_next_line = std::next(help_text_lines.begin());
90 help_next_line != help_text_lines.end();
91 ++help_next_line)
92 {
93 std::cout << std::setw(1 + max_key_length + 4) << " " << *help_next_line
94 << std::endl;
95 }
96 }
97 }
98 bool parse(int argc, char* argv[], int start_index = 1)
99 {
100 if(argc < start_index)
101 {
102 printf("not enough args\n");
103 return false;
104 }
105 for(int i = start_index; i < argc; i++)
106 {
107 char* cur_arg = argv[i];
108 if(cur_arg[0] != '-')
109 {
110 printf("illegal input\n");
111 print();
112 return false;
113 }
114 else
115 {
116 std::string text(cur_arg + 1);
117 if(text == "?")
118 {
119 print();
120 return false;
121 }
122 auto pos = text.find('=');
123 if(pos == std::string::npos)
124 {
125 printf("arg should be [key]=[value] pair, here:%s\n", text.c_str());
126 return false;
127 }
128 if(pos >= (text.size() - 1))
129 {
130 printf("cant find value after \"=\", here:%s\n", text.c_str());
131 return false;
132 }
133 auto key = text.substr(0, pos);
134 auto value = text.substr(pos + 1);
135 if(input_map.count(key) == 0)
136 {
137 printf("no such arg:%s\n", key.c_str());
138 return false;
139 }
140 input_map[key].value = value;
141 }
142 }
143 return true;
144 }
145
146 std::string get_str(const std::string& name) const
147 {
148 std::string value = input_map.at(name).value;
149 return value;
150 }
151
152 int get_int(const std::string& name) const
153 {
154 int value = atoi(input_map.at(name).value.c_str());
155 return value;
156 }
157
158 uint32_t get_uint32(const std::string& name) const
159 {
160 uint32_t value = strtoul(input_map.at(name).value.c_str(), nullptr, 10);
161 return value;
162 }
163
164 uint64_t get_uint64(const std::string& name) const
165 {
166 uint64_t value = strtoull(input_map.at(name).value.c_str(), nullptr, 10);
167 return value;
168 }
169
170 bool get_bool(const std::string& name) const
171 {
172 auto v = input_map.at(name).value;
173 if(v.compare("t") == 0 || v.compare("true") == 0)
174 return true;
175 if(v.compare("f") == 0 || v.compare("false") == 0)
176 return false;
177 int value = atoi(v.c_str());
178 return value == 0 ? false : true;
179 }
180
181 float get_float(const std::string& name) const
182 {
183 double value = atof(input_map.at(name).value.c_str());
184 return static_cast<float>(value);
185 }
186
187 double get_double(const std::string& name) const
188 {
189 double value = atof(input_map.at(name).value.c_str());
190 return value;
191 }
192
193 std::vector<std::string> get_string_vec(const std::string& name,
194 const std::string& delimiter = ",") const
195 {
196 if(get_str(name).empty())
197 {
198 return {};
199 }
200 std::string s = get_str(name);
201 std::vector<std::string> tokens;
202 size_t pos = 0;
203 std::string token;
204 while((pos = s.find(delimiter)) != std::string::npos)
205 {
206 token = s.substr(0, pos);
207 tokens.push_back(token);
208 s.erase(0, pos + delimiter.length());
209 }
210 tokens.push_back(s);
211
212 return tokens;
213 }
214
215 std::vector<int> get_int_vec(const std::string& name, const std::string& delimiter = ",") const
216 {
217 if(get_str(name).empty())
218 {
219 return {};
220 }
221 const std::vector<std::string> args = get_string_vec(name, delimiter);
222 std::vector<int> tokens;
223 tokens.reserve(static_cast<int>(args.size()));
224 for(const std::string& token : args)
225 {
226 int value = atoi(token.c_str());
227 tokens.push_back(value);
228 }
229 return tokens;
230 }
231
232 private:
233 std::unordered_map<std::string, Arg> input_map;
234 std::vector<std::string> keys;
235};
236} // namespace ck_tile
Definition arg_parser.hpp:28
std::string help_text
Definition arg_parser.hpp:32
std::string value
Definition arg_parser.hpp:31
std::string name
Definition arg_parser.hpp:30
ArgParser & insert(const std::string &_name, const std::string &_default_value, const std::string &_help_text)
Definition arg_parser.hpp:36
void print() const
Definition arg_parser.hpp:56
double get_double(const std::string &name) const
Definition arg_parser.hpp:187
bool parse(int argc, char *argv[], int start_index=1)
Definition arg_parser.hpp:98
std::string get_str(const std::string &name) const
Definition arg_parser.hpp:146
ArgParser()
Definition arg_parser.hpp:35
uint32_t get_uint32(const std::string &name) const
Definition arg_parser.hpp:158
uint64_t get_uint64(const std::string &name) const
Definition arg_parser.hpp:164
bool get_bool(const std::string &name) const
Definition arg_parser.hpp:170
float get_float(const std::string &name) const
Definition arg_parser.hpp:181
int get_int(const std::string &name) const
Definition arg_parser.hpp:152
std::vector< std::string > get_string_vec(const std::string &name, const std::string &delimiter=",") const
Definition arg_parser.hpp:193
std::vector< int > get_int_vec(const std::string &name, const std::string &delimiter=",") const
Definition arg_parser.hpp:215
Definition tile/core/algorithm/cluster_descriptor.hpp:13
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1697
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136