OSDN Git Service

na-get-lib,パフォーマンスチューニングなど(動作変更なし)
[applistation/AppliStation.git] / na-get-lib / NaGet / ArgParser.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text.RegularExpressions;\r
4 \r
5 namespace NaGet\r
6 {\r
7         /// <summary>\r
8         /// \92á\8b@\94\\82È\88ø\90\94\89ð\90Í\82ð\82·\82é\81B\r
9         /// </summary>\r
10         public class ArgParser\r
11         {\r
12                 /// <summary>\r
13                 /// \89ð\90Í\8c\8b\89Ê\r
14                 /// </summary>\r
15                 private Dictionary<string,object> opts;\r
16                 \r
17                 /// <summary>\r
18                 /// \89ð\90Í\8c\8b\89Ê\82ð\83n\83b\83V\83\85\8e«\8f\91\82Æ\82µ\82Ä\8eæ\93¾\r
19                 /// </summary>\r
20                 public Dictionary<string, object> Opts {\r
21                         get {\r
22                                 lock (opts) {\r
23                                         return opts;\r
24                                 }\r
25                         }\r
26                 }\r
27                 \r
28                 public ArgParser()\r
29                         : this(new Dictionary<string,object>())\r
30                 {\r
31                 }\r
32                 \r
33                 /// <summary>\r
34                 /// \8f\89\8aú\89»\r
35                 /// </summary>\r
36                 /// <param name="hashDefault">\8f\89\8aú\82Ì\8e«\8f\91</param>\r
37                 public ArgParser(Dictionary<string,object> hashDefault)\r
38                 {\r
39                         opts = hashDefault;\r
40                 }\r
41                 \r
42                 /// <summary>\r
43                 /// \83n\83b\83V\83\85\82Ì\92\86\90g\82ð\90Ý\92è\82 \82é\82¢\82Í\8eæ\93¾\82·\82é\81B\r
44                 /// </summary>\r
45                 public object this[string key] {\r
46                         set {\r
47                                 lock(opts) {\r
48                                         opts[key] = value;\r
49                                 }\r
50                         }\r
51                         get {\r
52                                 lock(opts) {\r
53                                         return opts[key];\r
54                                 }\r
55                         }\r
56                 }\r
57                 \r
58                 /// <summary>\r
59                 /// \88ø\90\94\82ð\89ð\90Í\82·\82é\r
60                 /// </summary>\r
61                 /// <param name="args">\91Î\8fÛ\88ø\90\94</param>\r
62                 /// <returns>\8ec\82Á\82½(\83I\83v\83V\83\87\83\93\82Å\82Í\82È\82¢)\88ø\90\94</returns>\r
63                 /// <exception cref="ApplicationException">\88ø\90\94\82ª\95s\90³\82È\82Æ\82«\82Ü\82½\82Í\81A\91\8dÝ\82µ\82È\82¢\83I\83v\83V\83\87\83\93\82ª\91\8dÝ\82·\82é\82Æ\82«</exception>\r
64                 public string[] Parse(string[] args)\r
65                 {\r
66                         List<string> rest = new List<string>();\r
67                         lock (opts) {\r
68                                 bool disableOptionParse = false;\r
69                                 for (int i = 0; i < args.Length; i++) {\r
70                                         if (disableOptionParse || !args[i].StartsWith("-")) {\r
71                                                 rest.Add(args[i]);\r
72                                         } else {\r
73                                                 if (args[i].StartsWith("-")) {\r
74                                                         if (args[i] == "--") {\r
75                                                                 // \83I\83v\83V\83\87\83\93\89ð\90Í\92\86\92f\r
76                                                                 disableOptionParse = true;\r
77                                                         } else {\r
78                                                                 parseOption(args[i]);\r
79                                                         }\r
80                                                 }\r
81                                         }\r
82                                 }\r
83                         }\r
84                         \r
85                         return rest.ToArray();\r
86                 }\r
87                 \r
88                 /// <summary>\r
89                 /// \83I\83v\83V\83\87\83\93\83g\81[\83N\83\93\82ð\89ð\90Í\81A\93Ç\82Ý\8d\9e\82Ý\82ð\82¨\82±\82È\82¤\r
90                 /// </summary>\r
91                 /// <param name="option">\83I\83v\83V\83\87\83\93\83g\81[\83N\83\93\95\8e\9a\97ñ</param>\r
92                 /// <exception cref="ApplicationException">\88ø\90\94\82ª\95s\90³\82È\82Æ\82«\82Ü\82½\82Í\81A\91\8dÝ\82µ\82È\82¢\83I\83v\83V\83\87\83\93\82Ì\8fê\8d\87</exception>\r
93                 private void parseOption(string option)\r
94                 {\r
95                         Match match = Regex.Match(option, "^--?([^=]+)(=\"?(.+)\"?)?$");\r
96                         if (match.Success) {\r
97                                 string key = match.Groups[1].Value;\r
98                                 string val = match.Groups[3].Value;\r
99                                 \r
100                                 if (opts.ContainsKey(key)) { // \93o\98^\8c\8b\89Ê\82É\93ü\82Á\82Ä\82¢\82é\82±\82Æ\r
101                                         object origVal = opts[key];\r
102                                         \r
103                                         try {\r
104                                                 if (origVal is bool) {\r
105                                                         opts[key] = val.ToLower() != "no";\r
106                                                 } else if (origVal is int) {\r
107                                                         opts[key] = int.Parse(val);\r
108                                                 } else /* if (origVal is string) */ {\r
109                                                         opts[key] = val;\r
110                                                 }\r
111                                         } catch (FormatException) {\r
112                                                 throw new ApplicationException(string.Format("Illegal Format For {0}", key));\r
113                                         }\r
114                                 } else {\r
115                                         throw new ApplicationException(string.Format("Undefined option: {0}", key));\r
116                                 }\r
117                         }\r
118                 }\r
119         }\r
120 }\r