13 #include <unordered_map>
37 const std::string& _default_value,
38 const std::string& _help_text)
42 in.
value = _default_value;
45 if(input_map.count(_name) != 0)
47 printf(
"arg:%s already exist\n", _name.c_str());
51 input_map[_name] = in;
52 keys.push_back(_name);
59 std::string::size_type max_key_length = 11;
62 if(max_key_length < key.length())
64 max_key_length = key.length();
71 auto value = input_map.at(key);
72 std::vector<std::string> help_text_lines;
74 for(
size_t next_pos =
value.help_text.find(
'\n', pos); next_pos != std::string::npos;)
76 help_text_lines.push_back(std::string(
value.help_text.begin() + pos,
77 value.help_text.begin() + next_pos++));
79 next_pos =
value.help_text.find(
'\n', pos);
81 help_text_lines.push_back(
82 std::string(
value.help_text.begin() + pos,
value.help_text.end()));
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
89 for(
auto help_next_line = std::next(help_text_lines.begin());
90 help_next_line != help_text_lines.end();
93 std::cout << std::setw(1 + max_key_length + 4) <<
" " << *help_next_line
98 bool parse(
int argc,
char* argv[],
int start_index = 1)
100 if(argc < start_index)
102 printf(
"not enough args\n");
105 for(
int i = start_index; i < argc; i++)
107 char* cur_arg = argv[i];
108 if(cur_arg[0] !=
'-')
110 printf(
"illegal input\n");
116 std::string text(cur_arg + 1);
122 auto pos = text.find(
'=');
123 if(pos == std::string::npos)
125 printf(
"arg should be [key]=[value] pair, here:%s\n", text.c_str());
128 if(pos >= (text.size() - 1))
130 printf(
"cant find value after \"=\", here:%s\n", text.c_str());
133 auto key = text.substr(0, pos);
134 auto value = text.substr(pos + 1);
135 if(input_map.count(key) == 0)
137 printf(
"no such arg:%s\n", key.c_str());
140 input_map[key].value =
value;
146 std::string
get_str(
const std::string& name)
const
148 std::string
value = input_map.at(name).value;
154 int value = atoi(input_map.at(name).value.c_str());
160 uint32_t value = strtoul(input_map.at(name).value.c_str(),
nullptr, 10);
166 uint64_t value = strtoull(input_map.at(name).value.c_str(),
nullptr, 10);
172 auto v = input_map.at(name).value;
173 if(v.compare(
"t") == 0 || v.compare(
"true") == 0)
175 if(v.compare(
"f") == 0 || v.compare(
"false") == 0)
177 int value = atoi(v.c_str());
178 return value == 0 ? false :
true;
183 double value = atof(input_map.at(name).value.c_str());
184 return static_cast<float>(
value);
189 double value = atof(input_map.at(name).value.c_str());
194 const std::string& delimiter =
",")
const
201 std::vector<std::string> tokens;
204 while((pos = s.find(delimiter)) != std::string::npos)
206 token = s.substr(0, pos);
207 tokens.push_back(token);
208 s.erase(0, pos + delimiter.length());
215 std::vector<int>
get_int_vec(
const std::string& name,
const std::string& delimiter =
",")
const
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)
226 int value = atoi(token.c_str());
227 tokens.push_back(
value);
233 std::unordered_map<std::string, Arg> input_map;
234 std::vector<std::string> keys;
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
Definition: arg_parser.hpp:24
std::vector< int > get_int_vec(const std::string &name, const std::string &delimiter=",") const
Definition: arg_parser.hpp:215
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
ArgParser & insert(const std::string &_name, const std::string &_default_value, const std::string &_help_text)
Definition: arg_parser.hpp:36
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
Definition: cluster_descriptor.hpp:13
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1350
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136