-
Notifications
You must be signed in to change notification settings - Fork 20
/
CommandLineParser.cs
35 lines (32 loc) · 1.15 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
using System.Text.RegularExpressions;
class CommandLine
{
public Dictionary<string, object> Parameters { get; set; } = new Dictionary<string, object>();
public Dictionary<string, object> Variables { get; set; } = new Dictionary<string, object>();
public static CommandLine Parse(string[] args)
{
CommandLine cl = new CommandLine();
for(int i=0;i<args.Length - 1;i++)
{
if(args[i].StartsWith("--") == false)
continue;
bool variable = args[i].StartsWith("--var:");
string arg = args[i][(variable ? 6 : 2)..];
object value;
string strValue = args[++i];
if(Regex.IsMatch(strValue, "^[\\d]+$") && int.TryParse(strValue, out int iValue))
value = iValue;
else if(strValue == "true")
value = true;
else if(strValue == "false")
value = false;
else
value = strValue;
if(variable)
cl.Variables.Add(arg, value);
else
cl.Parameters.Add(arg, value);
}
return cl;
}
}