-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLineParser.cs
168 lines (158 loc) · 5.64 KB
/
CommandLineParser.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class CommandLineParser
{
public Dictionary<string, List<string>> args = new Dictionary<string, List<string>>();
public static CommandLineParser Parse() => Parse(Environment.CommandLine);
public static CommandLineParser Parse(string cmdLine)
{
CommandLineParser result = new CommandLineParser();
string[] args = cmdLine.Split(' ');
string current = "";
List<string> accumulator = new List<string>();
string searchForEnd = "";
foreach (string item in args)
{
string item0 = item.Trim('\r', '\n');
string current0 = "";
if (item0.StartsWith("-") || item0.StartsWith("/"))
current0 = item0.Substring(1);
if (item0.StartsWith("--"))
current0 = item0.Substring(2);
if (current0.Length > 0)
{
if (current.Length > 0)
{
if (!result.args.ContainsKey(current))
result.args[current] = new List<string>();
if (accumulator.Count > 0)
result.args[current].Add(string.Join(" ", accumulator.ToArray()));
else
result.args[current].Add("true");
}
current = current0;
accumulator.Clear();
searchForEnd = "";
}
else
{
if (searchForEnd.Length > 0)
{
if (item0.EndsWith(searchForEnd))
{
accumulator.Add(item0.Substring(0, item0.Length - searchForEnd.Length));
searchForEnd = "";
}
else
accumulator.Add(item0);
}
else
{
if (item0.StartsWith("'") && accumulator.Count == 0)
{
var tmp = item0.Trim('\'');
accumulator.Add(tmp);
searchForEnd = "'";
}
else if (item0.StartsWith("\"") && accumulator.Count == 0)
{
var tmp = item0.Trim('\"');
accumulator.Add(tmp);
searchForEnd = "\"";
}
else if (item0.StartsWith("#") && accumulator.Count == 0)
{
var tmp = item0.Trim('#');
accumulator.Add(tmp);
searchForEnd = "#";
}
else
accumulator.Add(item0);
}
}
}
if (current.Length > 0)
{
if (!result.args.ContainsKey(current))
result.args[current] = new List<string>();
if (accumulator.Count > 0)
result.args[current].Add(string.Join(" ", accumulator.ToArray()));
else
result.args[current].Add("true");
}
return result;
}
public string GetString(string name, string _default = "", int index = 0)
{
string result = _default;
if (args.ContainsKey(name))
{
var list = args[name];
if (index < list.Count)
result = list[index];
}
return result;
}
public bool GetBool(string name, bool _default = false, int index = 0)
{
var tmp = GetString(name, "", index);
if (tmp == "true")
return true;
else
return _default;
}
public int GetInt32(string name, int _default = -1, int index = 0)
{
var tmp = GetString(name, _default.ToString(), index);
int result = 0;
if (!int.TryParse(tmp, out result))
result = _default;
return result;
}
// Try to find the file, absolute, running directory, binnary directory
public FileInfo GetFile(string name, string _default = "", int index = 0)
{
string file = GetString(name, _default, index);
FileInfo fi = new FileInfo(name);
if (fi.Exists)
return fi;
fi = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), file));
if (fi.Exists)
return fi;
fi = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(file)));
if (fi.Exists)
return fi;
fi = new FileInfo(Path.Combine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), Path.GetFileName(file)));
if (fi.Exists)
return fi;
if (!string.IsNullOrEmpty(file))
fi = new FileInfo(Path.Combine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), file));
return fi;
}
public string GetEnv(string name, string _default = "", int index = 0)
{
string result = GetString(name, _default, index);
if (!string.IsNullOrEmpty(result))
result = Environment.GetEnvironmentVariable(result);
return result;
}
// You can use the dictionnary or getters
public void Dump()
{
foreach (var arg in args)
{
Console.Write(arg.Key + "=");
string j = "";
int index = 0;
string sep = "";
while ((j = GetString(arg.Key, "", index++)) != "")
{
Console.Write(sep + j);
sep = ", ";
}
Console.WriteLine();
}
}
}