-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparseargs.hpp
119 lines (109 loc) · 3.5 KB
/
parseargs.hpp
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
#pragma once
#include <iostream>
#include <list>
#include <string>
#include "engine/exception.hpp"
struct Option {
bool isRequired;
std::string strShort;
std::string strLong;
std::string strHelp;
std::string strValue;
Option(const char* Short, const char* Long, const char* Help, bool required)
: strShort(Short), strLong(Long), strHelp(Help), strValue(""), isRequired(required) {}
std::string Show() {
if (!isRequired) {
return "\t" + strShort + " " + strLong + "\toptional " + strHelp;
}
return "\t" + strShort + " " + strLong + "\trequired " + strHelp;
}
};
struct Command {
std::string strCmd;
std::string strHelp;
std::list<Option> options;
Command(std::string cmd, std::string help, std::list<Option> options)
: strCmd(cmd), strHelp(help), options(options) {}
std::string JoinOptions() {
std::string result;
for (auto iter : options) {
result += iter.strShort;
result += " ";
}
return result;
}
bool IsValid() {
for (auto iter : options) {
if (iter.isRequired && iter.strValue.size() == 0) {
return false;
}
}
return true;
}
};
class ParseArgs {
protected:
Command mCmd;
std::list<Command> mAvaliableCmds;
// app -c=xxxxx update
// app -c=xxxxx scan -host -ports
public:
void PrintHelp(std::string AppName) {
for (auto iter : mAvaliableCmds) {
std::cout << AppName << " " << iter.strCmd << " [" << iter.JoinOptions() << "] "
<< iter.strHelp << std::endl;
std::cout << "options:" << std::endl;
for (auto opt : iter.options) {
std::cout << opt.Show() << std::endl;
}
}
}
explicit ParseArgs(int argc, char* argv[], std::list<Command> allCmds)
: mCmd("", "", std::list<Option>()), mAvaliableCmds(allCmds) {
if (argc < 2) {
return;
}
for (auto iter : allCmds) {
if (iter.strCmd == argv[1]) {
mCmd = iter;
break;
}
}
if (mCmd.strCmd.size() == 0) {
return;
}
for (int i = 2; i < argc; i++) {
std::string str = argv[i];
for (std::list<Option>::iterator iter = mCmd.options.begin();
iter != mCmd.options.end(); iter++) {
if (str == iter->strShort || str == iter->strLong || str.find(iter->strLong) == 0) {
if (str == iter->strShort || str == iter->strLong) {
if (argc < i + 1) {
throw Interpreter::RuntimeException("invalid option " + str);
}
iter->strValue = argv[i + 1];
i++;
break;
}
iter->strValue = GetValue(str);
break;
}
}
}
}
bool IsValid() { return mCmd.strCmd.size() > 0 && mCmd.IsValid(); }
std::string GetCommand() { return mCmd.strCmd; }
std::string GetOption(std::string opt) {
for (auto iter : mCmd.options) {
if (iter.strShort == opt || iter.strLong == opt) {
return iter.strValue;
}
}
return "";
}
private:
std::string GetValue(std::string& src) {
size_t pos = src.find("=");
return src.substr(pos + 1);
}
};