Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 450 | arune | 1 | //http://www.codeproject.com/csharp/Command_Line.asp |
| 2 | |||
| 3 | using System; |
||
| 4 | using System.Collections.Specialized; |
||
| 5 | using System.Text.RegularExpressions; |
||
| 6 | |||
| 7 | //namespace CommandLine.Utility |
||
| 8 | //{ |
||
| 9 | /// <summary> |
||
| 10 | /// Arguments class |
||
| 11 | /// </summary> |
||
| 12 | public class Arguments{ |
||
| 13 | // Variables |
||
| 14 | private StringDictionary Parameters; |
||
| 15 | |||
| 16 | // Constructor |
||
| 17 | public Arguments(string[] Args) |
||
| 18 | { |
||
| 19 | Parameters = new StringDictionary(); |
||
| 20 | Regex Spliter = new Regex(@"^-{1,2}|^/|=|:", |
||
| 21 | RegexOptions.IgnoreCase|RegexOptions.Compiled); |
||
| 22 | |||
| 23 | Regex Remover = new Regex(@"^['""]?(.*?)['""]?$", |
||
| 24 | RegexOptions.IgnoreCase|RegexOptions.Compiled); |
||
| 25 | |||
| 26 | string Parameter = null; |
||
| 27 | string[] Parts; |
||
| 28 | |||
| 29 | // Valid parameters forms: |
||
| 30 | // {-,/,--}param{ ,=,:}((",')value(",')) |
||
| 31 | // Examples: |
||
| 32 | // -param1 value1 --param2 /param3:"Test-:-work" |
||
| 33 | // /param4=happy -param5 '--=nice=--' |
||
| 34 | foreach(string Txt in Args) |
||
| 35 | { |
||
| 36 | // Look for new parameters (-,/ or --) and a |
||
| 37 | // possible enclosed value (=,:) |
||
| 38 | Parts = Spliter.Split(Txt,3); |
||
| 39 | |||
| 40 | switch(Parts.Length){ |
||
| 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 | //} |