* Refractored a bit.
[ngenerator.git] / CommandLine.cs
blob5cf5101e17d52ed2d66a3ced2a36eaa7d670cd05
1 // /home/jeremie/TaoParser/NGenerator/CommandLine.cs
2 // Writtent by jeremie at 21:43 03/06/2007
3 //
4 // This file is licensed under the LGPL licence as described in the COPYING file
6 using System;
7 using System.Collections.Generic;
9 namespace NGenerator
11 public class CommandLine
13 string[] args;
14 IDictionary<string, string> options;
15 //protected Option[] options;
16 string[] names;
17 char argSeparator = ':';
18 char argId = '-';
19 int nameIndex = -1;
21 public CommandLine(string[] argLine)
23 if (argLine == null)
24 throw new ArgumentNullException("argLine");
26 args = new string[argLine.Length];
27 argLine.CopyTo(args, 0);
30 public CommandLine(string[] argLine, char argument, char separator) :
31 this(argLine)
33 argSeparator = separator;
34 argId = argument;
37 /*public Option[] Options {
38 get {
39 if (options == null)
40 ParseOptions();
41 return options;
46 public string[] Names {
47 get {
48 if (names == null)
49 ParseNames();
50 string[] namesTemp = new string[names.Length];
51 Array.Copy(names, namesTemp, names.Length);
52 return namesTemp;
56 public bool IsDefined(string option)
58 if (options == null)
59 ParseOptions();
60 return options.ContainsKey(option);
63 public string this[string key] {
64 get {
65 string output = null;
66 options.TryGetValue(key, out output);
67 return output;
71 protected void ParseOptions()
73 if (options != null)
74 return;
76 options = new Dictionary<string, string>();
78 int index = -1;
80 foreach (string s in args) {
81 if (s[0] == argId) {
82 index++;
83 int indexOfSeparator = s.IndexOf(argSeparator);
85 // Determine the option name be it with a value or a switch
86 string optionName = (indexOfSeparator != -1) ?
87 s.Substring(1, (indexOfSeparator - 1)) : s.Substring(1);
89 // Retrieve the associated parameter value if any
90 string optionValue = (indexOfSeparator != -1) ?
91 s.Substring(indexOfSeparator + 1) : null;
93 options.Add(optionName, optionValue);
95 else {
96 break;
100 nameIndex = index + 1;
103 protected void ParseNames()
105 ICollection<string> temp = new List<string>();
107 if (nameIndex != -1)
108 for (int i = nameIndex; i < args.Length; i++)
109 temp.Add(args[i]);
110 else
111 foreach (string s in args)
112 if (s[0] != argId)
113 temp.Add(s);
115 if (temp.Count != 0) {
116 names = new string[temp.Count];
117 temp.CopyTo(names, 0);