Merge pull request #1725 from alexanderkyte/cache_crash
[mono.git] / mcs / class / Mono.Options / Mono.Options / Options.cs
1 //
2 // Options.cs
3 //
4 // Authors:
5 //  Jonathan Pryor <jpryor@novell.com>
6 //  Federico Di Gregorio <fog@initd.org>
7 //  Rolf Bjarne Kvinge <rolf@xamarin.com>
8 //
9 // Copyright (C) 2008 Novell (http://www.novell.com)
10 // Copyright (C) 2009 Federico Di Gregorio.
11 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 // Compile With:
34 //   gmcs -debug+ -r:System.Core Options.cs -o:NDesk.Options.dll
35 //   gmcs -debug+ -d:LINQ -r:System.Core Options.cs -o:NDesk.Options.dll
36 //
37 // The LINQ version just changes the implementation of
38 // OptionSet.Parse(IEnumerable<string>), and confers no semantic changes.
39
40 //
41 // A Getopt::Long-inspired option parsing library for C#.
42 //
43 // NDesk.Options.OptionSet is built upon a key/value table, where the
44 // key is a option format string and the value is a delegate that is 
45 // invoked when the format string is matched.
46 //
47 // Option format strings:
48 //  Regex-like BNF Grammar: 
49 //    name: .+
50 //    type: [=:]
51 //    sep: ( [^{}]+ | '{' .+ '}' )?
52 //    aliases: ( name type sep ) ( '|' name type sep )*
53 // 
54 // Each '|'-delimited name is an alias for the associated action.  If the
55 // format string ends in a '=', it has a required value.  If the format
56 // string ends in a ':', it has an optional value.  If neither '=' or ':'
57 // is present, no value is supported.  `=' or `:' need only be defined on one
58 // alias, but if they are provided on more than one they must be consistent.
59 //
60 // Each alias portion may also end with a "key/value separator", which is used
61 // to split option values if the option accepts > 1 value.  If not specified,
62 // it defaults to '=' and ':'.  If specified, it can be any character except
63 // '{' and '}' OR the *string* between '{' and '}'.  If no separator should be
64 // used (i.e. the separate values should be distinct arguments), then "{}"
65 // should be used as the separator.
66 //
67 // Options are extracted either from the current option by looking for
68 // the option name followed by an '=' or ':', or is taken from the
69 // following option IFF:
70 //  - The current option does not contain a '=' or a ':'
71 //  - The current option requires a value (i.e. not a Option type of ':')
72 //
73 // The `name' used in the option format string does NOT include any leading
74 // option indicator, such as '-', '--', or '/'.  All three of these are
75 // permitted/required on any named option.
76 //
77 // Option bundling is permitted so long as:
78 //   - '-' is used to start the option group
79 //   - all of the bundled options are a single character
80 //   - at most one of the bundled options accepts a value, and the value
81 //     provided starts from the next character to the end of the string.
82 //
83 // This allows specifying '-a -b -c' as '-abc', and specifying '-D name=value'
84 // as '-Dname=value'.
85 //
86 // Option processing is disabled by specifying "--".  All options after "--"
87 // are returned by OptionSet.Parse() unchanged and unprocessed.
88 //
89 // Unprocessed options are returned from OptionSet.Parse().
90 //
91 // Examples:
92 //  int verbose = 0;
93 //  OptionSet p = new OptionSet ()
94 //    .Add ("v", v => ++verbose)
95 //    .Add ("name=|value=", v => Console.WriteLine (v));
96 //  p.Parse (new string[]{"-v", "--v", "/v", "-name=A", "/name", "B", "extra"});
97 //
98 // The above would parse the argument string array, and would invoke the
99 // lambda expression three times, setting `verbose' to 3 when complete.  
100 // It would also print out "A" and "B" to standard output.
101 // The returned array would contain the string "extra".
102 //
103 // C# 3.0 collection initializers are supported and encouraged:
104 //  var p = new OptionSet () {
105 //    { "h|?|help", v => ShowHelp () },
106 //  };
107 //
108 // System.ComponentModel.TypeConverter is also supported, allowing the use of
109 // custom data types in the callback type; TypeConverter.ConvertFromString()
110 // is used to convert the value option to an instance of the specified
111 // type:
112 //
113 //  var p = new OptionSet () {
114 //    { "foo=", (Foo f) => Console.WriteLine (f.ToString ()) },
115 //  };
116 //
117 // Random other tidbits:
118 //  - Boolean options (those w/o '=' or ':' in the option format string)
119 //    are explicitly enabled if they are followed with '+', and explicitly
120 //    disabled if they are followed with '-':
121 //      string a = null;
122 //      var p = new OptionSet () {
123 //        { "a", s => a = s },
124 //      };
125 //      p.Parse (new string[]{"-a"});   // sets v != null
126 //      p.Parse (new string[]{"-a+"});  // sets v != null
127 //      p.Parse (new string[]{"-a-"});  // sets v == null
128 //
129
130 using System;
131 using System.Collections;
132 using System.Collections.Generic;
133 using System.Collections.ObjectModel;
134 using System.ComponentModel;
135 using System.Globalization;
136 using System.IO;
137 using System.Runtime.Serialization;
138 using System.Security.Permissions;
139 using System.Text;
140 using System.Text.RegularExpressions;
141
142 #if LINQ
143 using System.Linq;
144 #endif
145
146 #if TEST
147 using NDesk.Options;
148 #endif
149
150 #if NDESK_OPTIONS
151 namespace NDesk.Options
152 #else
153 namespace Mono.Options
154 #endif
155 {
156         static class StringCoda {
157
158                 public static IEnumerable<string> WrappedLines (string self, params int[] widths)
159                 {
160                         IEnumerable<int> w = widths;
161                         return WrappedLines (self, w);
162                 }
163
164                 public static IEnumerable<string> WrappedLines (string self, IEnumerable<int> widths)
165                 {
166                         if (widths == null)
167                                 throw new ArgumentNullException ("widths");
168                         return CreateWrappedLinesIterator (self, widths);
169                 }
170
171                 private static IEnumerable<string> CreateWrappedLinesIterator (string self, IEnumerable<int> widths)
172                 {
173                         if (string.IsNullOrEmpty (self)) {
174                                 yield return string.Empty;
175                                 yield break;
176                         }
177                         using (IEnumerator<int> ewidths = widths.GetEnumerator ()) {
178                                 bool? hw = null;
179                                 int width = GetNextWidth (ewidths, int.MaxValue, ref hw);
180                                 int start = 0, end;
181                                 do {
182                                         end = GetLineEnd (start, width, self);
183                                         char c = self [end-1];
184                                         if (char.IsWhiteSpace (c))
185                                                 --end;
186                                         bool needContinuation = end != self.Length && !IsEolChar (c);
187                                         string continuation = "";
188                                         if (needContinuation) {
189                                                 --end;
190                                                 continuation = "-";
191                                         }
192                                         string line = self.Substring (start, end - start) + continuation;
193                                         yield return line;
194                                         start = end;
195                                         if (char.IsWhiteSpace (c))
196                                                 ++start;
197                                         width = GetNextWidth (ewidths, width, ref hw);
198                                 } while (start < self.Length);
199                         }
200                 }
201
202                 private static int GetNextWidth (IEnumerator<int> ewidths, int curWidth, ref bool? eValid)
203                 {
204                         if (!eValid.HasValue || (eValid.HasValue && eValid.Value)) {
205                                 curWidth = (eValid = ewidths.MoveNext ()).Value ? ewidths.Current : curWidth;
206                                 // '.' is any character, - is for a continuation
207                                 const string minWidth = ".-";
208                                 if (curWidth < minWidth.Length)
209                                         throw new ArgumentOutOfRangeException ("widths",
210                                                         string.Format ("Element must be >= {0}, was {1}.", minWidth.Length, curWidth));
211                                 return curWidth;
212                         }
213                         // no more elements, use the last element.
214                         return curWidth;
215                 }
216
217                 private static bool IsEolChar (char c)
218                 {
219                         return !char.IsLetterOrDigit (c);
220                 }
221
222                 private static int GetLineEnd (int start, int length, string description)
223                 {
224                         int end = System.Math.Min (start + length, description.Length);
225                         int sep = -1;
226                         for (int i = start; i < end; ++i) {
227                                 if (description [i] == '\n')
228                                         return i+1;
229                                 if (IsEolChar (description [i]))
230                                         sep = i+1;
231                         }
232                         if (sep == -1 || end == description.Length)
233                                 return end;
234                         return sep;
235                 }
236         }
237
238         public class OptionValueCollection : IList, IList<string> {
239
240                 List<string> values = new List<string> ();
241                 OptionContext c;
242
243                 internal OptionValueCollection (OptionContext c)
244                 {
245                         this.c = c;
246                 }
247
248                 #region ICollection
249                 void ICollection.CopyTo (Array array, int index)  {(values as ICollection).CopyTo (array, index);}
250                 bool ICollection.IsSynchronized                   {get {return (values as ICollection).IsSynchronized;}}
251                 object ICollection.SyncRoot                       {get {return (values as ICollection).SyncRoot;}}
252                 #endregion
253
254                 #region ICollection<T>
255                 public void Add (string item)                       {values.Add (item);}
256                 public void Clear ()                                {values.Clear ();}
257                 public bool Contains (string item)                  {return values.Contains (item);}
258                 public void CopyTo (string[] array, int arrayIndex) {values.CopyTo (array, arrayIndex);}
259                 public bool Remove (string item)                    {return values.Remove (item);}
260                 public int Count                                    {get {return values.Count;}}
261                 public bool IsReadOnly                              {get {return false;}}
262                 #endregion
263
264                 #region IEnumerable
265                 IEnumerator IEnumerable.GetEnumerator () {return values.GetEnumerator ();}
266                 #endregion
267
268                 #region IEnumerable<T>
269                 public IEnumerator<string> GetEnumerator () {return values.GetEnumerator ();}
270                 #endregion
271
272                 #region IList
273                 int IList.Add (object value)                {return (values as IList).Add (value);}
274                 bool IList.Contains (object value)          {return (values as IList).Contains (value);}
275                 int IList.IndexOf (object value)            {return (values as IList).IndexOf (value);}
276                 void IList.Insert (int index, object value) {(values as IList).Insert (index, value);}
277                 void IList.Remove (object value)            {(values as IList).Remove (value);}
278                 void IList.RemoveAt (int index)             {(values as IList).RemoveAt (index);}
279                 bool IList.IsFixedSize                      {get {return false;}}
280                 object IList.this [int index]               {get {return this [index];} set {(values as IList)[index] = value;}}
281                 #endregion
282
283                 #region IList<T>
284                 public int IndexOf (string item)            {return values.IndexOf (item);}
285                 public void Insert (int index, string item) {values.Insert (index, item);}
286                 public void RemoveAt (int index)            {values.RemoveAt (index);}
287
288                 private void AssertValid (int index)
289                 {
290                         if (c.Option == null)
291                                 throw new InvalidOperationException ("OptionContext.Option is null.");
292                         if (index >= c.Option.MaxValueCount)
293                                 throw new ArgumentOutOfRangeException ("index");
294                         if (c.Option.OptionValueType == OptionValueType.Required &&
295                                         index >= values.Count)
296                                 throw new OptionException (string.Format (
297                                                         c.OptionSet.MessageLocalizer ("Missing required value for option '{0}'."), c.OptionName), 
298                                                 c.OptionName);
299                 }
300
301                 public string this [int index] {
302                         get {
303                                 AssertValid (index);
304                                 return index >= values.Count ? null : values [index];
305                         }
306                         set {
307                                 values [index] = value;
308                         }
309                 }
310                 #endregion
311
312                 public List<string> ToList ()
313                 {
314                         return new List<string> (values);
315                 }
316
317                 public string[] ToArray ()
318                 {
319                         return values.ToArray ();
320                 }
321
322                 public override string ToString ()
323                 {
324                         return string.Join (", ", values.ToArray ());
325                 }
326         }
327
328         public class OptionContext {
329                 private Option                option;
330                 private string                name;
331                 private int                   index;
332                 private OptionSet             set;
333                 private OptionValueCollection c;
334
335                 public OptionContext (OptionSet set)
336                 {
337                         this.set = set;
338                         this.c   = new OptionValueCollection (this);
339                 }
340
341                 public Option Option {
342                         get {return option;}
343                         set {option = value;}
344                 }
345
346                 public string OptionName { 
347                         get {return name;}
348                         set {name = value;}
349                 }
350
351                 public int OptionIndex {
352                         get {return index;}
353                         set {index = value;}
354                 }
355
356                 public OptionSet OptionSet {
357                         get {return set;}
358                 }
359
360                 public OptionValueCollection OptionValues {
361                         get {return c;}
362                 }
363         }
364
365         public enum OptionValueType {
366                 None, 
367                 Optional,
368                 Required,
369         }
370
371         public abstract class Option {
372                 string prototype, description;
373                 string[] names;
374                 OptionValueType type;
375                 int count;
376                 string[] separators;
377                 bool hidden;
378
379                 protected Option (string prototype, string description)
380                         : this (prototype, description, 1, false)
381                 {
382                 }
383
384                 protected Option (string prototype, string description, int maxValueCount)
385                         : this (prototype, description, maxValueCount, false)
386                 {
387                 }
388
389                 protected Option (string prototype, string description, int maxValueCount, bool hidden)
390                 {
391                         if (prototype == null)
392                                 throw new ArgumentNullException ("prototype");
393                         if (prototype.Length == 0)
394                                 throw new ArgumentException ("Cannot be the empty string.", "prototype");
395                         if (maxValueCount < 0)
396                                 throw new ArgumentOutOfRangeException ("maxValueCount");
397
398                         this.prototype   = prototype;
399                         this.description = description;
400                         this.count       = maxValueCount;
401                         this.names       = (this is OptionSet.Category)
402                                 // append GetHashCode() so that "duplicate" categories have distinct
403                                 // names, e.g. adding multiple "" categories should be valid.
404                                 ? new[]{prototype + this.GetHashCode ()}
405                                 : prototype.Split ('|');
406
407                         if (this is OptionSet.Category)
408                                 return;
409
410                         this.type        = ParsePrototype ();
411                         this.hidden      = hidden;
412
413                         if (this.count == 0 && type != OptionValueType.None)
414                                 throw new ArgumentException (
415                                                 "Cannot provide maxValueCount of 0 for OptionValueType.Required or " +
416                                                         "OptionValueType.Optional.",
417                                                 "maxValueCount");
418                         if (this.type == OptionValueType.None && maxValueCount > 1)
419                                 throw new ArgumentException (
420                                                 string.Format ("Cannot provide maxValueCount of {0} for OptionValueType.None.", maxValueCount),
421                                                 "maxValueCount");
422                         if (Array.IndexOf (names, "<>") >= 0 && 
423                                         ((names.Length == 1 && this.type != OptionValueType.None) ||
424                                          (names.Length > 1 && this.MaxValueCount > 1)))
425                                 throw new ArgumentException (
426                                                 "The default option handler '<>' cannot require values.",
427                                                 "prototype");
428                 }
429
430                 public string           Prototype       {get {return prototype;}}
431                 public string           Description     {get {return description;}}
432                 public OptionValueType  OptionValueType {get {return type;}}
433                 public int              MaxValueCount   {get {return count;}}
434                 public bool             Hidden          {get {return hidden;}}
435
436                 public string[] GetNames ()
437                 {
438                         return (string[]) names.Clone ();
439                 }
440
441                 public string[] GetValueSeparators ()
442                 {
443                         if (separators == null)
444                                 return new string [0];
445                         return (string[]) separators.Clone ();
446                 }
447
448                 protected static T Parse<T> (string value, OptionContext c)
449                 {
450                         Type tt = typeof (T);
451                         bool nullable = tt.IsValueType && tt.IsGenericType && 
452                                 !tt.IsGenericTypeDefinition && 
453                                 tt.GetGenericTypeDefinition () == typeof (Nullable<>);
454                         Type targetType = nullable ? tt.GetGenericArguments () [0] : typeof (T);
455                         TypeConverter conv = TypeDescriptor.GetConverter (targetType);
456                         T t = default (T);
457                         try {
458                                 if (value != null)
459                                         t = (T) conv.ConvertFromString (value);
460                         }
461                         catch (Exception e) {
462                                 throw new OptionException (
463                                                 string.Format (
464                                                         c.OptionSet.MessageLocalizer ("Could not convert string `{0}' to type {1} for option `{2}'."),
465                                                         value, targetType.Name, c.OptionName),
466                                                 c.OptionName, e);
467                         }
468                         return t;
469                 }
470
471                 internal string[] Names           {get {return names;}}
472                 internal string[] ValueSeparators {get {return separators;}}
473
474                 static readonly char[] NameTerminator = new char[]{'=', ':'};
475
476                 private OptionValueType ParsePrototype ()
477                 {
478                         char type = '\0';
479                         List<string> seps = new List<string> ();
480                         for (int i = 0; i < names.Length; ++i) {
481                                 string name = names [i];
482                                 if (name.Length == 0)
483                                         throw new ArgumentException ("Empty option names are not supported.", "prototype");
484
485                                 int end = name.IndexOfAny (NameTerminator);
486                                 if (end == -1)
487                                         continue;
488                                 names [i] = name.Substring (0, end);
489                                 if (type == '\0' || type == name [end])
490                                         type = name [end];
491                                 else 
492                                         throw new ArgumentException (
493                                                         string.Format ("Conflicting option types: '{0}' vs. '{1}'.", type, name [end]),
494                                                         "prototype");
495                                 AddSeparators (name, end, seps);
496                         }
497
498                         if (type == '\0')
499                                 return OptionValueType.None;
500
501                         if (count <= 1 && seps.Count != 0)
502                                 throw new ArgumentException (
503                                                 string.Format ("Cannot provide key/value separators for Options taking {0} value(s).", count),
504                                                 "prototype");
505                         if (count > 1) {
506                                 if (seps.Count == 0)
507                                         this.separators = new string[]{":", "="};
508                                 else if (seps.Count == 1 && seps [0].Length == 0)
509                                         this.separators = null;
510                                 else
511                                         this.separators = seps.ToArray ();
512                         }
513
514                         return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
515                 }
516
517                 private static void AddSeparators (string name, int end, ICollection<string> seps)
518                 {
519                         int start = -1;
520                         for (int i = end+1; i < name.Length; ++i) {
521                                 switch (name [i]) {
522                                         case '{':
523                                                 if (start != -1)
524                                                         throw new ArgumentException (
525                                                                         string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
526                                                                         "prototype");
527                                                 start = i+1;
528                                                 break;
529                                         case '}':
530                                                 if (start == -1)
531                                                         throw new ArgumentException (
532                                                                         string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
533                                                                         "prototype");
534                                                 seps.Add (name.Substring (start, i-start));
535                                                 start = -1;
536                                                 break;
537                                         default:
538                                                 if (start == -1)
539                                                         seps.Add (name [i].ToString ());
540                                                 break;
541                                 }
542                         }
543                         if (start != -1)
544                                 throw new ArgumentException (
545                                                 string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
546                                                 "prototype");
547                 }
548
549                 public void Invoke (OptionContext c)
550                 {
551                         OnParseComplete (c);
552                         c.OptionName  = null;
553                         c.Option      = null;
554                         c.OptionValues.Clear ();
555                 }
556
557                 protected abstract void OnParseComplete (OptionContext c);
558
559                 public override string ToString ()
560                 {
561                         return Prototype;
562                 }
563         }
564
565         public abstract class ArgumentSource {
566
567                 protected ArgumentSource ()
568                 {
569                 }
570
571                 public abstract string[] GetNames ();
572                 public abstract string Description { get; }
573                 public abstract bool GetArguments (string value, out IEnumerable<string> replacement);
574
575                 public static IEnumerable<string> GetArgumentsFromFile (string file)
576                 {
577                         return GetArguments (File.OpenText (file), true);
578                 }
579
580                 public static IEnumerable<string> GetArguments (TextReader reader)
581                 {
582                         return GetArguments (reader, false);
583                 }
584
585                 // Cribbed from mcs/driver.cs:LoadArgs(string)
586                 static IEnumerable<string> GetArguments (TextReader reader, bool close)
587                 {
588                         try {
589                                 StringBuilder arg = new StringBuilder ();
590
591                                 string line;
592                                 while ((line = reader.ReadLine ()) != null) {
593                                         int t = line.Length;
594
595                                         for (int i = 0; i < t; i++) {
596                                                 char c = line [i];
597                                                 
598                                                 if (c == '"' || c == '\'') {
599                                                         char end = c;
600                                                         
601                                                         for (i++; i < t; i++){
602                                                                 c = line [i];
603
604                                                                 if (c == end)
605                                                                         break;
606                                                                 arg.Append (c);
607                                                         }
608                                                 } else if (c == ' ') {
609                                                         if (arg.Length > 0) {
610                                                                 yield return arg.ToString ();
611                                                                 arg.Length = 0;
612                                                         }
613                                                 } else
614                                                         arg.Append (c);
615                                         }
616                                         if (arg.Length > 0) {
617                                                 yield return arg.ToString ();
618                                                 arg.Length = 0;
619                                         }
620                                 }
621                         }
622                         finally {
623                                 if (close)
624                                         reader.Close ();
625                         }
626                 }
627         }
628
629         public class ResponseFileSource : ArgumentSource {
630
631                 public override string[] GetNames ()
632                 {
633                         return new string[]{"@file"};
634                 }
635
636                 public override string Description {
637                         get {return "Read response file for more options.";}
638                 }
639
640                 public override bool GetArguments (string value, out IEnumerable<string> replacement)
641                 {
642                         if (string.IsNullOrEmpty (value) || !value.StartsWith ("@")) {
643                                 replacement = null;
644                                 return false;
645                         }
646                         replacement = ArgumentSource.GetArgumentsFromFile (value.Substring (1));
647                         return true;
648                 }
649         }
650
651         [Serializable]
652         public class OptionException : Exception {
653                 private string option;
654
655                 public OptionException ()
656                 {
657                 }
658
659                 public OptionException (string message, string optionName)
660                         : base (message)
661                 {
662                         this.option = optionName;
663                 }
664
665                 public OptionException (string message, string optionName, Exception innerException)
666                         : base (message, innerException)
667                 {
668                         this.option = optionName;
669                 }
670
671                 protected OptionException (SerializationInfo info, StreamingContext context)
672                         : base (info, context)
673                 {
674                         this.option = info.GetString ("OptionName");
675                 }
676
677                 public string OptionName {
678                         get {return this.option;}
679                 }
680
681 #pragma warning disable 618 // SecurityPermissionAttribute is obsolete
682                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
683 #pragma warning restore 618
684                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
685                 {
686                         base.GetObjectData (info, context);
687                         info.AddValue ("OptionName", option);
688                 }
689         }
690
691         public delegate void OptionAction<TKey, TValue> (TKey key, TValue value);
692
693         public class OptionSet : KeyedCollection<string, Option>
694         {
695                 public OptionSet ()
696                         : this (delegate (string f) {return f;})
697                 {
698                 }
699
700                 public OptionSet (Converter<string, string> localizer)
701                 {
702                         this.localizer = localizer;
703                         this.roSources = new ReadOnlyCollection<ArgumentSource>(sources);
704                 }
705
706                 Converter<string, string> localizer;
707
708                 public Converter<string, string> MessageLocalizer {
709                         get {return localizer;}
710                 }
711
712                 List<ArgumentSource> sources = new List<ArgumentSource> ();
713                 ReadOnlyCollection<ArgumentSource> roSources;
714
715                 public ReadOnlyCollection<ArgumentSource> ArgumentSources {
716                         get {return roSources;}
717                 }
718
719
720                 protected override string GetKeyForItem (Option item)
721                 {
722                         if (item == null)
723                                 throw new ArgumentNullException ("option");
724                         if (item.Names != null && item.Names.Length > 0)
725                                 return item.Names [0];
726                         // This should never happen, as it's invalid for Option to be
727                         // constructed w/o any names.
728                         throw new InvalidOperationException ("Option has no names!");
729                 }
730
731                 [Obsolete ("Use KeyedCollection.this[string]")]
732                 protected Option GetOptionForName (string option)
733                 {
734                         if (option == null)
735                                 throw new ArgumentNullException ("option");
736                         try {
737                                 return base [option];
738                         }
739                         catch (KeyNotFoundException) {
740                                 return null;
741                         }
742                 }
743
744                 protected override void InsertItem (int index, Option item)
745                 {
746                         base.InsertItem (index, item);
747                         AddImpl (item);
748                 }
749
750                 protected override void RemoveItem (int index)
751                 {
752                         Option p = Items [index];
753                         base.RemoveItem (index);
754                         // KeyedCollection.RemoveItem() handles the 0th item
755                         for (int i = 1; i < p.Names.Length; ++i) {
756                                 Dictionary.Remove (p.Names [i]);
757                         }
758                 }
759
760                 protected override void SetItem (int index, Option item)
761                 {
762                         base.SetItem (index, item);
763                         AddImpl (item);
764                 }
765
766                 private void AddImpl (Option option)
767                 {
768                         if (option == null)
769                                 throw new ArgumentNullException ("option");
770                         List<string> added = new List<string> (option.Names.Length);
771                         try {
772                                 // KeyedCollection.InsertItem/SetItem handle the 0th name.
773                                 for (int i = 1; i < option.Names.Length; ++i) {
774                                         Dictionary.Add (option.Names [i], option);
775                                         added.Add (option.Names [i]);
776                                 }
777                         }
778                         catch (Exception) {
779                                 foreach (string name in added)
780                                         Dictionary.Remove (name);
781                                 throw;
782                         }
783                 }
784
785                 public OptionSet Add (string header)
786                 {
787                         if (header == null)
788                                 throw new ArgumentNullException ("header");
789                         Add (new Category (header));
790                         return this;
791                 }
792
793                 internal sealed class Category : Option {
794
795                         // Prototype starts with '=' because this is an invalid prototype
796                         // (see Option.ParsePrototype(), and thus it'll prevent Category
797                         // instances from being accidentally used as normal options.
798                         public Category (string description)
799                                 : base ("=:Category:= " + description, description)
800                         {
801                         }
802
803                         protected override void OnParseComplete (OptionContext c)
804                         {
805                                 throw new NotSupportedException ("Category.OnParseComplete should not be invoked.");
806                         }
807                 }
808
809
810                 public new OptionSet Add (Option option)
811                 {
812                         base.Add (option);
813                         return this;
814                 }
815
816                 sealed class ActionOption : Option {
817                         Action<OptionValueCollection> action;
818
819                         public ActionOption (string prototype, string description, int count, Action<OptionValueCollection> action)
820                                 : this (prototype, description, count, action, false)
821                         {
822                         }
823
824                         public ActionOption (string prototype, string description, int count, Action<OptionValueCollection> action, bool hidden)
825                                 : base (prototype, description, count, hidden)
826                         {
827                                 if (action == null)
828                                         throw new ArgumentNullException ("action");
829                                 this.action = action;
830                         }
831
832                         protected override void OnParseComplete (OptionContext c)
833                         {
834                                 action (c.OptionValues);
835                         }
836                 }
837
838                 public OptionSet Add (string prototype, Action<string> action)
839                 {
840                         return Add (prototype, null, action);
841                 }
842
843                 public OptionSet Add (string prototype, string description, Action<string> action)
844                 {
845                         return Add (prototype, description, action, false);
846                 }
847
848                 public OptionSet Add (string prototype, string description, Action<string> action, bool hidden)
849                 {
850                         if (action == null)
851                                 throw new ArgumentNullException ("action");
852                         Option p = new ActionOption (prototype, description, 1, 
853                                         delegate (OptionValueCollection v) { action (v [0]); }, hidden);
854                         base.Add (p);
855                         return this;
856                 }
857
858                 public OptionSet Add (string prototype, OptionAction<string, string> action)
859                 {
860                         return Add (prototype, null, action);
861                 }
862
863                 public OptionSet Add (string prototype, string description, OptionAction<string, string> action)
864                 {
865                         return Add (prototype, description, action, false);
866                 }
867
868                 public OptionSet Add (string prototype, string description, OptionAction<string, string> action, bool hidden)   {
869                         if (action == null)
870                                 throw new ArgumentNullException ("action");
871                         Option p = new ActionOption (prototype, description, 2, 
872                                         delegate (OptionValueCollection v) {action (v [0], v [1]);}, hidden);
873                         base.Add (p);
874                         return this;
875                 }
876
877                 sealed class ActionOption<T> : Option {
878                         Action<T> action;
879
880                         public ActionOption (string prototype, string description, Action<T> action)
881                                 : base (prototype, description, 1)
882                         {
883                                 if (action == null)
884                                         throw new ArgumentNullException ("action");
885                                 this.action = action;
886                         }
887
888                         protected override void OnParseComplete (OptionContext c)
889                         {
890                                 action (Parse<T> (c.OptionValues [0], c));
891                         }
892                 }
893
894                 sealed class ActionOption<TKey, TValue> : Option {
895                         OptionAction<TKey, TValue> action;
896
897                         public ActionOption (string prototype, string description, OptionAction<TKey, TValue> action)
898                                 : base (prototype, description, 2)
899                         {
900                                 if (action == null)
901                                         throw new ArgumentNullException ("action");
902                                 this.action = action;
903                         }
904
905                         protected override void OnParseComplete (OptionContext c)
906                         {
907                                 action (
908                                                 Parse<TKey> (c.OptionValues [0], c),
909                                                 Parse<TValue> (c.OptionValues [1], c));
910                         }
911                 }
912
913                 public OptionSet Add<T> (string prototype, Action<T> action)
914                 {
915                         return Add (prototype, null, action);
916                 }
917
918                 public OptionSet Add<T> (string prototype, string description, Action<T> action)
919                 {
920                         return Add (new ActionOption<T> (prototype, description, action));
921                 }
922
923                 public OptionSet Add<TKey, TValue> (string prototype, OptionAction<TKey, TValue> action)
924                 {
925                         return Add (prototype, null, action);
926                 }
927
928                 public OptionSet Add<TKey, TValue> (string prototype, string description, OptionAction<TKey, TValue> action)
929                 {
930                         return Add (new ActionOption<TKey, TValue> (prototype, description, action));
931                 }
932
933                 public OptionSet Add (ArgumentSource source)
934                 {
935                         if (source == null)
936                                 throw new ArgumentNullException ("source");
937                         sources.Add (source);
938                         return this;
939                 }
940
941                 protected virtual OptionContext CreateOptionContext ()
942                 {
943                         return new OptionContext (this);
944                 }
945
946                 public List<string> Parse (IEnumerable<string> arguments)
947                 {
948                         if (arguments == null)
949                                 throw new ArgumentNullException ("arguments");
950                         OptionContext c = CreateOptionContext ();
951                         c.OptionIndex = -1;
952                         bool process = true;
953                         List<string> unprocessed = new List<string> ();
954                         Option def = Contains ("<>") ? this ["<>"] : null;
955                         ArgumentEnumerator ae = new ArgumentEnumerator (arguments);
956                         foreach (string argument in ae) {
957                                 ++c.OptionIndex;
958                                 if (argument == "--") {
959                                         process = false;
960                                         continue;
961                                 }
962                                 if (!process) {
963                                         Unprocessed (unprocessed, def, c, argument);
964                                         continue;
965                                 }
966                                 if (AddSource (ae, argument))
967                                         continue;
968                                 if (!Parse (argument, c))
969                                         Unprocessed (unprocessed, def, c, argument);
970                         }
971                         if (c.Option != null)
972                                 c.Option.Invoke (c);
973                         return unprocessed;
974                 }
975
976                 class ArgumentEnumerator : IEnumerable<string> {
977                         List<IEnumerator<string>> sources = new List<IEnumerator<string>> ();
978
979                         public ArgumentEnumerator (IEnumerable<string> arguments)
980                         {
981                                 sources.Add (arguments.GetEnumerator ());
982                         }
983
984                         public void Add (IEnumerable<string> arguments)
985                         {
986                                 sources.Add (arguments.GetEnumerator ());
987                         }
988
989                         public IEnumerator<string> GetEnumerator ()
990                         {
991                                 do {
992                                         IEnumerator<string> c = sources [sources.Count-1];
993                                         if (c.MoveNext ())
994                                                 yield return c.Current;
995                                         else {
996                                                 c.Dispose ();
997                                                 sources.RemoveAt (sources.Count-1);
998                                         }
999                                 } while (sources.Count > 0);
1000                         }
1001
1002                         IEnumerator IEnumerable.GetEnumerator ()
1003                         {
1004                                 return GetEnumerator ();
1005                         }
1006                 }
1007
1008                 bool AddSource (ArgumentEnumerator ae, string argument)
1009                 {
1010                         foreach (ArgumentSource source in sources) {
1011                                 IEnumerable<string> replacement;
1012                                 if (!source.GetArguments (argument, out replacement))
1013                                         continue;
1014                                 ae.Add (replacement);
1015                                 return true;
1016                         }
1017                         return false;
1018                 }
1019
1020                 private static bool Unprocessed (ICollection<string> extra, Option def, OptionContext c, string argument)
1021                 {
1022                         if (def == null) {
1023                                 extra.Add (argument);
1024                                 return false;
1025                         }
1026                         c.OptionValues.Add (argument);
1027                         c.Option = def;
1028                         c.Option.Invoke (c);
1029                         return false;
1030                 }
1031
1032                 private readonly Regex ValueOption = new Regex (
1033                         @"^(?<flag>--|-|/)(?<name>[^:=]+)((?<sep>[:=])(?<value>.*))?$");
1034
1035                 protected bool GetOptionParts (string argument, out string flag, out string name, out string sep, out string value)
1036                 {
1037                         if (argument == null)
1038                                 throw new ArgumentNullException ("argument");
1039
1040                         flag = name = sep = value = null;
1041                         Match m = ValueOption.Match (argument);
1042                         if (!m.Success) {
1043                                 return false;
1044                         }
1045                         flag  = m.Groups ["flag"].Value;
1046                         name  = m.Groups ["name"].Value;
1047                         if (m.Groups ["sep"].Success && m.Groups ["value"].Success) {
1048                                 sep   = m.Groups ["sep"].Value;
1049                                 value = m.Groups ["value"].Value;
1050                         }
1051                         return true;
1052                 }
1053
1054                 protected virtual bool Parse (string argument, OptionContext c)
1055                 {
1056                         if (c.Option != null) {
1057                                 ParseValue (argument, c);
1058                                 return true;
1059                         }
1060
1061                         string f, n, s, v;
1062                         if (!GetOptionParts (argument, out f, out n, out s, out v))
1063                                 return false;
1064
1065                         Option p;
1066                         if (Contains (n)) {
1067                                 p = this [n];
1068                                 c.OptionName = f + n;
1069                                 c.Option     = p;
1070                                 switch (p.OptionValueType) {
1071                                         case OptionValueType.None:
1072                                                 c.OptionValues.Add (n);
1073                                                 c.Option.Invoke (c);
1074                                                 break;
1075                                         case OptionValueType.Optional:
1076                                         case OptionValueType.Required: 
1077                                                 ParseValue (v, c);
1078                                                 break;
1079                                 }
1080                                 return true;
1081                         }
1082                         // no match; is it a bool option?
1083                         if (ParseBool (argument, n, c))
1084                                 return true;
1085                         // is it a bundled option?
1086                         if (ParseBundledValue (f, string.Concat (n + s + v), c))
1087                                 return true;
1088
1089                         return false;
1090                 }
1091
1092                 private void ParseValue (string option, OptionContext c)
1093                 {
1094                         if (option != null)
1095                                 foreach (string o in c.Option.ValueSeparators != null 
1096                                                 ? option.Split (c.Option.ValueSeparators, c.Option.MaxValueCount - c.OptionValues.Count, StringSplitOptions.None)
1097                                                 : new string[]{option}) {
1098                                         c.OptionValues.Add (o);
1099                                 }
1100                         if (c.OptionValues.Count == c.Option.MaxValueCount || 
1101                                         c.Option.OptionValueType == OptionValueType.Optional)
1102                                 c.Option.Invoke (c);
1103                         else if (c.OptionValues.Count > c.Option.MaxValueCount) {
1104                                 throw new OptionException (localizer (string.Format (
1105                                                                 "Error: Found {0} option values when expecting {1}.", 
1106                                                                 c.OptionValues.Count, c.Option.MaxValueCount)),
1107                                                 c.OptionName);
1108                         }
1109                 }
1110
1111                 private bool ParseBool (string option, string n, OptionContext c)
1112                 {
1113                         Option p;
1114                         string rn;
1115                         if (n.Length >= 1 && (n [n.Length-1] == '+' || n [n.Length-1] == '-') &&
1116                                         Contains ((rn = n.Substring (0, n.Length-1)))) {
1117                                 p = this [rn];
1118                                 string v = n [n.Length-1] == '+' ? option : null;
1119                                 c.OptionName  = option;
1120                                 c.Option      = p;
1121                                 c.OptionValues.Add (v);
1122                                 p.Invoke (c);
1123                                 return true;
1124                         }
1125                         return false;
1126                 }
1127
1128                 private bool ParseBundledValue (string f, string n, OptionContext c)
1129                 {
1130                         if (f != "-")
1131                                 return false;
1132                         for (int i = 0; i < n.Length; ++i) {
1133                                 Option p;
1134                                 string opt = f + n [i].ToString ();
1135                                 string rn = n [i].ToString ();
1136                                 if (!Contains (rn)) {
1137                                         if (i == 0)
1138                                                 return false;
1139                                         throw new OptionException (string.Format (localizer (
1140                                                                         "Cannot use unregistered option '{0}' in bundle '{1}'."), rn, f + n), null);
1141                                 }
1142                                 p = this [rn];
1143                                 switch (p.OptionValueType) {
1144                                         case OptionValueType.None:
1145                                                 Invoke (c, opt, n, p);
1146                                                 break;
1147                                         case OptionValueType.Optional:
1148                                         case OptionValueType.Required: {
1149                                                 string v     = n.Substring (i+1);
1150                                                 c.Option     = p;
1151                                                 c.OptionName = opt;
1152                                                 ParseValue (v.Length != 0 ? v : null, c);
1153                                                 return true;
1154                                         }
1155                                         default:
1156                                                 throw new InvalidOperationException ("Unknown OptionValueType: " + p.OptionValueType);
1157                                 }
1158                         }
1159                         return true;
1160                 }
1161
1162                 private static void Invoke (OptionContext c, string name, string value, Option option)
1163                 {
1164                         c.OptionName  = name;
1165                         c.Option      = option;
1166                         c.OptionValues.Add (value);
1167                         option.Invoke (c);
1168                 }
1169
1170                 private const int OptionWidth = 29;
1171                 private const int Description_FirstWidth  = 80 - OptionWidth;
1172                 private const int Description_RemWidth    = 80 - OptionWidth - 2;
1173
1174                 public void WriteOptionDescriptions (TextWriter o)
1175                 {
1176                         foreach (Option p in this) {
1177                                 int written = 0;
1178
1179                                 if (p.Hidden)
1180                                         continue;
1181
1182                                 Category c = p as Category;
1183                                 if (c != null) {
1184                                         WriteDescription (o, p.Description, "", 80, 80);
1185                                         continue;
1186                                 }
1187
1188                                 if (!WriteOptionPrototype (o, p, ref written))
1189                                         continue;
1190
1191                                 if (written < OptionWidth)
1192                                         o.Write (new string (' ', OptionWidth - written));
1193                                 else {
1194                                         o.WriteLine ();
1195                                         o.Write (new string (' ', OptionWidth));
1196                                 }
1197
1198                                 WriteDescription (o, p.Description, new string (' ', OptionWidth+2),
1199                                                 Description_FirstWidth, Description_RemWidth);
1200                         }
1201
1202                         foreach (ArgumentSource s in sources) {
1203                                 string[] names = s.GetNames ();
1204                                 if (names == null || names.Length == 0)
1205                                         continue;
1206
1207                                 int written = 0;
1208
1209                                 Write (o, ref written, "  ");
1210                                 Write (o, ref written, names [0]);
1211                                 for (int i = 1; i < names.Length; ++i) {
1212                                         Write (o, ref written, ", ");
1213                                         Write (o, ref written, names [i]);
1214                                 }
1215
1216                                 if (written < OptionWidth)
1217                                         o.Write (new string (' ', OptionWidth - written));
1218                                 else {
1219                                         o.WriteLine ();
1220                                         o.Write (new string (' ', OptionWidth));
1221                                 }
1222
1223                                 WriteDescription (o, s.Description, new string (' ', OptionWidth+2),
1224                                                 Description_FirstWidth, Description_RemWidth);
1225                         }
1226                 }
1227
1228                 void WriteDescription (TextWriter o, string value, string prefix, int firstWidth, int remWidth)
1229                 {
1230                         bool indent = false;
1231                         foreach (string line in GetLines (localizer (GetDescription (value)), firstWidth, remWidth)) {
1232                                 if (indent)
1233                                         o.Write (prefix);
1234                                 o.WriteLine (line);
1235                                 indent = true;
1236                         }
1237                 }
1238
1239                 bool WriteOptionPrototype (TextWriter o, Option p, ref int written)
1240                 {
1241                         string[] names = p.Names;
1242
1243                         int i = GetNextOptionIndex (names, 0);
1244                         if (i == names.Length)
1245                                 return false;
1246
1247                         if (names [i].Length == 1) {
1248                                 Write (o, ref written, "  -");
1249                                 Write (o, ref written, names [0]);
1250                         }
1251                         else {
1252                                 Write (o, ref written, "      --");
1253                                 Write (o, ref written, names [0]);
1254                         }
1255
1256                         for ( i = GetNextOptionIndex (names, i+1); 
1257                                         i < names.Length; i = GetNextOptionIndex (names, i+1)) {
1258                                 Write (o, ref written, ", ");
1259                                 Write (o, ref written, names [i].Length == 1 ? "-" : "--");
1260                                 Write (o, ref written, names [i]);
1261                         }
1262
1263                         if (p.OptionValueType == OptionValueType.Optional ||
1264                                         p.OptionValueType == OptionValueType.Required) {
1265                                 if (p.OptionValueType == OptionValueType.Optional) {
1266                                         Write (o, ref written, localizer ("["));
1267                                 }
1268                                 Write (o, ref written, localizer ("=" + GetArgumentName (0, p.MaxValueCount, p.Description)));
1269                                 string sep = p.ValueSeparators != null && p.ValueSeparators.Length > 0 
1270                                         ? p.ValueSeparators [0]
1271                                         : " ";
1272                                 for (int c = 1; c < p.MaxValueCount; ++c) {
1273                                         Write (o, ref written, localizer (sep + GetArgumentName (c, p.MaxValueCount, p.Description)));
1274                                 }
1275                                 if (p.OptionValueType == OptionValueType.Optional) {
1276                                         Write (o, ref written, localizer ("]"));
1277                                 }
1278                         }
1279                         return true;
1280                 }
1281
1282                 static int GetNextOptionIndex (string[] names, int i)
1283                 {
1284                         while (i < names.Length && names [i] == "<>") {
1285                                 ++i;
1286                         }
1287                         return i;
1288                 }
1289
1290                 static void Write (TextWriter o, ref int n, string s)
1291                 {
1292                         n += s.Length;
1293                         o.Write (s);
1294                 }
1295
1296                 private static string GetArgumentName (int index, int maxIndex, string description)
1297                 {
1298                         if (description == null)
1299                                 return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
1300                         string[] nameStart;
1301                         if (maxIndex == 1)
1302                                 nameStart = new string[]{"{0:", "{"};
1303                         else
1304                                 nameStart = new string[]{"{" + index + ":"};
1305                         for (int i = 0; i < nameStart.Length; ++i) {
1306                                 int start, j = 0;
1307                                 do {
1308                                         start = description.IndexOf (nameStart [i], j);
1309                                 } while (start >= 0 && j != 0 ? description [j++ - 1] == '{' : false);
1310                                 if (start == -1)
1311                                         continue;
1312                                 int end = description.IndexOf ("}", start);
1313                                 if (end == -1)
1314                                         continue;
1315                                 return description.Substring (start + nameStart [i].Length, end - start - nameStart [i].Length);
1316                         }
1317                         return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
1318                 }
1319
1320                 private static string GetDescription (string description)
1321                 {
1322                         if (description == null)
1323                                 return string.Empty;
1324                         StringBuilder sb = new StringBuilder (description.Length);
1325                         int start = -1;
1326                         for (int i = 0; i < description.Length; ++i) {
1327                                 switch (description [i]) {
1328                                         case '{':
1329                                                 if (i == start) {
1330                                                         sb.Append ('{');
1331                                                         start = -1;
1332                                                 }
1333                                                 else if (start < 0)
1334                                                         start = i + 1;
1335                                                 break;
1336                                         case '}':
1337                                                 if (start < 0) {
1338                                                         if ((i+1) == description.Length || description [i+1] != '}')
1339                                                                 throw new InvalidOperationException ("Invalid option description: " + description);
1340                                                         ++i;
1341                                                         sb.Append ("}");
1342                                                 }
1343                                                 else {
1344                                                         sb.Append (description.Substring (start, i - start));
1345                                                         start = -1;
1346                                                 }
1347                                                 break;
1348                                         case ':':
1349                                                 if (start < 0)
1350                                                         goto default;
1351                                                 start = i + 1;
1352                                                 break;
1353                                         default:
1354                                                 if (start < 0)
1355                                                         sb.Append (description [i]);
1356                                                 break;
1357                                 }
1358                         }
1359                         return sb.ToString ();
1360                 }
1361
1362                 private static IEnumerable<string> GetLines (string description, int firstWidth, int remWidth)
1363                 {
1364                         return StringCoda.WrappedLines (description, firstWidth, remWidth);
1365                 }
1366         }
1367 }
1368