summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.UITest.TestCloud/Mono.Options/Option.cs
blob: f91934c26e9d180ad725f943060d4a984f77641c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Mono.Options
{
	public abstract class Option
	{
		static readonly char[] NameTerminator = { '=', ':' };

		protected Option(string prototype, string description)
			: this(prototype, description, 1, false)
		{
		}

		protected Option(string prototype, string description, int maxValueCount)
			: this(prototype, description, maxValueCount, false)
		{
		}

		protected Option(string prototype, string description, int maxValueCount, bool hidden)
		{
			if (prototype == null)
				throw new ArgumentNullException("prototype");
			if (prototype.Length == 0)
				throw new ArgumentException("Cannot be the empty string.", "prototype");
			if (maxValueCount < 0)
				throw new ArgumentOutOfRangeException("maxValueCount");

			Prototype = prototype;
			Description = description;
			MaxValueCount = maxValueCount;
			Names = (this is OptionSet.Category)
				// append GetHashCode() so that "duplicate" categories have distinct
				// names, e.g. adding multiple "" categories should be valid.
				? new[] { prototype + GetHashCode() }
				: prototype.Split('|');

			if (this is OptionSet.Category)
				return;

			OptionValueType = ParsePrototype();
			Hidden = hidden;

			if (MaxValueCount == 0 && OptionValueType != OptionValueType.None)
			{
				throw new ArgumentException(
					"Cannot provide maxValueCount of 0 for OptionValueType.Required or " +
					"OptionValueType.Optional.",
					"maxValueCount");
			}
			if (OptionValueType == OptionValueType.None && maxValueCount > 1)
			{
				throw new ArgumentException(
					string.Format("Cannot provide maxValueCount of {0} for OptionValueType.None.", maxValueCount),
					"maxValueCount");
			}
			if (Array.IndexOf(Names, "<>") >= 0 &&
			    ((Names.Length == 1 && OptionValueType != OptionValueType.None) ||
			     (Names.Length > 1 && MaxValueCount > 1)))
			{
				throw new ArgumentException(
					"The default option handler '<>' cannot require values.",
					"prototype");
			}
		}

		public string Prototype { get; }

		public string Description { get; }

		public OptionValueType OptionValueType { get; }

		public int MaxValueCount { get; }

		public bool Hidden { get; }

		internal string[] Names { get; }

		internal string[] ValueSeparators { get; private set; }

		public string[] GetNames()
		{
			return (string[])Names.Clone();
		}

		public string[] GetValueSeparators()
		{
			if (ValueSeparators == null)
				return new string[0];
			return (string[])ValueSeparators.Clone();
		}

		protected static T Parse<T>(string value, OptionContext c)
		{
			Type tt = typeof (T);
			bool nullable = tt.IsValueType && tt.IsGenericType &&
			                !tt.IsGenericTypeDefinition &&
			                tt.GetGenericTypeDefinition() == typeof (Nullable<>);
			Type targetType = nullable ? tt.GetGenericArguments()[0] : typeof (T);
			TypeConverter conv = TypeDescriptor.GetConverter(targetType);
			T t = default(T);
			try
			{
				if (value != null)
					t = (T)conv.ConvertFromString(value);
			}
			catch (Exception e)
			{
				throw new OptionException(
					string.Format(
						c.OptionSet.MessageLocalizer("Could not convert string `{0}' to type {1} for option `{2}'."),
						value, targetType.Name, c.OptionName),
					c.OptionName, e);
			}
			return t;
		}

		OptionValueType ParsePrototype()
		{
			char type = '\0';
			List<string> seps = new List<string>();
			for (int i = 0; i < Names.Length; ++i)
			{
				string name = Names[i];
				if (name.Length == 0)
					throw new ArgumentException("Empty option names are not supported.", "prototype");

				int end = name.IndexOfAny(NameTerminator);
				if (end == -1)
					continue;
				Names[i] = name.Substring(0, end);
				if (type == '\0' || type == name[end])
					type = name[end];
				else
				{
					throw new ArgumentException(
						string.Format("Conflicting option types: '{0}' vs. '{1}'.", type, name[end]),
						"prototype");
				}
				AddSeparators(name, end, seps);
			}

			if (type == '\0')
				return OptionValueType.None;

			if (MaxValueCount <= 1 && seps.Count != 0)
			{
				throw new ArgumentException(
					string.Format("Cannot provide key/value separators for Options taking {0} value(s).", MaxValueCount),
					"prototype");
			}
			if (MaxValueCount > 1)
			{
				if (seps.Count == 0)
					ValueSeparators = new[] { ":", "=" };
				else if (seps.Count == 1 && seps[0].Length == 0)
					ValueSeparators = null;
				else
					ValueSeparators = seps.ToArray();
			}

			return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
		}

		static void AddSeparators(string name, int end, ICollection<string> seps)
		{
			int start = -1;
			for (int i = end + 1; i < name.Length; ++i)
			{
				switch (name[i])
				{
					case '{':
						if (start != -1)
						{
							throw new ArgumentException(
								string.Format("Ill-formed name/value separator found in \"{0}\".", name),
								"prototype");
						}
						start = i + 1;
						break;
					case '}':
						if (start == -1)
						{
							throw new ArgumentException(
								string.Format("Ill-formed name/value separator found in \"{0}\".", name),
								"prototype");
						}
						seps.Add(name.Substring(start, i - start));
						start = -1;
						break;
					default:
						if (start == -1)
							seps.Add(name[i].ToString());
						break;
				}
			}
			if (start != -1)
			{
				throw new ArgumentException(
					string.Format("Ill-formed name/value separator found in \"{0}\".", name),
					"prototype");
			}
		}

		public void Invoke(OptionContext c)
		{
			OnParseComplete(c);
			c.OptionName = null;
			c.Option = null;
			c.OptionValues.Clear();
		}

		protected abstract void OnParseComplete(OptionContext c);

		public override string ToString()
		{
			return Prototype;
		}
	}
}