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 /// <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
66 List<string> rest = new List<string>();
\r
68 bool disableOptionParse = false;
\r
69 for (int i = 0; i < args.Length; i++) {
\r
70 if (disableOptionParse || !args[i].StartsWith("-")) {
\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
78 parseOption(args[i]);
\r
85 return rest.ToArray();
\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
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
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
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
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
111 } catch (FormatException) {
\r
112 throw new ApplicationException(string.Format("Illegal Format For {0}", key));
\r
115 throw new ApplicationException(string.Format("Undefined option: {0}", key));
\r