-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathCommandLineParser.cs
More file actions
265 lines (244 loc) · 10.1 KB
/
CommandLineParser.cs
File metadata and controls
265 lines (244 loc) · 10.1 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System.Collections;
namespace SQLBench
{
//
// Written by the Microsoft CSS SQL Networking Team
//
// Calling order:
//
// AddRule - n times
// string result = Parse(args) - once, result contains error message or "" if okay
// ArrayList List = GetArgs(argname) - once per arg, returns an ArrayList of 0..n CommandlineArgs
//
// Arguments can have the following properties:
//
// Case sensitive or case-insensitive
// Required or optional
// Appear once or many times (or 0 times if optional)
// Have a value or not
// If the arg name is "" then the value must not be proceeded by a flag that requires a value
//
// EXAMPLE USAGE:
//
// appname.exe filename [-out filename] [-g value] [-G] -h value [-flags value [-flags value [...]]]
//
// ArgRule(string argName, bool hasValue, bool allowDuplicates = false, bool caseInsensitive = true, bool required = true)
//
// ignore
// name value dup case req
// cp.AddRule(new ArgRule("", true, false, true, true)); // file name is required
// cp.AddRule(new ArgRule("flags", true, true, true, false)); // flags is optional but may appear more than once
// cp.AddRule(new ArgRule("out", true, false, true, false)); // out is optional but may only appear once
// cp.AddRule(new ArgRule("g", true, false, false, false)); // g is optional and case-sensitive
// cp.AddRule(new ArgRule("G", false, false, false, false)); // G is optional and takes no value and is case sensitive
// cp.AddRule(new ArgRule("h", true, false, true, true)); // h is required and takes an argument
//
// argements can appear in any order, as long as name/value pairs are adjacent
// arguments other than -g and -G are case-insensitive
// -flags may appear 0..n times
// -G can come before filename
// name value pairs can be: -x=y, -x:y, or -x y
//
// Test harness: cmdParser
//
/*!
* \brief
* \details
* \author Malcolm Stewart
* \version 2.0
* \date Feb 18th 2015
* \pre None
* \bug None
* \warning None
* \copyright Microsoft Corporation
*/
class CommandLineParser
{
public ArrayList Args = new ArrayList();
public ArrayList Rules = new ArrayList();
public string Parse(string[] args)
{
string lastToken = "";
string ruleViolation = "";
foreach (string arg in args)
{
string argName = "";
if (arg.StartsWith("-") || arg.StartsWith("/"))
{
string[] parts = null;
argName = arg.Substring(1); // strip leading - or /
//
// process lastToken, if it has a value
//
if (lastToken.Length > 0)
{
if (lastToken.StartsWith("-") || lastToken.StartsWith("/"))
{
ruleViolation = AddArg(lastToken.Substring(1), ""); // switch with no arguments
}
else
{
ruleViolation = AddArg("", lastToken); // switch-less argument
}
if (ruleViolation != "") return ruleViolation;
lastToken = "";
}
//
// does the switch have a value in the same arg ???
//
if (argName.Contains(":") || argName.Contains("="))
{
parts = argName.Split(new char[] {':', '='}, 2);
ruleViolation = AddArg(parts[0], parts[1]); // switch with argument separated by : or =
if (ruleViolation != "") return ruleViolation;
}
else
{
lastToken = arg; // switch - argument may be in next arg - wait for next iteration
}
}
else // switchless argument - process lastToken
{
if (lastToken.Length > 0)
{
if (lastToken.StartsWith("-") || lastToken.StartsWith("/"))
{
//
// Should the switch take an argument?
//
lastToken = lastToken.Substring(1); // trim - or /
ArgRule r = GetArgRule(lastToken);
if (r.fHasValue)
{
ruleViolation = AddArg(lastToken, arg); // switch with value in next argument
if (ruleViolation != "") return ruleViolation;
lastToken = "";
}
else
{
ruleViolation = AddArg(lastToken, ""); // switch takes no arguments, promote arg to lastToken
if (ruleViolation != "") return ruleViolation;
lastToken = arg;
}
}
else
{
ruleViolation = AddArg("", lastToken); // switch-less argument
if (ruleViolation != "") return ruleViolation;
lastToken = "";
}
}
else
{
lastToken = arg;
}
}
}
//
// Process lastToken if one exists
//
if (lastToken.Length > 0)
{
if (lastToken.StartsWith("-") || lastToken.StartsWith("/"))
{
ruleViolation = AddArg(lastToken.Substring(1), ""); // switch takes no arguments, promote arg to lastToken
if (ruleViolation != "") return ruleViolation;
}
else
{
ruleViolation = AddArg("", lastToken); // switch-less argument
if (ruleViolation != "") return ruleViolation;
}
}
if (ruleViolation != "") return ruleViolation;
//
// Evaluate missing required switches
//
foreach (ArgRule r in Rules)
{
if (r.fMustAppear && (GetArgs(r.name).Count == 0))
{
if (r.name == "")
return @"Required value is missing.";
else
return @"Required switch " + r.name + " is missing.";
}
}
return "";
}
private string AddArg(string name, string value)
{
// check rules
ArgRule r = GetArgRule(name);
if (r == null && name == "") return @"Invalid argument: " + value;
if (r == null) return @"Invalid switch: " + name;
if (r.fInsensitive) name = name.ToLower();
if (r.fDuplicates == false) if (GetArgs(name).Count > 0)
{
if (name == "")
return @"A switch-less value can only appear once.";
else
return @"Switch " + name + " can only appear once.";
}
if (r.fHasValue == false) if (value != "") return @"Switch " + name + " does not take an argument.";
if (r.fHasValue == true) if (value == "") return @"Switch " + name + " requires an argument.";
//
// if fall through, we add the argument
//
if (value == "") value = "true";
CommandLineArgs a = new CommandLineArgs();
a.name = name;
a.value = value;
Args.Add(a);
return "";
}
public ArrayList GetArgs(string name)
{
ArrayList outList = new ArrayList();
foreach (CommandLineArgs a in Args)
{
if (a.name == name)
outList.Add(a);
}
return outList;
}
private ArgRule GetArgRule(string name)
{
foreach (ArgRule r in Rules)
{
if (r.name == name)
return r;
if (r.fInsensitive && (r.name.ToLower() == name.ToLower()))
return r;
}
return null;
}
public void AddRule(ArgRule r)
{
Rules.Add(r);
}
}
public class CommandLineArgs
{
public string name = "";
public string value = "";
}
public class ArgRule
{
public string name = "";
public bool fHasValue = false;
public bool fDuplicates = false;
public bool fInsensitive = true;
public bool fMustAppear = true;
public ArgRule(string argName, bool hasValue, bool allowDuplicates = false, bool caseInsensitive = true, bool required = true)
{
name = caseInsensitive ? argName.ToLower() : argName;
fHasValue = hasValue;
fDuplicates = allowDuplicates;
fInsensitive = caseInsensitive;
fMustAppear = required;
}
}
}