-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparser.h
62 lines (53 loc) · 2.02 KB
/
argparser.h
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
#pragma once
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include "lpm/types.h"
namespace ArgParser {
// Check if the arguments provided and the command are valid.
Command::Type validate(std::string& command, args_t& args, const args_map_t& valid_args) {
for (int index = 0; auto& valid_arg_pair : valid_args) {
// Check if the command provided is valid.
if (valid_arg_pair.first == command) {
auto& valid_values = valid_arg_pair.second;
// We need to check if all the user-provided arguments
// are valid for this command.
for (auto& user_arg : args) {
if (
std::find(
valid_values.begin(),
valid_values.end(),
user_arg.first
) == valid_values.end()
) {
LPM_PRINT_ERROR("Unrecognized parameter: " << user_arg.first);
return Command::Type::UNKNOWN;
}
}
return (Command::Type) index;
}
++index;
}
LPM_PRINT_ERROR("Unrecognized command: " << command);
return Command::Type::UNKNOWN;
}
args_t parse(int& argc, char* argv[], int start_index) {
args_t args;
for (int i = start_index; i < argc; i++) {
if (argv[i][0] == '-') {
// Assign argv[i] to a new string and remove all leading dashes.
std::string key = argv[i];
size_t pos = key.find_first_not_of('-');
key = key.substr(pos);
// Check if there is a next argument and that it doesn't start with a '-'
if (i + 1 < argc && argv[i + 1][0] != '-') {
args[key] = argv[i + 1];
} else {
args[key] = "";
}
}
}
return args;
}
}