-
Notifications
You must be signed in to change notification settings - Fork 21
/
ClassArguments.cs
99 lines (95 loc) · 3.8 KB
/
ClassArguments.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
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
namespace GitForce
{
/// <summary>
/// Arguments class
/// </summary>
public class Arguments
{
// Variables
private readonly StringDictionary parameters;
private readonly List<string> unassociated = new List<string>();
// Constructor
public Arguments(string[] args)
{
parameters = new StringDictionary();
Regex spliter = new Regex(@"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex remover = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
string parameter = null;
string[] parts;
// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples: -param1 value1 --param2 /param3:"Test-:-work" /param4=happy -param5 '--=nice=--'
foreach (string txt in args)
{
// Look for new parameters (-,/ or --) and a possible enclosed value (=,:)
parts = spliter.Split(txt, 3);
switch (parts.Length)
{
// Found a value (for the last parameter found (space separator))
case 1:
if (parameter != null)
{
if (!parameters.ContainsKey(parameter))
{
parts[0] = remover.Replace(parts[0], "$1");
parameters.Add(parameter, parts[0]);
}
parameter = null;
}
else
{
// No parameter waiting for a value
unassociated.Add(parts[0]);
}
break;
// Found just a parameter
case 2:
// The last parameter is still waiting. With no value, set it to true.
if (parameter != null)
{
if (!parameters.ContainsKey(parameter)) parameters.Add(parameter, "true");
}
parameter = parts[1];
break;
// Parameter with enclosed value
case 3:
// The last parameter is still waiting. With no value, set it to true.
if (parameter != null)
{
if (!parameters.ContainsKey(parameter)) parameters.Add(parameter, "true");
}
parameter = parts[1];
// Remove possible enclosing characters (",')
if (!parameters.ContainsKey(parameter))
{
parts[2] = remover.Replace(parts[2], "$1");
parameters.Add(parameter, parts[2]);
}
parameter = null;
break;
}
}
// In case a parameter is still waiting
if (parameter != null)
{
if (!parameters.ContainsKey(parameter)) parameters.Add(parameter, "true");
}
}
// Retrieve a parameter value if it exists
public string this[string param]
{
get
{
return (parameters[param]);
}
}
// Returns all arguments not associated with any --parameter
public List<string> GetUnassociatedArgs()
{
return unassociated;
}
}
}