2 using System.Collections.Generic;
\r
3 using System.Text.RegularExpressions;
\r
8 ///
\92á
\8b@
\94\
\82È
\88ø
\90\94\89ð
\90Í
\82ð
\82·
\82é
\81B
\r
10 public class ArgParser
\r
13 ///
\89ð
\90Í
\8c\8b\89Ê
\r
15 private Dictionary<string,object> opts;
\r
18 ///
\89ð
\90Í
\8c\8b\89Ê
\82ð
\83n
\83b
\83V
\83\85\8e«
\8f\91\82Æ
\82µ
\82Ä
\8eæ
\93¾
\r
20 public Dictionary<string, object> Opts {
\r
29 : this(new Dictionary<string,object>())
\r
36 /// <param name="hashDefault">
\8f\89\8aú
\82Ì
\8e«
\8f\91</param>
\r
37 public ArgParser(Dictionary<string,object> hashDefault)
\r
43 ///
\83n
\83b
\83V
\83\85\82Ì
\92\86\90g
\82ð
\90Ý
\92è
\82 \82é
\82¢
\82Í
\8eæ
\93¾
\82·
\82é
\81B
\r
45 public object this[string key] {
\r
59 ///
\88ø
\90\94\82ð
\89ð
\90Í
\82·
\82é
\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
65 List<string> rest = new List<string>();
\r
67 bool disableOptionParse = false;
\r
68 for (int i = 0; i < args.Length; i++) {
\r
69 if (disableOptionParse || !args[i].StartsWith("-")) {
\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
77 parseOption(args[i]);
\r
84 return rest.ToArray();
\r
88 ///
\83I
\83v
\83V
\83\87\83\93\83g
\81[
\83N
\83\93\82Ì
\89ð
\90Í
\r
90 /// <param name="option"></param>
\r
91 private void parseOption(string option)
\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
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
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
109 } catch (FormatException) {
\r
110 throw new ApplicationException(string.Format("Illegal Format For {0}", key));
\r
113 throw new ApplicationException(string.Format("Undefined option: {0}", key));
\r