-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.cpp
More file actions
128 lines (121 loc) · 3.59 KB
/
Copy pathargs.cpp
File metadata and controls
128 lines (121 loc) · 3.59 KB
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
#include "args.hpp"
#include <iostream>
#include <stdlib.h>
#include <getopt.h>
vector<ArgumentInfo*>* info_list;
ArgumentInfo::ArgumentInfo(
string type, string name, string default_value, string description, bool optional, void* reference)
: type_(type), name_(name), default_value_(default_value),
description_(description), optional_(optional), reference_(reference)
{
if (info_list == NULL) {
info_list = new vector<ArgumentInfo*>;
}
info_list->push_back(this);
}
void ArgumentInfo::Print()
{
cout<<string(8, ' ')<<"--"<<name_
<<string(max(int(14 - name_.length()), 1), ' ')
<<description_<<endl<<string(24, ' ')<<"type: "<<type_;
if (optional_) {
cout<<endl<<string(24, ' ')<<"default value: "<<default_value_;
}
cout<<endl;
}
bool ArgumentInfo::Assign(const string& value)
{
char* p;
if (type_ == "int") {
if (value.empty()) {
return false;
}
*(int*)reference_ = strtol(value.c_str(), &p, 10);
if (p != value.c_str() + value.size()) {
return false;
}
} else if (type_ == "string") {
*(string*)reference_ = value;
} else if (type_ == "bool") {
string s;
for (int i = 0; i < value.size(); ++i) {
s += tolower(value[i]);
}
if (s == "true") {
*(bool*)reference_ = true;
} else if (s == "false") {
*(bool*)reference_ = false;
} else {
return false;
}
}
return true;
}
void PrintUsage()
{
cout<<"Usage: jcore [options] "<<endl;
for (int i = 0; i < info_list->size(); ++i) {
(*info_list)[i]->Print();
}
}
int ParseArguments(int argc, const char* argv[])
{
if (info_list == NULL)
info_list = new vector<ArgumentInfo*>;
vector<bool> assigned(info_list->size(), false);
for (int i = 1; i < argc; ++i)
{
if (argv[i][0] == '-' && argv[i][1] == '-')
{
const char* p = argv[i] + 2;
while (*p && *p != '=')
++p;
string name(argv[i] + 2, p - argv[i] - 2);
bool found = false;
for (int j = 0; j < info_list->size(); ++j)
{
if ((*info_list)[j]->name() == name)
{
if (*p)
{
if (!(*info_list)[j]->Assign(p + 1))
{
cerr<<"Invalid value for argument "<<name<<endl;
PrintUsage();
return -1;
}
}
else
{
if ((*info_list)[j]->type() != "bool")
{
cerr<<"Missing value for argument "<<name<<endl;
PrintUsage();
return -1;
}
(*info_list)[j]->Assign("true");
}
found = true;
assigned[j] = true;
break;
}
}
if (!found)
{
cerr<<"Invalid argument "<<name<<endl;
PrintUsage();
return -1;
}
}
}
for (int i = 0; i < info_list->size(); ++i)
{
if (!assigned[i] && !(*info_list)[i]->IsOptional())
{
cerr<<"Missing argument "<<(*info_list)[i]->name()<<endl;
PrintUsage();
return -1;
}
}
return 0;
}