summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.UITest.TestCloud/Mono.Options/ArgumentSource.cs
blob: da24d02b846e2105ed5169d8aa33a0f2b5643148 (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
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Mono.Options
{
	public abstract class ArgumentSource
	{
		public abstract string Description { get; }

		public abstract string[] GetNames();
		public abstract bool GetArguments(string value, out IEnumerable<string> replacement);

		public static IEnumerable<string> GetArgumentsFromFile(string file)
		{
			return GetArguments(File.OpenText(file), true);
		}

		public static IEnumerable<string> GetArguments(TextReader reader)
		{
			return GetArguments(reader, false);
		}

		// Cribbed from mcs/driver.cs:LoadArgs(string)
		static IEnumerable<string> GetArguments(TextReader reader, bool close)
		{
			try
			{
				StringBuilder arg = new StringBuilder();

				string line;
				while ((line = reader.ReadLine()) != null)
				{
					int t = line.Length;

					for (int i = 0; i < t; i++)
					{
						char c = line[i];

						if (c == '"' || c == '\'')
						{
							char end = c;

							for (i++; i < t; i++)
							{
								c = line[i];

								if (c == end)
									break;
								arg.Append(c);
							}
						}
						else if (c == ' ')
						{
							if (arg.Length > 0)
							{
								yield return arg.ToString();
								arg.Length = 0;
							}
						}
						else
							arg.Append(c);
					}
					if (arg.Length > 0)
					{
						yield return arg.ToString();
						arg.Length = 0;
					}
				}
			}
			finally
			{
				if (close)
					reader.Close();
			}
		}
	}
}