5 // Jonathan Pryor <jpryor@novell.com>
6 // Federico Di Gregorio <fog@initd.org>
7 // Rolf Bjarne Kvinge <rolf@xamarin.com>
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)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
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
37 // The LINQ version just changes the implementation of
38 // OptionSet.Parse(IEnumerable<string>), and confers no semantic changes.
41 // A Getopt::Long-inspired option parsing library for C#.
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.
47 // Option format strings:
48 // Regex-like BNF Grammar:
51 // sep: ( [^{}]+ | '{' .+ '}' )?
52 // aliases: ( name type sep ) ( '|' name type sep )*
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.
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.
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 ':')
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.
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.
83 // This allows specifying '-a -b -c' as '-abc', and specifying '-D name=value'
86 // Option processing is disabled by specifying "--". All options after "--"
87 // are returned by OptionSet.Parse() unchanged and unprocessed.
89 // Unprocessed options are returned from OptionSet.Parse().
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"});
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".
103 // C# 3.0 collection initializers are supported and encouraged:
104 // var p = new OptionSet () {
105 // { "h|?|help", v => ShowHelp () },
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
113 // var p = new OptionSet () {
114 // { "foo=", (Foo f) => Console.WriteLine (f.ToString ()) },
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 '-':
122 // var p = new OptionSet () {
123 // { "a", s => a = s },
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
131 using System.Collections;
132 using System.Collections.Generic;
133 using System.Collections.ObjectModel;
134 using System.ComponentModel;
135 using System.Globalization;
137 using System.Runtime.Serialization;
138 using System.Security.Permissions;
140 using System.Text.RegularExpressions;
151 namespace NDesk.Options
153 namespace Mono.Options
156 static class StringCoda {
158 public static IEnumerable<string> WrappedLines (string self, params int[] widths)
160 IEnumerable<int> w = widths;
161 return WrappedLines (self, w);
164 public static IEnumerable<string> WrappedLines (string self, IEnumerable<int> widths)
167 throw new ArgumentNullException ("widths");
168 return CreateWrappedLinesIterator (self, widths);
171 private static IEnumerable<string> CreateWrappedLinesIterator (string self, IEnumerable<int> widths)
173 if (string.IsNullOrEmpty (self)) {
174 yield return string.Empty;
177 using (IEnumerator<int> ewidths = widths.GetEnumerator ()) {
179 int width = GetNextWidth (ewidths, int.MaxValue, ref hw);
182 end = GetLineEnd (start, width, self);
183 char c = self [end-1];
184 if (char.IsWhiteSpace (c))
186 bool needContinuation = end != self.Length && !IsEolChar (c);
187 string continuation = "";
188 if (needContinuation) {
192 string line = self.Substring (start, end - start) + continuation;
195 if (char.IsWhiteSpace (c))
197 width = GetNextWidth (ewidths, width, ref hw);
198 } while (start < self.Length);
202 private static int GetNextWidth (IEnumerator<int> ewidths, int curWidth, ref bool? eValid)
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));
213 // no more elements, use the last element.
217 private static bool IsEolChar (char c)
219 return !char.IsLetterOrDigit (c);
222 private static int GetLineEnd (int start, int length, string description)
224 int end = System.Math.Min (start + length, description.Length);
226 for (int i = start; i < end; ++i) {
227 if (description [i] == '\n')
229 if (IsEolChar (description [i]))
232 if (sep == -1 || end == description.Length)
238 public class OptionValueCollection : IList, IList<string> {
240 List<string> values = new List<string> ();
243 internal OptionValueCollection (OptionContext c)
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;}}
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;}}
265 IEnumerator IEnumerable.GetEnumerator () {return values.GetEnumerator ();}
268 #region IEnumerable<T>
269 public IEnumerator<string> GetEnumerator () {return values.GetEnumerator ();}
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;}}
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);}
288 private void AssertValid (int index)
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),
301 public string this [int index] {
304 return index >= values.Count ? null : values [index];
307 values [index] = value;
312 public List<string> ToList ()
314 return new List<string> (values);
317 public string[] ToArray ()
319 return values.ToArray ();
322 public override string ToString ()
324 return string.Join (", ", values.ToArray ());
328 public class OptionContext {
329 private Option option;
332 private OptionSet set;
333 private OptionValueCollection c;
335 public OptionContext (OptionSet set)
338 this.c = new OptionValueCollection (this);
341 public Option Option {
343 set {option = value;}
346 public string OptionName {
351 public int OptionIndex {
356 public OptionSet OptionSet {
360 public OptionValueCollection OptionValues {
365 public enum OptionValueType {
371 public abstract class Option {
372 string prototype, description;
374 OptionValueType type;
379 protected Option (string prototype, string description)
380 : this (prototype, description, 1, false)
384 protected Option (string prototype, string description, int maxValueCount)
385 : this (prototype, description, maxValueCount, false)
389 protected Option (string prototype, string description, int maxValueCount, bool hidden)
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");
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 ('|');
407 if (this is OptionSet.Category)
410 this.type = ParsePrototype ();
411 this.hidden = hidden;
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.",
418 if (this.type == OptionValueType.None && maxValueCount > 1)
419 throw new ArgumentException (
420 string.Format ("Cannot provide maxValueCount of {0} for OptionValueType.None.", 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.",
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;}}
436 public string[] GetNames ()
438 return (string[]) names.Clone ();
441 public string[] GetValueSeparators ()
443 if (separators == null)
444 return new string [0];
445 return (string[]) separators.Clone ();
448 protected static T Parse<T> (string value, OptionContext c)
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);
459 t = (T) conv.ConvertFromString (value);
461 catch (Exception e) {
462 throw new OptionException (
464 c.OptionSet.MessageLocalizer ("Could not convert string `{0}' to type {1} for option `{2}'."),
465 value, targetType.Name, c.OptionName),
471 internal string[] Names {get {return names;}}
472 internal string[] ValueSeparators {get {return separators;}}
474 static readonly char[] NameTerminator = new char[]{'=', ':'};
476 private OptionValueType ParsePrototype ()
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");
485 int end = name.IndexOfAny (NameTerminator);
488 names [i] = name.Substring (0, end);
489 if (type == '\0' || type == name [end])
492 throw new ArgumentException (
493 string.Format ("Conflicting option types: '{0}' vs. '{1}'.", type, name [end]),
495 AddSeparators (name, end, seps);
499 return OptionValueType.None;
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),
507 this.separators = new string[]{":", "="};
508 else if (seps.Count == 1 && seps [0].Length == 0)
509 this.separators = null;
511 this.separators = seps.ToArray ();
514 return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
517 private static void AddSeparators (string name, int end, ICollection<string> seps)
520 for (int i = end+1; i < name.Length; ++i) {
524 throw new ArgumentException (
525 string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
531 throw new ArgumentException (
532 string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
534 seps.Add (name.Substring (start, i-start));
539 seps.Add (name [i].ToString ());
544 throw new ArgumentException (
545 string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
549 public void Invoke (OptionContext c)
554 c.OptionValues.Clear ();
557 protected abstract void OnParseComplete (OptionContext c);
559 public override string ToString ()
565 public abstract class ArgumentSource {
567 protected ArgumentSource ()
571 public abstract string[] GetNames ();
572 public abstract string Description { get; }
573 public abstract bool GetArguments (string value, out IEnumerable<string> replacement);
575 public static IEnumerable<string> GetArgumentsFromFile (string file)
577 return GetArguments (File.OpenText (file), true);
580 public static IEnumerable<string> GetArguments (TextReader reader)
582 return GetArguments (reader, false);
585 // Cribbed from mcs/driver.cs:LoadArgs(string)
586 static IEnumerable<string> GetArguments (TextReader reader, bool close)
589 StringBuilder arg = new StringBuilder ();
592 while ((line = reader.ReadLine ()) != null) {
595 for (int i = 0; i < t; i++) {
598 if (c == '"' || c == '\'') {
601 for (i++; i < t; i++){
608 } else if (c == ' ') {
609 if (arg.Length > 0) {
610 yield return arg.ToString ();
616 if (arg.Length > 0) {
617 yield return arg.ToString ();
629 public class ResponseFileSource : ArgumentSource {
631 public override string[] GetNames ()
633 return new string[]{"@file"};
636 public override string Description {
637 get {return "Read response file for more options.";}
640 public override bool GetArguments (string value, out IEnumerable<string> replacement)
642 if (string.IsNullOrEmpty (value) || !value.StartsWith ("@")) {
646 replacement = ArgumentSource.GetArgumentsFromFile (value.Substring (1));
652 public class OptionException : Exception {
653 private string option;
655 public OptionException ()
659 public OptionException (string message, string optionName)
662 this.option = optionName;
665 public OptionException (string message, string optionName, Exception innerException)
666 : base (message, innerException)
668 this.option = optionName;
671 protected OptionException (SerializationInfo info, StreamingContext context)
672 : base (info, context)
674 this.option = info.GetString ("OptionName");
677 public string OptionName {
678 get {return this.option;}
681 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
682 public override void GetObjectData (SerializationInfo info, StreamingContext context)
684 base.GetObjectData (info, context);
685 info.AddValue ("OptionName", option);
689 public delegate void OptionAction<TKey, TValue> (TKey key, TValue value);
691 public class OptionSet : KeyedCollection<string, Option>
694 : this (delegate (string f) {return f;})
698 public OptionSet (Converter<string, string> localizer)
700 this.localizer = localizer;
701 this.roSources = new ReadOnlyCollection<ArgumentSource>(sources);
704 Converter<string, string> localizer;
706 public Converter<string, string> MessageLocalizer {
707 get {return localizer;}
710 List<ArgumentSource> sources = new List<ArgumentSource> ();
711 ReadOnlyCollection<ArgumentSource> roSources;
713 public ReadOnlyCollection<ArgumentSource> ArgumentSources {
714 get {return roSources;}
718 protected override string GetKeyForItem (Option item)
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!");
729 [Obsolete ("Use KeyedCollection.this[string]")]
730 protected Option GetOptionForName (string option)
733 throw new ArgumentNullException ("option");
735 return base [option];
737 catch (KeyNotFoundException) {
742 protected override void InsertItem (int index, Option item)
744 base.InsertItem (index, item);
748 protected override void RemoveItem (int index)
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]);
758 protected override void SetItem (int index, Option item)
760 base.SetItem (index, item);
764 private void AddImpl (Option option)
767 throw new ArgumentNullException ("option");
768 List<string> added = new List<string> (option.Names.Length);
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]);
777 foreach (string name in added)
778 Dictionary.Remove (name);
783 public OptionSet Add (string header)
786 throw new ArgumentNullException ("header");
787 Add (new Category (header));
791 internal sealed class Category : Option {
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)
801 protected override void OnParseComplete (OptionContext c)
803 throw new NotSupportedException ("Category.OnParseComplete should not be invoked.");
808 public new OptionSet Add (Option option)
814 sealed class ActionOption : Option {
815 Action<OptionValueCollection> action;
817 public ActionOption (string prototype, string description, int count, Action<OptionValueCollection> action)
818 : this (prototype, description, count, action, false)
822 public ActionOption (string prototype, string description, int count, Action<OptionValueCollection> action, bool hidden)
823 : base (prototype, description, count, hidden)
826 throw new ArgumentNullException ("action");
827 this.action = action;
830 protected override void OnParseComplete (OptionContext c)
832 action (c.OptionValues);
836 public OptionSet Add (string prototype, Action<string> action)
838 return Add (prototype, null, action);
841 public OptionSet Add (string prototype, string description, Action<string> action)
843 return Add (prototype, description, action, false);
846 public OptionSet Add (string prototype, string description, Action<string> action, bool hidden)
849 throw new ArgumentNullException ("action");
850 Option p = new ActionOption (prototype, description, 1,
851 delegate (OptionValueCollection v) { action (v [0]); }, hidden);
856 public OptionSet Add (string prototype, OptionAction<string, string> action)
858 return Add (prototype, null, action);
861 public OptionSet Add (string prototype, string description, OptionAction<string, string> action)
863 return Add (prototype, description, action, false);
866 public OptionSet Add (string prototype, string description, OptionAction<string, string> action, bool hidden) {
868 throw new ArgumentNullException ("action");
869 Option p = new ActionOption (prototype, description, 2,
870 delegate (OptionValueCollection v) {action (v [0], v [1]);}, hidden);
875 sealed class ActionOption<T> : Option {
878 public ActionOption (string prototype, string description, Action<T> action)
879 : base (prototype, description, 1)
882 throw new ArgumentNullException ("action");
883 this.action = action;
886 protected override void OnParseComplete (OptionContext c)
888 action (Parse<T> (c.OptionValues [0], c));
892 sealed class ActionOption<TKey, TValue> : Option {
893 OptionAction<TKey, TValue> action;
895 public ActionOption (string prototype, string description, OptionAction<TKey, TValue> action)
896 : base (prototype, description, 2)
899 throw new ArgumentNullException ("action");
900 this.action = action;
903 protected override void OnParseComplete (OptionContext c)
906 Parse<TKey> (c.OptionValues [0], c),
907 Parse<TValue> (c.OptionValues [1], c));
911 public OptionSet Add<T> (string prototype, Action<T> action)
913 return Add (prototype, null, action);
916 public OptionSet Add<T> (string prototype, string description, Action<T> action)
918 return Add (new ActionOption<T> (prototype, description, action));
921 public OptionSet Add<TKey, TValue> (string prototype, OptionAction<TKey, TValue> action)
923 return Add (prototype, null, action);
926 public OptionSet Add<TKey, TValue> (string prototype, string description, OptionAction<TKey, TValue> action)
928 return Add (new ActionOption<TKey, TValue> (prototype, description, action));
931 public OptionSet Add (ArgumentSource source)
934 throw new ArgumentNullException ("source");
935 sources.Add (source);
939 protected virtual OptionContext CreateOptionContext ()
941 return new OptionContext (this);
944 public List<string> Parse (IEnumerable<string> arguments)
946 if (arguments == null)
947 throw new ArgumentNullException ("arguments");
948 OptionContext c = CreateOptionContext ();
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) {
956 if (argument == "--") {
961 Unprocessed (unprocessed, def, c, argument);
964 if (AddSource (ae, argument))
966 if (!Parse (argument, c))
967 Unprocessed (unprocessed, def, c, argument);
969 if (c.Option != null)
974 class ArgumentEnumerator : IEnumerable<string> {
975 List<IEnumerator<string>> sources = new List<IEnumerator<string>> ();
977 public ArgumentEnumerator (IEnumerable<string> arguments)
979 sources.Add (arguments.GetEnumerator ());
982 public void Add (IEnumerable<string> arguments)
984 sources.Add (arguments.GetEnumerator ());
987 public IEnumerator<string> GetEnumerator ()
990 IEnumerator<string> c = sources [sources.Count-1];
992 yield return c.Current;
995 sources.RemoveAt (sources.Count-1);
997 } while (sources.Count > 0);
1000 IEnumerator IEnumerable.GetEnumerator ()
1002 return GetEnumerator ();
1006 bool AddSource (ArgumentEnumerator ae, string argument)
1008 foreach (ArgumentSource source in sources) {
1009 IEnumerable<string> replacement;
1010 if (!source.GetArguments (argument, out replacement))
1012 ae.Add (replacement);
1018 private static bool Unprocessed (ICollection<string> extra, Option def, OptionContext c, string argument)
1021 extra.Add (argument);
1024 c.OptionValues.Add (argument);
1026 c.Option.Invoke (c);
1030 private readonly Regex ValueOption = new Regex (
1031 @"^(?<flag>--|-|/)(?<name>[^:=]+)((?<sep>[:=])(?<value>.*))?$");
1033 protected bool GetOptionParts (string argument, out string flag, out string name, out string sep, out string value)
1035 if (argument == null)
1036 throw new ArgumentNullException ("argument");
1038 flag = name = sep = value = null;
1039 Match m = ValueOption.Match (argument);
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;
1052 protected virtual bool Parse (string argument, OptionContext c)
1054 if (c.Option != null) {
1055 ParseValue (argument, c);
1060 if (!GetOptionParts (argument, out f, out n, out s, out v))
1066 c.OptionName = f + n;
1068 switch (p.OptionValueType) {
1069 case OptionValueType.None:
1070 c.OptionValues.Add (n);
1071 c.Option.Invoke (c);
1073 case OptionValueType.Optional:
1074 case OptionValueType.Required:
1080 // no match; is it a bool option?
1081 if (ParseBool (argument, n, c))
1083 // is it a bundled option?
1084 if (ParseBundledValue (f, string.Concat (n + s + v), c))
1090 private void ParseValue (string option, OptionContext c)
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);
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)),
1109 private bool ParseBool (string option, string n, OptionContext c)
1113 if (n.Length >= 1 && (n [n.Length-1] == '+' || n [n.Length-1] == '-') &&
1114 Contains ((rn = n.Substring (0, n.Length-1)))) {
1116 string v = n [n.Length-1] == '+' ? option : null;
1117 c.OptionName = option;
1119 c.OptionValues.Add (v);
1126 private bool ParseBundledValue (string f, string n, OptionContext c)
1130 for (int i = 0; i < n.Length; ++i) {
1132 string opt = f + n [i].ToString ();
1133 string rn = n [i].ToString ();
1134 if (!Contains (rn)) {
1137 throw new OptionException (string.Format (localizer (
1138 "Cannot bundle unregistered option '{0}'."), opt), opt);
1141 switch (p.OptionValueType) {
1142 case OptionValueType.None:
1143 Invoke (c, opt, n, p);
1145 case OptionValueType.Optional:
1146 case OptionValueType.Required: {
1147 string v = n.Substring (i+1);
1150 ParseValue (v.Length != 0 ? v : null, c);
1154 throw new InvalidOperationException ("Unknown OptionValueType: " + p.OptionValueType);
1160 private static void Invoke (OptionContext c, string name, string value, Option option)
1162 c.OptionName = name;
1164 c.OptionValues.Add (value);
1168 private const int OptionWidth = 29;
1169 private const int Description_FirstWidth = 80 - OptionWidth;
1170 private const int Description_RemWidth = 80 - OptionWidth - 2;
1172 public void WriteOptionDescriptions (TextWriter o)
1174 foreach (Option p in this) {
1180 Category c = p as Category;
1182 WriteDescription (o, p.Description, "", 80, 80);
1186 if (!WriteOptionPrototype (o, p, ref written))
1189 if (written < OptionWidth)
1190 o.Write (new string (' ', OptionWidth - written));
1193 o.Write (new string (' ', OptionWidth));
1196 WriteDescription (o, p.Description, new string (' ', OptionWidth+2),
1197 Description_FirstWidth, Description_RemWidth);
1200 foreach (ArgumentSource s in sources) {
1201 string[] names = s.GetNames ();
1202 if (names == null || names.Length == 0)
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]);
1214 if (written < OptionWidth)
1215 o.Write (new string (' ', OptionWidth - written));
1218 o.Write (new string (' ', OptionWidth));
1221 WriteDescription (o, s.Description, new string (' ', OptionWidth+2),
1222 Description_FirstWidth, Description_RemWidth);
1226 void WriteDescription (TextWriter o, string value, string prefix, int firstWidth, int remWidth)
1228 bool indent = false;
1229 foreach (string line in GetLines (localizer (GetDescription (value)), firstWidth, remWidth)) {
1237 bool WriteOptionPrototype (TextWriter o, Option p, ref int written)
1239 string[] names = p.Names;
1241 int i = GetNextOptionIndex (names, 0);
1242 if (i == names.Length)
1245 if (names [i].Length == 1) {
1246 Write (o, ref written, " -");
1247 Write (o, ref written, names [0]);
1250 Write (o, ref written, " --");
1251 Write (o, ref written, names [0]);
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]);
1261 if (p.OptionValueType == OptionValueType.Optional ||
1262 p.OptionValueType == OptionValueType.Required) {
1263 if (p.OptionValueType == OptionValueType.Optional) {
1264 Write (o, ref written, localizer ("["));
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]
1270 for (int c = 1; c < p.MaxValueCount; ++c) {
1271 Write (o, ref written, localizer (sep + GetArgumentName (c, p.MaxValueCount, p.Description)));
1273 if (p.OptionValueType == OptionValueType.Optional) {
1274 Write (o, ref written, localizer ("]"));
1280 static int GetNextOptionIndex (string[] names, int i)
1282 while (i < names.Length && names [i] == "<>") {
1288 static void Write (TextWriter o, ref int n, string s)
1294 private static string GetArgumentName (int index, int maxIndex, string description)
1296 if (description == null)
1297 return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
1300 nameStart = new string[]{"{0:", "{"};
1302 nameStart = new string[]{"{" + index + ":"};
1303 for (int i = 0; i < nameStart.Length; ++i) {
1306 start = description.IndexOf (nameStart [i], j);
1307 } while (start >= 0 && j != 0 ? description [j++ - 1] == '{' : false);
1310 int end = description.IndexOf ("}", start);
1313 return description.Substring (start + nameStart [i].Length, end - start - nameStart [i].Length);
1315 return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
1318 private static string GetDescription (string description)
1320 if (description == null)
1321 return string.Empty;
1322 StringBuilder sb = new StringBuilder (description.Length);
1324 for (int i = 0; i < description.Length; ++i) {
1325 switch (description [i]) {
1336 if ((i+1) == description.Length || description [i+1] != '}')
1337 throw new InvalidOperationException ("Invalid option description: " + description);
1342 sb.Append (description.Substring (start, i - start));
1353 sb.Append (description [i]);
1357 return sb.ToString ();
1360 private static IEnumerable<string> GetLines (string description, int firstWidth, int remWidth)
1362 return StringCoda.WrappedLines (description, firstWidth, remWidth);