OSDN Git Service

AppliStation,ハッシュが非整合の時に尋ねるようにする部分の改良
[applistation/AppliStation.git] / AppliStation / ArgParse.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text.RegularExpressions;\r
4 \r
5 namespace AppliStation\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 ArgParse\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 ArgParse()\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 ArgParse(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                 public string[] Parse(string[] args)\r
64                 {\r
65                         List<string> rest = new List<string>();\r
66                         lock (opts) {\r
67                                 bool disableOptionParse = false;\r
68                                 for (int i = 0; i < args.Length; i++) {\r
69                                         if (disableOptionParse || !args[i].StartsWith("-")) {\r
70                                                 rest.Add(args[i]);\r
71                                         } else {\r
72                                                 if (args[i].StartsWith("-")) {\r
73                                                         if (args[i] == "--") {\r
74                                                                 // \83I\83v\83V\83\87\83\93\89ð\90Í\92\86\92f\r
75                                                                 disableOptionParse = true;\r
76                                                         } else {\r
77                                                                 parseOption(args[i]);\r
78                                                         }\r
79                                                 }\r
80                                         }\r
81                                 }\r
82                         }\r
83                         \r
84                         return rest.ToArray();\r
85                 }\r
86                 \r
87                 /// <summary>\r
88                 /// \83I\83v\83V\83\87\83\93\83g\81[\83N\83\93\82Ì\89ð\90Í\r
89                 /// </summary>\r
90                 /// <param name="option"></param>\r
91                 private void parseOption(string option)\r
92                 {\r
93                         Match match = Regex.Match(option, "^--?([^=]+)(=\"?(.+)\"?)?$");\r
94                         if (match.Success) {\r
95                                 string key = match.Groups[1].Value;\r
96                                 string val = match.Groups[3].Value;\r
97                                 \r
98                                 if (opts.ContainsKey(key)) { // \93o\98^\8c\8b\89Ê\82É\93ü\82Á\82Ä\82¢\82é\82±\82Æ\r
99                                         object origVal = opts[key];\r
100                                         \r
101                                         try {\r
102                                                 if (origVal is bool) {\r
103                                                         opts[key] = val.ToLower() != "no";\r
104                                                 } else if (origVal is int) {\r
105                                                         opts[key] = int.Parse(val);\r
106                                                 } else /* if (origVal is string) */ {\r
107                                                         opts[key] = val;\r
108                                                 }\r
109                                         } catch (FormatException) {\r
110                                                 throw new ApplicationException(string.Format("Illegal Format For {0}", key));\r
111                                         }\r
112                                 } else {\r
113                                         throw new ApplicationException(string.Format("Undefined option: {0}", key));\r
114                                 }\r
115                         }\r
116                 }\r
117         }\r
118 }\r