-
Notifications
You must be signed in to change notification settings - Fork 36
/
args-parser.linq
102 lines (98 loc) · 1.9 KB
/
args-parser.linq
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
<Query Kind="Statements" />
string file = @"D:\GitProjects\Righthand\Cake\Cake.Docker\src\Cake.Docker\SwarmInit\args.txt";
string[] lines = File.ReadAllLines(file);
int delimiter = -1;
string firstLine = lines[0];
for (int i = 0; i < firstLine.Length; i++)
{
if (firstLine[i] == ' ' && firstLine[i + 1] != '-')
{
delimiter = i;
break;
}
}
foreach (string line in lines)
{
string cmd = line.Substring(0,delimiter).Trim();
string info = line.Substring(delimiter+1).Trim();
if (!string.IsNullOrEmpty(info) && !string.IsNullOrEmpty(cmd))
{
int pos = cmd.IndexOf("--") + 2;
int eqPos = cmd.IndexOf('=', pos);
string raw;
bool isBool;
bool isInt;
bool isArray;
bool isTimeSpan;
string equals;
if (eqPos >= 0)
{
isBool = false;
raw = cmd.Substring(pos, eqPos - pos);
equals = cmd.Substring(eqPos + 1);
isArray = cmd.EndsWith("[]");
int tmp;
isInt = int.TryParse(equals, out tmp);
}
else
{
if (info.StartsWith("value", StringComparison.Ordinal))
{
info = info.Substring("value".Length).TrimStart();
}
else if (info.StartsWith("duration", StringComparison.Ordinal))
{
isTimeSpan = true;
}
isBool = true;
raw = cmd.Substring(pos);
equals = null;
}
string name = "";
bool upperCase = true;
foreach (char c in raw)
{
if (upperCase)
{
name += char.ToUpper(c);
upperCase = false;
}
else
{
if (c == '-')
{
upperCase = true;
}
else
{
name += c;
}
}
}
"/// <summary>".Dump();
("/// " + info).Dump();
"/// </summary>".Dump();
string type;
switch (name.ToLower())
{
case "env":
type = "Dictionary<string, string>";
break;
default:
if (isBool)
{
type = "bool";
}
else if (isInt)
{
type = "int?";
}
else
{
type = "string" + (isArray ? "[]" : "");
}
break;
}
$"public {type} {name} {{ get; set; }}".Dump();
}
}