Update README.txt
[GitSharp.git] / Git / CmdParserOptionSet.cs
blob74d19b08403ec725adfc4e616fede3a9069b0b51
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3 * Copyright (C) 2009, Rolenun <rolenun@gmail.com>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 using System;
40 using System.Collections.Generic;
41 using NDesk.Options;
43 namespace GitSharp.CLI
45 public class CmdParserOptionSet : OptionSet
47 protected override void InsertItem(int index, Option item)
49 base.InsertItem (index, item);
52 protected override OptionContext CreateOptionContext()
54 return new OptionContext (this);
57 protected override bool Parse(string option, OptionContext c)
59 string f, n, s, v;
60 bool haveParts = GetOptionParts (option, out f, out n, out s, out v);
61 Option nextOption = null;
62 string newOption = option;
64 if (haveParts)
66 nextOption = Contains (n) ? this [n] : null;
67 newOption = f + n + (v != null ? s + v : "");
70 if (c.Option != null)
72 // Prevent --a --b
73 if (c.Option != null && haveParts)
75 throw new OptionException (
76 string.Format ("Found option `{0}' as value for option `{1}'.",
77 option, c.OptionName), c.OptionName);
80 // have a option w/ required value; try to concat values.
81 if (AppendValue (option, c))
83 if (!option.EndsWith ("\\") &&
84 c.Option.MaxValueCount == c.OptionValues.Count)
86 c.Option.Invoke (c);
89 return true;
91 else
92 base.Parse (newOption, c);
94 if (!haveParts || v == null)
96 // Not an option; let base handle as a non-option argument.
97 return base.Parse (newOption, c);
99 if (nextOption.OptionValueType != OptionValueType.None &&
100 v.EndsWith ("\\"))
102 c.Option = nextOption;
103 c.OptionValues.Add (v);
104 c.OptionName = f + n;
105 return true;
107 return base.Parse (newOption, c);
110 private bool AppendValue(string value, OptionContext c)
112 bool added = false;
113 string[] seps = c.Option.GetValueSeparators ();
114 foreach (var o in seps.Length != 0 ? value.Split(seps, StringSplitOptions.None) : new string[] { value })
116 int idx = c.OptionValues.Count - 1;
117 if (idx == -1 || !c.OptionValues[idx].EndsWith("\\"))
119 c.OptionValues.Add(o);
120 added = true;
122 else
124 c.OptionValues[idx] += value;
125 added = true;
128 return added;