5
// Jonathan Pryor <jpryor@novell.com>
7
// Copyright (C) 2008 Novell (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
// gmcs -debug+ -r:System.Core Options.cs -o:NDesk.Options.dll
31
// gmcs -debug+ -d:LINQ -r:System.Core Options.cs -o:NDesk.Options.dll
33
// The LINQ version just changes the implementation of
34
// OptionSet.Parse(IEnumerable<string>), and confers no semantic changes.
37
// A Getopt::Long-inspired option parsing library for C#.
39
// NDesk.Options.OptionSet is built upon a key/value table, where the
40
// key is a option format string and the value is a delegate that is
41
// invoked when the format string is matched.
43
// Option format strings:
44
// Regex-like BNF Grammar:
47
// sep: ( [^{}]+ | '{' .+ '}' )?
48
// aliases: ( name type sep ) ( '|' name type sep )*
50
// Each '|'-delimited name is an alias for the associated action. If the
51
// format string ends in a '=', it has a required value. If the format
52
// string ends in a ':', it has an optional value. If neither '=' or ':'
53
// is present, no value is supported. `=' or `:' need only be defined on one
54
// alias, but if they are provided on more than one they must be consistent.
56
// Each alias portion may also end with a "key/value separator", which is used
57
// to split option values if the option accepts > 1 value. If not specified,
58
// it defaults to '=' and ':'. If specified, it can be any character except
59
// '{' and '}' OR the *string* between '{' and '}'. If no separator should be
60
// used (i.e. the separate values should be distinct arguments), then "{}"
61
// should be used as the separator.
63
// Options are extracted either from the current option by looking for
64
// the option name followed by an '=' or ':', or is taken from the
65
// following option IFF:
66
// - The current option does not contain a '=' or a ':'
67
// - The current option requires a value (i.e. not a Option type of ':')
69
// The `name' used in the option format string does NOT include any leading
70
// option indicator, such as '-', '--', or '/'. All three of these are
71
// permitted/required on any named option.
73
// Option bundling is permitted so long as:
74
// - '-' is used to start the option group
75
// - all of the bundled options are a single character
76
// - at most one of the bundled options accepts a value, and the value
77
// provided starts from the next character to the end of the string.
79
// This allows specifying '-a -b -c' as '-abc', and specifying '-D name=value'
82
// Option processing is disabled by specifying "--". All options after "--"
83
// are returned by OptionSet.Parse() unchanged and unprocessed.
85
// Unprocessed options are returned from OptionSet.Parse().
89
// OptionSet p = new OptionSet ()
90
// .Add ("v", v => ++verbose)
91
// .Add ("name=|value=", v => Console.WriteLine (v));
92
// p.Parse (new string[]{"-v", "--v", "/v", "-name=A", "/name", "B", "extra"});
94
// The above would parse the argument string array, and would invoke the
95
// lambda expression three times, setting `verbose' to 3 when complete.
96
// It would also print out "A" and "B" to standard output.
97
// The returned array would contain the string "extra".
99
// C# 3.0 collection initializers are supported and encouraged:
100
// var p = new OptionSet () {
101
// { "h|?|help", v => ShowHelp () },
104
// System.ComponentModel.TypeConverter is also supported, allowing the use of
105
// custom data types in the callback type; TypeConverter.ConvertFromString()
106
// is used to convert the value option to an instance of the specified
109
// var p = new OptionSet () {
110
// { "foo=", (Foo f) => Console.WriteLine (f.ToString ()) },
113
// Random other tidbits:
114
// - Boolean options (those w/o '=' or ':' in the option format string)
115
// are explicitly enabled if they are followed with '+', and explicitly
116
// disabled if they are followed with '-':
118
// var p = new OptionSet () {
119
// { "a", s => a = s },
121
// p.Parse (new string[]{"-a"}); // sets v != null
122
// p.Parse (new string[]{"-a+"}); // sets v != null
123
// p.Parse (new string[]{"-a-"}); // sets v == null
127
using System.Collections;
128
using System.Collections.Generic;
129
using System.Collections.ObjectModel;
130
using System.ComponentModel;
131
using System.Globalization;
133
using System.Runtime.Serialization;
134
using System.Security.Permissions;
136
using System.Text.RegularExpressions;
146
namespace NDesk.Options {
148
public class OptionValueCollection : IList, IList<string> {
150
List<string> values = new List<string> ();
153
internal OptionValueCollection (OptionContext c)
159
void ICollection.CopyTo (Array array, int index) {(values as ICollection).CopyTo (array, index);}
160
bool ICollection.IsSynchronized {get {return (values as ICollection).IsSynchronized;}}
161
object ICollection.SyncRoot {get {return (values as ICollection).SyncRoot;}}
164
#region ICollection<T>
165
public void Add (string item) {values.Add (item);}
166
public void Clear () {values.Clear ();}
167
public bool Contains (string item) {return values.Contains (item);}
168
public void CopyTo (string[] array, int arrayIndex) {values.CopyTo (array, arrayIndex);}
169
public bool Remove (string item) {return values.Remove (item);}
170
public int Count {get {return values.Count;}}
171
public bool IsReadOnly {get {return false;}}
175
IEnumerator IEnumerable.GetEnumerator () {return values.GetEnumerator ();}
178
#region IEnumerable<T>
179
public IEnumerator<string> GetEnumerator () {return values.GetEnumerator ();}
183
int IList.Add (object value) {return (values as IList).Add (value);}
184
bool IList.Contains (object value) {return (values as IList).Contains (value);}
185
int IList.IndexOf (object value) {return (values as IList).IndexOf (value);}
186
void IList.Insert (int index, object value) {(values as IList).Insert (index, value);}
187
void IList.Remove (object value) {(values as IList).Remove (value);}
188
void IList.RemoveAt (int index) {(values as IList).RemoveAt (index);}
189
bool IList.IsFixedSize {get {return false;}}
190
object IList.this [int index] {get {return this [index];} set {(values as IList)[index] = value;}}
194
public int IndexOf (string item) {return values.IndexOf (item);}
195
public void Insert (int index, string item) {values.Insert (index, item);}
196
public void RemoveAt (int index) {values.RemoveAt (index);}
198
private void AssertValid (int index)
200
if (c.Option == null)
201
throw new InvalidOperationException ("OptionContext.Option is null.");
202
if (index >= c.Option.MaxValueCount)
203
throw new ArgumentOutOfRangeException ("index");
204
if (c.Option.OptionValueType == OptionValueType.Required &&
205
index >= values.Count)
206
throw new OptionException (string.Format (
207
c.OptionSet.MessageLocalizer ("Missing required value for option '{0}'."), c.OptionName),
211
public string this [int index] {
214
return index >= values.Count ? null : values [index];
217
values [index] = value;
222
public List<string> ToList ()
224
return new List<string> (values);
227
public string[] ToArray ()
229
return values.ToArray ();
232
public override string ToString ()
234
return string.Join (", ", values.ToArray ());
238
public class OptionContext {
239
private Option option;
242
private OptionSet set;
243
private OptionValueCollection c;
245
public OptionContext (OptionSet set)
248
this.c = new OptionValueCollection (this);
251
public Option Option {
253
set {option = value;}
256
public string OptionName {
261
public int OptionIndex {
266
public OptionSet OptionSet {
270
public OptionValueCollection OptionValues {
275
public enum OptionValueType {
281
public abstract class Option {
282
string prototype, description;
284
OptionValueType type;
288
protected Option (string prototype, string description)
289
: this (prototype, description, 1)
293
protected Option (string prototype, string description, int maxValueCount)
295
if (prototype == null)
296
throw new ArgumentNullException ("prototype");
297
if (prototype.Length == 0)
298
throw new ArgumentException ("Cannot be the empty string.", "prototype");
299
if (maxValueCount < 0)
300
throw new ArgumentOutOfRangeException ("maxValueCount");
302
this.prototype = prototype;
303
this.names = prototype.Split ('|');
304
this.description = description;
305
this.count = maxValueCount;
306
this.type = ParsePrototype ();
308
if (this.count == 0 && type != OptionValueType.None)
309
throw new ArgumentException (
310
"Cannot provide maxValueCount of 0 for OptionValueType.Required or " +
311
"OptionValueType.Optional.",
313
if (this.type == OptionValueType.None && maxValueCount > 1)
314
throw new ArgumentException (
315
string.Format ("Cannot provide maxValueCount of {0} for OptionValueType.None.", maxValueCount),
317
if (Array.IndexOf (names, "<>") >= 0 &&
318
((names.Length == 1 && this.type != OptionValueType.None) ||
319
(names.Length > 1 && this.MaxValueCount > 1)))
320
throw new ArgumentException (
321
"The default option handler '<>' cannot require values.",
325
public string Prototype {get {return prototype;}}
326
public string Description {get {return description;}}
327
public OptionValueType OptionValueType {get {return type;}}
328
public int MaxValueCount {get {return count;}}
330
public string[] GetNames ()
332
return (string[]) names.Clone ();
335
public string[] GetValueSeparators ()
337
if (separators == null)
338
return new string [0];
339
return (string[]) separators.Clone ();
342
protected static T Parse<T> (string value, OptionContext c)
344
TypeConverter conv = TypeDescriptor.GetConverter (typeof (T));
348
t = (T) conv.ConvertFromString (value);
350
catch (Exception e) {
351
throw new OptionException (
353
c.OptionSet.MessageLocalizer ("Could not convert string `{0}' to type {1} for option `{2}'."),
354
value, typeof (T).Name, c.OptionName),
360
internal string[] Names {get {return names;}}
361
internal string[] ValueSeparators {get {return separators;}}
363
static readonly char[] NameTerminator = new char[]{'=', ':'};
365
private OptionValueType ParsePrototype ()
368
List<string> seps = new List<string> ();
369
for (int i = 0; i < names.Length; ++i) {
370
string name = names [i];
371
if (name.Length == 0)
372
throw new ArgumentException ("Empty option names are not supported.", "prototype");
374
int end = name.IndexOfAny (NameTerminator);
377
names [i] = name.Substring (0, end);
378
if (type == '\0' || type == name [end])
381
throw new ArgumentException (
382
string.Format ("Conflicting option types: '{0}' vs. '{1}'.", type, name [end]),
384
AddSeparators (name, end, seps);
388
return OptionValueType.None;
390
if (count <= 1 && seps.Count != 0)
391
throw new ArgumentException (
392
string.Format ("Cannot provide key/value separators for Options taking {0} value(s).", count),
396
this.separators = new string[]{":", "="};
397
else if (seps.Count == 1 && seps [0].Length == 0)
398
this.separators = null;
400
this.separators = seps.ToArray ();
403
return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
406
private static void AddSeparators (string name, int end, ICollection<string> seps)
409
for (int i = end+1; i < name.Length; ++i) {
413
throw new ArgumentException (
414
string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
420
throw new ArgumentException (
421
string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
423
seps.Add (name.Substring (start, i-start));
428
seps.Add (name [i].ToString ());
433
throw new ArgumentException (
434
string.Format ("Ill-formed name/value separator found in \"{0}\".", name),
438
public void Invoke (OptionContext c)
443
c.OptionValues.Clear ();
446
protected abstract void OnParseComplete (OptionContext c);
448
public override string ToString ()
455
public class OptionException : Exception {
456
private string option;
458
public OptionException ()
462
public OptionException (string message, string optionName)
465
this.option = optionName;
468
public OptionException (string message, string optionName, Exception innerException)
469
: base (message, innerException)
471
this.option = optionName;
474
protected OptionException (SerializationInfo info, StreamingContext context)
475
: base (info, context)
477
this.option = info.GetString ("OptionName");
480
public string OptionName {
481
get {return this.option;}
484
[SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
485
public override void GetObjectData (SerializationInfo info, StreamingContext context)
487
base.GetObjectData (info, context);
488
info.AddValue ("OptionName", option);
492
public delegate void OptionAction<TKey, TValue> (TKey key, TValue value);
494
public class OptionSet : KeyedCollection<string, Option>
497
: this (delegate (string f) {return f;})
501
public OptionSet (Converter<string, string> localizer)
503
this.localizer = localizer;
506
Converter<string, string> localizer;
508
public Converter<string, string> MessageLocalizer {
509
get {return localizer;}
512
protected override string GetKeyForItem (Option item)
515
throw new ArgumentNullException ("option");
516
if (item.Names != null && item.Names.Length > 0)
517
return item.Names [0];
518
// This should never happen, as it's invalid for Option to be
519
// constructed w/o any names.
520
throw new InvalidOperationException ("Option has no names!");
523
[Obsolete ("Use KeyedCollection.this[string]")]
524
protected Option GetOptionForName (string option)
527
throw new ArgumentNullException ("option");
529
return base [option];
531
catch (KeyNotFoundException) {
536
protected override void InsertItem (int index, Option item)
538
base.InsertItem (index, item);
542
protected override void RemoveItem (int index)
544
base.RemoveItem (index);
545
Option p = Items [index];
546
// KeyedCollection.RemoveItem() handles the 0th item
547
for (int i = 1; i < p.Names.Length; ++i) {
548
Dictionary.Remove (p.Names [i]);
552
protected override void SetItem (int index, Option item)
554
base.SetItem (index, item);
559
private void AddImpl (Option option)
562
throw new ArgumentNullException ("option");
563
List<string> added = new List<string> (option.Names.Length);
565
// KeyedCollection.InsertItem/SetItem handle the 0th name.
566
for (int i = 1; i < option.Names.Length; ++i) {
567
Dictionary.Add (option.Names [i], option);
568
added.Add (option.Names [i]);
572
foreach (string name in added)
573
Dictionary.Remove (name);
578
public new OptionSet Add (Option option)
584
sealed class ActionOption : Option {
585
Action<OptionValueCollection> action;
587
public ActionOption (string prototype, string description, int count, Action<OptionValueCollection> action)
588
: base (prototype, description, count)
591
throw new ArgumentNullException ("action");
592
this.action = action;
595
protected override void OnParseComplete (OptionContext c)
597
action (c.OptionValues);
601
public OptionSet Add (string prototype, Action<string> action)
603
return Add (prototype, null, action);
606
public OptionSet Add (string prototype, string description, Action<string> action)
609
throw new ArgumentNullException ("action");
610
Option p = new ActionOption (prototype, description, 1,
611
delegate (OptionValueCollection v) { action (v [0]); });
616
public OptionSet Add (string prototype, OptionAction<string, string> action)
618
return Add (prototype, null, action);
621
public OptionSet Add (string prototype, string description, OptionAction<string, string> action)
624
throw new ArgumentNullException ("action");
625
Option p = new ActionOption (prototype, description, 2,
626
delegate (OptionValueCollection v) {action (v [0], v [1]);});
631
sealed class ActionOption<T> : Option {
634
public ActionOption (string prototype, string description, Action<T> action)
635
: base (prototype, description, 1)
638
throw new ArgumentNullException ("action");
639
this.action = action;
642
protected override void OnParseComplete (OptionContext c)
644
action (Parse<T> (c.OptionValues [0], c));
648
sealed class ActionOption<TKey, TValue> : Option {
649
OptionAction<TKey, TValue> action;
651
public ActionOption (string prototype, string description, OptionAction<TKey, TValue> action)
652
: base (prototype, description, 2)
655
throw new ArgumentNullException ("action");
656
this.action = action;
659
protected override void OnParseComplete (OptionContext c)
662
Parse<TKey> (c.OptionValues [0], c),
663
Parse<TValue> (c.OptionValues [1], c));
667
public OptionSet Add<T> (string prototype, Action<T> action)
669
return Add (prototype, null, action);
672
public OptionSet Add<T> (string prototype, string description, Action<T> action)
674
return Add (new ActionOption<T> (prototype, description, action));
677
public OptionSet Add<TKey, TValue> (string prototype, OptionAction<TKey, TValue> action)
679
return Add (prototype, null, action);
682
public OptionSet Add<TKey, TValue> (string prototype, string description, OptionAction<TKey, TValue> action)
684
return Add (new ActionOption<TKey, TValue> (prototype, description, action));
687
protected virtual OptionContext CreateOptionContext ()
689
return new OptionContext (this);
693
public List<string> Parse (IEnumerable<string> arguments)
696
OptionContext c = CreateOptionContext ();
698
var def = GetOptionForName ("<>");
700
from argument in arguments
701
where ++c.OptionIndex >= 0 && (process || def != null)
705
: !Parse (argument, c)
707
? Unprocessed (null, def, c, argument)
711
? Unprocessed (null, def, c, argument)
715
List<string> r = unprocessed.ToList ();
716
if (c.Option != null)
721
public List<string> Parse (IEnumerable<string> arguments)
723
OptionContext c = CreateOptionContext ();
726
List<string> unprocessed = new List<string> ();
727
Option def = Contains ("<>") ? this ["<>"] : null;
728
foreach (string argument in arguments) {
730
if (argument == "--") {
735
Unprocessed (unprocessed, def, c, argument);
738
if (!Parse (argument, c))
739
Unprocessed (unprocessed, def, c, argument);
741
if (c.Option != null)
747
private static bool Unprocessed (ICollection<string> extra, Option def, OptionContext c, string argument)
750
extra.Add (argument);
753
c.OptionValues.Add (argument);
759
private readonly Regex ValueOption = new Regex (
760
@"^(?<flag>--|-|/)(?<name>[^:=]+)((?<sep>[:=])(?<value>.*))?$");
762
protected bool GetOptionParts (string argument, out string flag, out string name, out string sep, out string value)
764
if (argument == null)
765
throw new ArgumentNullException ("argument");
767
flag = name = sep = value = null;
768
Match m = ValueOption.Match (argument);
772
flag = m.Groups ["flag"].Value;
773
name = m.Groups ["name"].Value;
774
if (m.Groups ["sep"].Success && m.Groups ["value"].Success) {
775
sep = m.Groups ["sep"].Value;
776
value = m.Groups ["value"].Value;
781
protected virtual bool Parse (string argument, OptionContext c)
783
if (c.Option != null) {
784
ParseValue (argument, c);
789
if (!GetOptionParts (argument, out f, out n, out s, out v))
795
c.OptionName = f + n;
797
switch (p.OptionValueType) {
798
case OptionValueType.None:
799
c.OptionValues.Add (n);
802
case OptionValueType.Optional:
803
case OptionValueType.Required:
809
// no match; is it a bool option?
810
if (ParseBool (argument, n, c))
812
// is it a bundled option?
813
if (ParseBundledValue (f, string.Concat (n + s + v), c))
819
private void ParseValue (string option, OptionContext c)
822
foreach (string o in c.Option.ValueSeparators != null
823
? option.Split (c.Option.ValueSeparators, StringSplitOptions.None)
824
: new string[]{option}) {
825
c.OptionValues.Add (o);
827
if (c.OptionValues.Count == c.Option.MaxValueCount ||
828
c.Option.OptionValueType == OptionValueType.Optional)
830
else if (c.OptionValues.Count > c.Option.MaxValueCount) {
831
throw new OptionException (localizer (string.Format (
832
"Error: Found {0} option values when expecting {1}.",
833
c.OptionValues.Count, c.Option.MaxValueCount)),
838
private bool ParseBool (string option, string n, OptionContext c)
842
if (n.Length >= 1 && (n [n.Length-1] == '+' || n [n.Length-1] == '-') &&
843
Contains ((rn = n.Substring (0, n.Length-1)))) {
845
string v = n [n.Length-1] == '+' ? option : null;
846
c.OptionName = option;
848
c.OptionValues.Add (v);
855
private bool ParseBundledValue (string f, string n, OptionContext c)
859
for (int i = 0; i < n.Length; ++i) {
861
string opt = f + n [i].ToString ();
862
string rn = n [i].ToString ();
863
if (!Contains (rn)) {
866
throw new OptionException (string.Format (localizer (
867
"Cannot bundle unregistered option '{0}'."), opt), opt);
870
switch (p.OptionValueType) {
871
case OptionValueType.None:
872
Invoke (c, opt, n, p);
874
case OptionValueType.Optional:
875
case OptionValueType.Required: {
876
string v = n.Substring (i+1);
879
ParseValue (v.Length != 0 ? v : null, c);
883
throw new InvalidOperationException ("Unknown OptionValueType: " + p.OptionValueType);
889
private static void Invoke (OptionContext c, string name, string value, Option option)
893
c.OptionValues.Add (value);
897
private const int OptionWidth = 29;
899
public void WriteOptionDescriptions (TextWriter o)
901
foreach (Option p in this) {
903
if (!WriteOptionPrototype (o, p, ref written))
906
if (written < OptionWidth)
907
o.Write (new string (' ', OptionWidth - written));
910
o.Write (new string (' ', OptionWidth));
913
List<string> lines = GetLines (localizer (GetDescription (p.Description)));
914
o.WriteLine (lines [0]);
915
string prefix = new string (' ', OptionWidth+2);
916
for (int i = 1; i < lines.Count; ++i) {
918
o.WriteLine (lines [i]);
923
bool WriteOptionPrototype (TextWriter o, Option p, ref int written)
925
string[] names = p.Names;
927
int i = GetNextOptionIndex (names, 0);
928
if (i == names.Length)
931
if (names [i].Length == 1) {
932
Write (o, ref written, " -");
933
Write (o, ref written, names [0]);
936
Write (o, ref written, " --");
937
Write (o, ref written, names [0]);
940
for ( i = GetNextOptionIndex (names, i+1);
941
i < names.Length; i = GetNextOptionIndex (names, i+1)) {
942
Write (o, ref written, ", ");
943
Write (o, ref written, names [i].Length == 1 ? "-" : "--");
944
Write (o, ref written, names [i]);
947
if (p.OptionValueType == OptionValueType.Optional ||
948
p.OptionValueType == OptionValueType.Required) {
949
if (p.OptionValueType == OptionValueType.Optional) {
950
Write (o, ref written, localizer ("["));
952
Write (o, ref written, localizer ("=" + GetArgumentName (0, p.MaxValueCount, p.Description)));
953
string sep = p.ValueSeparators != null && p.ValueSeparators.Length > 0
954
? p.ValueSeparators [0]
956
for (int c = 1; c < p.MaxValueCount; ++c) {
957
Write (o, ref written, localizer (sep + GetArgumentName (c, p.MaxValueCount, p.Description)));
959
if (p.OptionValueType == OptionValueType.Optional) {
960
Write (o, ref written, localizer ("]"));
966
static int GetNextOptionIndex (string[] names, int i)
968
while (i < names.Length && names [i] == "<>") {
974
static void Write (TextWriter o, ref int n, string s)
980
private static string GetArgumentName (int index, int maxIndex, string description)
982
if (description == null)
983
return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
986
nameStart = new string[]{"{0:", "{"};
988
nameStart = new string[]{"{" + index + ":"};
989
for (int i = 0; i < nameStart.Length; ++i) {
992
start = description.IndexOf (nameStart [i], j);
993
} while (start >= 0 && j != 0 ? description [j++ - 1] == '{' : false);
996
int end = description.IndexOf ("}", start);
999
return description.Substring (start + nameStart [i].Length, end - start - nameStart [i].Length);
1001
return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1);
1004
private static string GetDescription (string description)
1006
if (description == null)
1007
return string.Empty;
1008
StringBuilder sb = new StringBuilder (description.Length);
1010
for (int i = 0; i < description.Length; ++i) {
1011
switch (description [i]) {
1022
if ((i+1) == description.Length || description [i+1] != '}')
1023
throw new InvalidOperationException ("Invalid option description: " + description);
1028
sb.Append (description.Substring (start, i - start));
1039
sb.Append (description [i]);
1043
return sb.ToString ();
1046
private static List<string> GetLines (string description)
1048
List<string> lines = new List<string> ();
1049
if (string.IsNullOrEmpty (description)) {
1050
lines.Add (string.Empty);
1053
int length = 80 - OptionWidth - 2;
1056
end = GetLineEnd (start, length, description);
1058
if (end < description.Length) {
1059
char c = description [end];
1060
if (c == '-' || (char.IsWhiteSpace (c) && c != '\n'))
1062
else if (c != '\n') {
1067
lines.Add (description.Substring (start, end - start));
1069
lines [lines.Count-1] += "-";
1072
if (start < description.Length && description [start] == '\n')
1074
} while (end < description.Length);
1078
private static int GetLineEnd (int start, int length, string description)
1080
int end = Math.Min (start + length, description.Length);
1082
for (int i = start; i < end; ++i) {
1083
switch (description [i]) {
1097
if (sep == -1 || end == description.Length)