corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | using System; |
2 | using System.Collections.Specialized; |
||
3 | using System.Text.RegularExpressions; |
||
4 | |||
5 | namespace CommandLine.Utility |
||
6 | { |
||
7 | /// <summary> |
||
8 | /// Arguments class |
||
9 | /// </summary> |
||
10 | public class Arguments |
||
11 | { |
||
12 | // Variables |
||
13 | private StringDictionary Parameters; |
||
14 | |||
15 | // Constructor |
||
16 | public Arguments(string[] Args) |
||
17 | { |
||
18 | Parameters = new StringDictionary(); |
||
19 | Regex Splitter = new Regex(@"^-{1,2}|=", |
||
20 | RegexOptions.IgnoreCase | RegexOptions.Compiled); |
||
21 | |||
22 | Regex Remover = new Regex(@"^['""]?(.*?)['""]?$", |
||
23 | RegexOptions.IgnoreCase | RegexOptions.Compiled); |
||
24 | |||
25 | string Parameter = null; |
||
26 | string[] Parts; |
||
27 | |||
28 | // Valid parameters forms: |
||
29 | // {-,/,--}param{ ,=,:}((",')value(",')) |
||
30 | // Examples: |
||
31 | // -param1 value1 --param2 |
||
32 | // /param4=happy -param5 '--=nice=--' |
||
33 | foreach (string Txt in Args) |
||
34 | { |
||
35 | // Look for new parameters (-,/ or --) and a |
||
36 | // possible enclosed value (=,:) |
||
37 | Parts = Splitter.Split(Txt, 3); |
||
38 | |||
39 | switch (Parts.Length) |
||
40 | { |
||
41 | // Found a value (for the last parameter |
||
42 | // found (space separator)) |
||
43 | case 1: |
||
44 | if (Parameter != null) |
||
45 | { |
||
46 | if (!Parameters.ContainsKey(Parameter)) |
||
47 | { |
||
48 | Parts[0] = |
||
49 | Remover.Replace(Parts[0], "$1"); |
||
50 | |||
51 | Parameters.Add(Parameter, Parts[0]); |
||
52 | } |
||
53 | Parameter = null; |
||
54 | } |
||
55 | // else Error: no parameter waiting for a value (skipped) |
||
56 | break; |
||
57 | |||
58 | // Found just a parameter |
||
59 | case 2: |
||
60 | // The last parameter is still waiting. |
||
61 | // With no value, set it to true. |
||
62 | if (Parameter != null) |
||
63 | { |
||
64 | if (!Parameters.ContainsKey(Parameter)) |
||
65 | Parameters.Add(Parameter, "true"); |
||
66 | } |
||
67 | Parameter = Parts[1]; |
||
68 | break; |
||
69 | |||
70 | // Parameter with enclosed value |
||
71 | case 3: |
||
72 | // The last parameter is still waiting. |
||
73 | // With no value, set it to true. |
||
74 | if (Parameter != null) |
||
75 | { |
||
76 | if (!Parameters.ContainsKey(Parameter)) |
||
77 | Parameters.Add(Parameter, "true"); |
||
78 | } |
||
79 | |||
80 | Parameter = Parts[1]; |
||
81 | |||
82 | // Remove possible enclosing characters (",') |
||
83 | if (!Parameters.ContainsKey(Parameter)) |
||
84 | { |
||
85 | Parts[2] = Remover.Replace(Parts[2], "$1"); |
||
86 | Parameters.Add(Parameter, Parts[2]); |
||
87 | } |
||
88 | |||
89 | Parameter = null; |
||
90 | break; |
||
91 | } |
||
92 | } |
||
93 | // In case a parameter is still waiting |
||
94 | if (Parameter != null) |
||
95 | { |
||
96 | if (!Parameters.ContainsKey(Parameter)) |
||
97 | Parameters.Add(Parameter, "true"); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | // Retrieve a parameter value if it exists |
||
102 | // (overriding C# indexer property) |
||
103 | public string this[string Param] |
||
104 | { |
||
105 | get |
||
106 | { |
||
107 | return (Parameters[Param]); |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | } |