Merge pull request #409 from Alkarex/patch-1
[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                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
682                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
683                 {
684                         base.GetObjectData (info, context);
685                         info.AddValue ("OptionName", option);
686                 }
687         }
688
689         public delegate void OptionAction<TKey, TValue> (TKey key, TValue value);
690
691         public class OptionSet : KeyedCollection<string, Option>
692         {
693                 public OptionSet ()
694                         : this (delegate (string f) {return f;})
695                 {
696                 }
697
698                 public OptionSet (Converter<string, string> localizer)
699                 {
700                         this.localizer = localizer;
701                         this.roSources = new ReadOnlyCollection<ArgumentSource>(sources);
702                 }
703
704                 Converter<string, string> localizer;
705
706                 public Converter<string, string> MessageLocalizer {
707                         get {return localizer;}
708                 }
709
710                 List<ArgumentSource> sources = new List<ArgumentSource> ();
711                 ReadOnlyCollection<ArgumentSource> roSources;
712
713                 public ReadOnlyCollection<ArgumentSource> ArgumentSources {
714                         get {return roSources;}
715                 }
716
717
718                 protected override string GetKeyForItem (Option item)
719                 {
720                         if (item == null)
721                                 throw new ArgumentNullException ("option");
722                         if (item.Names != null && item.Names.Length > 0)
723                                 return item.Names [0];
724                         // This should never happen, as it's invalid for Option to be
725                         // constructed w/o any names.
726                         throw new InvalidOperationException ("Option has no names!");
727                 }
728
729                 [Obsolete ("Use KeyedCollection.this[string]")]
730                 protected Option GetOptionForName (string option)
731                 {
732                         if (option == null)
733                                 throw new ArgumentNullException ("option");
734                         try {
735                                 return base [option];
736                         }
737                         catch (KeyNotFoundException) {
738                                 return null;
739                         }
740                 }
741
742                 protected override void InsertItem (int index, Option item)
743                 {
744                         base.InsertItem (index, item);
745                         AddImpl (item);
746                 }
747
748                 protected override void RemoveItem (int index)
749                 {
750                         Option p = Items [index];
751                         base.RemoveItem (index);
752                         // KeyedCollection.RemoveItem() handles the 0th item
753                         for (int i = 1; i < p.Names.Length; ++i) {
754                                 Dictionary.Remove (p.Names [i]);
755                         }
756                 }
757
758                 protected override void SetItem (int index, Option item)
759                 {
760                         base.SetItem (index, item);
761                         AddImpl (item);
762                 }
763
764                 private void AddImpl (Option option)
765                 {
766                         if (option == null)
767                                 throw new ArgumentNullException ("option");
768                         List<string> added = new List<string> (option.Names.Length);
769                         try {
770                                 // KeyedCollection.InsertItem/SetItem handle the 0th name.
771                                 for (int i = 1; i < option.Names.Length; ++i) {
772                                         Dictionary.Add (option.Names [i], option);
773                                         added.Add (option.Names [i]);
774                                 }
775                         }
776                         catch (Exception) {
777                                 foreach (string name in added)
778                                         Dictionary.Remove (name);
779                                 throw;
780                         }
781                 }
782
783                 public OptionSet Add (string header)
784                 {
785                         if (header == null)
786                                 throw new ArgumentNullException ("header");
787                         Add (new Category (header));
788                         return this;
789                 }
790
791                 internal sealed class Category : Option {
792
793                         // Prototype starts with '=' because this is an invalid prototype
794                         // (see Option.ParsePrototype(), and thus it'll prevent Category
795                         // instances from being accidentally used as normal options.
796                         public Category (string description)
797                                 : base ("=:Category:= " + description, description)
798                         {
799                         }
800
801                         protected override void OnParseComplete (OptionContext c)
802                         {
803                                 throw new NotSupportedException ("Category.OnParseComplete should not be invoked.");
804                         }
805                 }
806
807
808                 public new OptionSet Add (Option option)
809                 {
810                         base.Add (option);
811                         return this;
812                 }
813
814                 sealed class ActionOption : Option {
815                         Action<OptionValueCollection> action;
816
817                         public ActionOption (string prototype, string description, int count, Action<OptionValueCollection> action)
818                                 : this (prototype, description, count, action, false)
819                         {
820                         }
821
822                         public ActionOption (string prototype, string description, int count, Action<OptionValueCollection> action, bool hidden)
823                                 : base (prototype, description, count, hidden)
824                         {
825                                 if (action == null)
826                                         throw new ArgumentNullException ("action");
827                                 this.action = action;
828                         }
829
830                         protected override void OnParseComplete (OptionContext c)
831                         {
832                                 action (c.OptionValues);
833                         }
834                 }
835
836                 public OptionSet Add (string prototype, Action<string> action)
837                 {
838                         return Add (prototype, null, action);
839                 }
840
841                 public OptionSet Add (string prototype, string description, Action<string> action)
842                 {
843                         return Add (prototype, description, action, false);
844                 }
845
846                 public OptionSet Add (string prototype, string description, Action<string> action, bool hidden)
847                 {
848                         if (action == null)
849                                 throw new ArgumentNullException ("action");
850                         Option p = new ActionOption (prototype, description, 1, 
851                                         delegate (OptionValueCollection v) { action (v [0]); }, hidden);
852                         base.Add (p);
853                         return this;
854                 }
855
856                 public OptionSet Add (string prototype, OptionAction<string, string> action)
857                 {
858                         return Add (prototype, null, action);
859                 }
860
861                 public OptionSet Add (string prototype, string description, OptionAction<string, string> action)
862                 {
863                         return Add (prototype, description, action, false);
864                 }
865
866                 public OptionSet Add (string prototype, string description, OptionAction<string, string> action, bool hidden)   {
867                         if (action == null)
868                                 throw new ArgumentNullException ("action");
869                         Option p = new ActionOption (prototype, description, 2, 
870                                         delegate (OptionValueCollection v) {action (v [0], v [1]);}, hidden);
871                         base.Add (p);
872                         return this;
873                 }
874
875                 sealed class ActionOption<T> : Option {
876                         Action<T> action;
877
878                         public ActionOption (string prototype, string description, Action<T> action)
879                                 : base (prototype, description, 1)
880                         {
881                                 if (action == null)
882                                         throw new ArgumentNullException ("action");
883                                 this.action = action;
884                         }
885
886                         protected override void OnParseComplete (OptionContext c)
887                         {
888                                 action (Parse<T> (c.OptionValues [0], c));
889                         }
890                 }
891
892                 sealed class ActionOption<TKey, TValue> : Option {
893                         OptionAction<TKey, TValue> action;
894
895                         public ActionOption (string prototype, string description, OptionAction<TKey, TValue> action)
896                                 : base (prototype, description, 2)
897                         {
898                                 if (action == null)
899                                         throw new ArgumentNullException ("action");
900                                 this.action = action;
901                         }
902
903                         protected override void OnParseComplete (OptionContext c)
904                         {
905                                 action (
906                                                 Parse<TKey> (c.OptionValues [0], c),
907                                                 Parse<TValue> (c.OptionValues [1], c));
908                         }
909                 }
910
911                 public OptionSet Add<T> (string prototype, Action<T> action)
912                 {
913                         return Add (prototype, null, action);
914                 }
915
916                 public OptionSet Add<T> (string prototype, string description, Action<T> action)
917                 {
918                         return Add (new ActionOption<T> (prototype, description, action));
919                 }
920
921                 public OptionSet Add<TKey, TValue> (string prototype, OptionAction<TKey, TValue> action)
922                 {
923                         return Add (prototype, null, action);
924                 }
925
926                 public OptionSet Add<TKey, TValue> (string prototype, string description, OptionAction<TKey, TValue> action)
927                 {
928                         return Add (new ActionOption<TKey, TValue> (prototype, description, action));
929                 }
930
931                 public OptionSet Add (ArgumentSource source)
932                 {
933                         if (source == null)
934                                 throw new ArgumentNullException ("source");
935                         sources.Add (source);
936                         return this;
937                 }
938
939                 protected virtual OptionContext CreateOptionContext ()
940                 {
941                         return new OptionContext (this);
942                 }
943
944                 public List<string> Parse (IEnumerable<string> arguments)
945                 {
946                         if (arguments == null)
947                                 throw new ArgumentNullException ("arguments");
948                         OptionContext c = CreateOptionContext ();
949                         c.OptionIndex = -1;
950                         bool process = true;
951                         List<string> unprocessed = new List<string> ();
952                         Option def = Contains ("<>") ? this ["<>"] : null;
953                         ArgumentEnumerator ae = new ArgumentEnumerator (arguments);
954                         foreach (string argument in ae) {
955                                 ++c.OptionIndex;
956                                 if (argument == "--") {
957                                         process = false;
958                                         continue;
959                                 }
960                                 if (!process) {
961                                         Unprocessed (unprocessed, def, c, argument);
962                                         continue;
963                                 }
964                                 if (AddSource (ae, argument))
965                                         continue;
966                                 if (!Parse (argument, c))
967                                         Unprocessed (unprocessed, def, c, argument);
968                         }
969                         if (c.Option != null)
970                                 c.Option.Invoke (c);
971                         return unprocessed;
972                 }
973
974                 class ArgumentEnumerator : IEnumerable<string> {
975                         List<IEnumerator<string>> sources = new List<IEnumerator<string>> ();
976
977                         public ArgumentEnumerator (IEnumerable<string> arguments)
978                         {
979                                 sources.Add (arguments.GetEnumerator ());
980                         }
981
982                         public void Add (IEnumerable<string> arguments)
983                         {
984                                 sources.Add (arguments.GetEnumerator ());
985                         }
986
987                         public IEnumerator<string> GetEnumerator ()
988                         {
989                                 do {
990                                         IEnumerator<string> c = sources [sources.Count-1];
991                                         if (c.MoveNext ())
992                                                 yield return c.Current;
993                                         else {
994                                                 c.Dispose ();
995                                                 sources.RemoveAt (sources.Count-1);
996                                         }
997                                 } while (sources.Count > 0);
998                         }
999
1000                         IEnumerator IEnumerable.GetEnumerator ()
1001                         {
1002                                 return GetEnumerator ();
1003                         }
1004                 }
1005
1006                 bool AddSource (ArgumentEnumerator ae, string argument)
1007                 {
1008                         foreach (ArgumentSource source in sources) {
1009                                 IEnumerable<string> replacement;
1010                                 if (!source.GetArguments (argument, out replacement))
1011                                         continue;
1012                                 ae.Add (replacement);
1013                                 return true;
1014                         }
1015                         return false;
1016                 }
1017
1018                 private static bool Unprocessed (ICollection<string> extra, Option def, OptionContext c, string argument)
1019                 {
1020                         if (def == null) {
1021                                 extra.Add (argument);
1022                                 return false;
1023                         }
1024                         c.OptionValues.Add (argument);
1025                         c.Option = def;
1026                         c.Option.Invoke (c);
1027                         return false;
1028                 }
1029
1030                 private readonly Regex ValueOption = new Regex (
1031                         @"^(?<flag>--|-|/)(?<name>[^:=]+)((?<sep>[:=])(?<value>.*))?$");
1032
1033                 protected bool GetOptionParts (string argument, out string flag, out string name, out string sep, out string value)
1034                 {
1035                         if (argument == null)
1036                                 throw new ArgumentNullException ("argument");
1037
1038                         flag = name = sep = value = null;
1039                         Match m = ValueOption.Match (argument);
1040                         if (!m.Success) {
1041                                 return false;
1042                         }
1043                         flag  = m.Groups ["flag"].Value;
1044                         name  = m.Groups ["name"].Value;
1045                         if (m.Groups ["sep"].Success && m.Groups ["value"].Success) {
1046                                 sep   = m.Groups ["sep"].Value;
1047                                 value = m.Groups ["value"].Value;
1048                         }
1049                         return true;
1050                 }
1051
1052                 protected virtual bool Parse (string argument, OptionContext c)
1053                 {
1054                         if (c.Option != null) {
1055                                 ParseValue (argument, c);
1056                                 return true;
1057                         }
1058
1059                         string f, n, s, v;
1060                         if (!GetOptionParts (argument, out f, out n, out s, out v))
1061                                 return false;
1062
1063                         Option p;
1064                         if (Contains (n)) {
1065                                 p = this [n];
1066                                 c.OptionName = f + n;
1067                                 c.Option     = p;
1068                                 switch (p.OptionValueType) {
1069                                         case OptionValueType.None:
1070                                                 c.OptionValues.Add (n);
1071                                                 c.Option.Invoke (c);
1072                                                 break;
1073                                         case OptionValueType.Optional:
1074                                         case OptionValueType.Required: 
1075                                                 ParseValue (v, c);
1076                                                 break;
1077                                 }
1078                                 return true;
1079                         }
1080                         // no match; is it a bool option?
1081                         if (ParseBool (argument, n, c))
1082                                 return true;
1083                         // is it a bundled option?
1084                         if (ParseBundledValue (f, string.Concat (n + s + v), c))
1085                                 return true;
1086
1087                         return false;
1088                 }
1089
1090                 private void ParseValue (string option, OptionContext c)
1091                 {
1092                         if (option != null)
1093                                 foreach (string o in c.Option.ValueSeparators != null 
1094                                                 ? option.Split (c.Option.ValueSeparators, c.Option.MaxValueCount - c.OptionValues.Count, StringSplitOptions.None)
1095                                                 : new string[]{option}) {
1096                                         c.OptionValues.Add (o);
1097                                 }
1098                         if (c.OptionValues.Count == c.Option.MaxValueCount || 
1099                                         c.Option.OptionValueType == OptionValueType.Optional)
1100                                 c.Option.Invoke (c);
1101                         else if (c.OptionValues.Count > c.Option.MaxValueCount) {
1102                                 throw new OptionException (localizer (string.Format (
1103                                                                 "Error: Found {0} option values when expecting {1}.", 
1104                                                                 c.OptionValues.Count, c.Option.MaxValueCount)),
1105                                                 c.OptionName);
1106                         }
1107                 }
1108
1109                 private bool ParseBool (string option, string n, OptionContext c)
1110                 {
1111                         Option p;
1112                         string rn;
1113                         if (n.Length >= 1 && (n [n.Length-1] == '+' || n [n.Length-1] == '-') &&
1114                                         Contains ((rn = n.Substring (0, n.Length-1)))) {
1115                                 p = this [rn];
1116                                 string v = n [n.Length-1] == '+' ? option : null;
1117                                 c.OptionName  = option;
1118                                 c.Option      = p;
1119                                 c.OptionValues.Add (v);
1120                                 p.Invoke (c);
1121                                 return true;
1122                         }
1123                         return false;
1124                 }
1125
1126                 private bool ParseBundledValue (string f, string n, OptionContext c)
1127                 {
1128                         if (f != "-")
1129                                 return false;
1130                         for (int i = 0; i < n.Length; ++i) {
1131                                 Option p;
1132                                 string opt = f + n [i].ToString ();
1133                                 string rn = n [i].ToString ();
1134                                 if (!Contains (rn)) {
1135                                         if (i == 0)
1136                                                 return false;
1137                                         throw new OptionException (string.Format (localizer (
1138                                                                         "Cannot bundle unregistered option '{0}'."), opt), opt);
1139                                 }
1140                                 p = this [rn];
1141                                 switch (p.OptionValueType) {
1142                                         case OptionValueType.None:
1143                                                 Invoke (c, opt, n, p);
1144                                                 break;
1145                                         case OptionValueType.Optional:
1146                                         case OptionValueType.Required: {
1147                                                 string v     = n.Substring (i+1);
1148                                                 c.Option     = p;
1149                                                 c.OptionName = opt;
1150                                                 ParseValue (v.Length != 0 ? v : null, c);
1151                                                 return true;
1152                                         }
1153                                         default:
1154                                                 throw new InvalidOperationException ("Unknown OptionValueType: " + p.OptionValueType);
1155                                 }
1156                         }
1157                         return true;
1158                 }
1159
1160                 private static void Invoke (OptionContext c, string name, string value, Option option)
1161                 {
1162                         c.OptionName  = name;
1163                         c.Option      = option;
1164                         c.OptionValues.Add (value);
1165                         option.Invoke (c);
1166                 }
1167
1168                 private const int OptionWidth = 29;
1169                 private const int Description_FirstWidth  = 80 - OptionWidth;
1170                 private const int Description_RemWidth    = 80 - OptionWidth - 2;
1171
1172                 public void WriteOptionDescriptions (TextWriter o)
1173                 {
1174                         foreach (Option p in this) {
1175                                 int written = 0;
1176
1177                                 if (p.Hidden)
1178                                         continue;
1179
1180                                 Category c = p as Category;
1181                                 if (c != null) {
1182                                         WriteDescription (o, p.Description, "", 80, 80);
1183                                         continue;
1184                                 }
1185
1186                                 if (!WriteOptionPrototype (o, p, ref written))
1187                                         continue;
1188
1189                                 if (written < OptionWidth)
1190                                         o.Write (new string (' ', OptionWidth - written));
1191                                 else {
1192                                         o.WriteLine ();
1193                                         o.Write (new string (' ', OptionWidth));
1194                                 }
1195
1196                                 WriteDescription (o, p.Description, new string (' ', OptionWidth+2),
1197                                                 Description_FirstWidth, Description_RemWidth);
1198                         }
1199
1200                         foreach (ArgumentSource s in sources) {
1201                                 string[] names = s.GetNames ();
1202                                 if (names == null || names.Length == 0)
1203                                         continue;
1204
1205                                 int written = 0;
1206
1207                                 Write (o, ref written, "  ");
1208                                 Write (o, ref written, names [0]);
1209                                 for (int i = 1; i < names.Length; ++i) {
1210                                         Write (o, ref written, ", ");
1211                                         Write (o, ref written, names [i]);
1212                                 }
1213
1214                                 if (written < OptionWidth)
1215                                         o.Write (new string (' ', OptionWidth - written));
1216                                 else {
1217                                         o.WriteLine ();
1218                                         o.Write (new string (' ', OptionWidth));
1219                                 }
1220
1221                                 WriteDescription (o, s.Description, new string (' ', OptionWidth+2),
1222                                                 Description_FirstWidth, Description_RemWidth);
1223                         }
1224                 }
1225
1226                 void WriteDescription (TextWriter o, string value, string prefix, int firstWidth, int remWidth)
1227                 {
1228                         bool indent = false;
1229                         foreach (string line in GetLines (localizer (GetDescription (value)), firstWidth, remWidth)) {
1230                                 if (indent)
1231                                         o.Write (prefix);
1232                                 o.WriteLine (line);
1233                                 indent = true;
1234                         }
1235                 }
1236
1237                 bool WriteOptionPrototype (TextWriter o, Option p, ref int written)
1238                 {
1239                         string[] names = p.Names;
1240
1241                         int i = GetNextOptionIndex (names, 0);
1242                         if (i == names.Length)
1243                                 return false;
1244
1245                         if (names [i].Length == 1) {
1246                                 Write (o, ref written, "  -");
1247                                 Write (o, ref written, names [0]);
1248                         }
1249                         else {
1250                                 Write (o, ref written, "      --");
1251                                 Write (o, ref written, names [0]);
1252                         }
1253
1254                         for ( i = GetNextOptionIndex (names, i+1); 
1255                                         i < names.Length; i = GetNextOptionIndex (names, i+1)) {
1256                                 Write (o, ref written, ", ");
1257                                 Write (o, ref written, names [i].Length == 1 ? "-" : "--");
1258                                 Write (o, ref written, names [i]);
1259                         }
1260
1261                         if (p.OptionValueType == OptionValueType.Optional ||
1262                                         p.OptionValueType == OptionValueType.Required) {
1263                                 if (p.OptionValueType == OptionValueType.Optional) {
1264                                         Write (o, ref written, localizer ("["));
1265                                 }
1266                                 Write (o, ref written, localizer ("=" + GetArgumentName (0, p.MaxValueCount, p.Description)));
1267                                 string sep = p.ValueSeparators != null && p.ValueSeparators.Length > 0 
1268                                         ? p.ValueSeparators [0]
1269                                         : " ";
1270                                 for (int c = 1; c < p.MaxValueCount; ++c) {
1271                                         Write (o, ref written, localizer (sep + GetArgumentName (c, p.MaxValueCount, p.Description)));
1272                                 }
1273                                 if (p.OptionValueType == OptionValueType.Optional) {
1274                                         Write (o, ref written, localizer ("]"));
1275                                 }
1276                         }
1277                         return true;
1278                 }
1279
1280                 static int GetNextOptionIndex (string[] names, int i)
1281                 {
1282                         while (i < names.Length && names [i] == "<>") {
1283                                 ++i;
1284                         }
1285                         return i;
1286                 }
1287
1288                 static void Write (TextWriter o, ref int n, string s)
1289                 {
1290                         n += s.Length;
1291                         o.Write (s);
1292                 }
1293
1294                 private static string GetArgumentName (int index, int maxIndex, string description)
1295                 {
1296                         if (description == null)
1297                                 return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
1298                         string[] nameStart;
1299                         if (maxIndex == 1)
1300                                 nameStart = new string[]{"{0:", "{"};
1301                         else
1302                                 nameStart = new string[]{"{" + index + ":"};
1303                         for (int i = 0; i < nameStart.Length; ++i) {
1304                                 int start, j = 0;
1305                                 do {
1306                                         start = description.IndexOf (nameStart [i], j);
1307                                 } while (start >= 0 && j != 0 ? description [j++ - 1] == '{' : false);
1308                                 if (start == -1)
1309                                         continue;
1310                                 int end = description.IndexOf ("}", start);
1311                                 if (end == -1)
1312                                         continue;
1313                                 return description.Substring (start + nameStart [i].Length, end - start - nameStart [i].Length);
1314                         }
1315                         return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
1316                 }
1317
1318                 private static string GetDescription (string description)
1319                 {
1320                         if (description == null)
1321                                 return string.Empty;
1322                         StringBuilder sb = new StringBuilder (description.Length);
1323                         int start = -1;
1324                         for (int i = 0; i < description.Length; ++i) {
1325                                 switch (description [i]) {
1326                                         case '{':
1327                                                 if (i == start) {
1328                                                         sb.Append ('{');
1329                                                         start = -1;
1330                                                 }
1331                                                 else if (start < 0)
1332                                                         start = i + 1;
1333                                                 break;
1334                                         case '}':
1335                                                 if (start < 0) {
1336                                                         if ((i+1) == description.Length || description [i+1] != '}')
1337                                                                 throw new InvalidOperationException ("Invalid option description: " + description);
1338                                                         ++i;
1339                                                         sb.Append ("}");
1340                                                 }
1341                                                 else {
1342                                                         sb.Append (description.Substring (start, i - start));
1343                                                         start = -1;
1344                                                 }
1345                                                 break;
1346                                         case ':':
1347                                                 if (start < 0)
1348                                                         goto default;
1349                                                 start = i + 1;
1350                                                 break;
1351                                         default:
1352                                                 if (start < 0)
1353                                                         sb.Append (description [i]);
1354                                                 break;
1355                                 }
1356                         }
1357                         return sb.ToString ();
1358                 }
1359
1360                 private static IEnumerable<string> GetLines (string description, int firstWidth, int remWidth)
1361                 {
1362                         return StringCoda.WrappedLines (description, firstWidth, remWidth);
1363                 }
1364         }
1365 }
1366