1 | namespace HeuristicLab.BenchmarkSuite {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Globalization;
|
---|
5 | using System.IO;
|
---|
6 | using System.Linq;
|
---|
7 |
|
---|
8 | public static class ExampleArgumentConverter {
|
---|
9 | private static readonly char[] ArrayValueSeparator = { ' ', ',', ';' };
|
---|
10 | private static readonly char[] ArraySymbolTrim = { '[', ']', '(', ')' };
|
---|
11 | public static double[] ConvertDoubles(string input) {
|
---|
12 | return ConvertMultiple(input, str => double.Parse(str, CultureInfo.InvariantCulture));
|
---|
13 | }
|
---|
14 |
|
---|
15 | public static double ConvertDouble(string input) {
|
---|
16 | return double.Parse(input, CultureInfo.InvariantCulture);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public static long[] ConvertIntegers(string input) {
|
---|
20 | return ConvertMultiple(input, long.Parse);
|
---|
21 | }
|
---|
22 |
|
---|
23 | public static long ConvertInteger(string input) {
|
---|
24 |
|
---|
25 | return string.IsNullOrEmpty(input) ? default(long) : long.Parse(input);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public static T[] ConvertMultiple<T>(string input, Func<string, T> converter) {
|
---|
29 | return input
|
---|
30 | .Trim(ArraySymbolTrim)
|
---|
31 | .Split(ArrayValueSeparator)
|
---|
32 | .Where(s => !string.IsNullOrWhiteSpace(s))
|
---|
33 | .Select(converter)
|
---|
34 | .ToArray();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static IEnumerable<string> SplitByNewLine(string input) {
|
---|
38 | return input.Split(new[] { Environment.NewLine, "\n", "\r\n" }, StringSplitOptions.None);
|
---|
39 | }
|
---|
40 |
|
---|
41 | private static string[] trueFormats = { "true", "t", "1" };
|
---|
42 | private static string[] falseFormats = { "false", "f", "0" };
|
---|
43 |
|
---|
44 | public static bool ConvertBoolean(string input) {
|
---|
45 | var str = input.ToLower();
|
---|
46 |
|
---|
47 | if (trueFormats.Contains(str)) return true;
|
---|
48 | if (falseFormats.Contains(str)) return false;
|
---|
49 | throw new InvalidDataException(string.Format("Unable to parse {0} as boolean", str));
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static bool[] ConvertBooleans(string input) {
|
---|
53 | return input
|
---|
54 | .Trim(ArraySymbolTrim)
|
---|
55 | .Split(ArrayValueSeparator)
|
---|
56 | .Where(s => !string.IsNullOrWhiteSpace(s))
|
---|
57 | .Select(ConvertBoolean)
|
---|
58 | .ToArray();
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|