-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCommandLineArguments.cs
64 lines (58 loc) · 2.36 KB
/
CommandLineArguments.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace EXRViewer {
public static class CommandLineArguments {
/// <summary>
/// Parses command line arguments.
/// </summary>
/// <param name="arguments">String containing the arguments. Uses Environment.CommandLine if null. Does not allow escaped double quotes.</param>
/// <returns>List of arguments.</returns>
public static List<string> Parse(string arguments = null) {
return Parse(false, arguments);
}
/// <summary>
/// Parses command line arguments.
/// </summary>
/// <param name="allowEscapedDoubleQuotes">If true a sequence of \" between double quotes will be a single quote.</param>
/// <param name="arguments">String containing the arguments. Uses Environment.CommandLine if null.</param>
/// <returns>List of arguments.</returns>
public static List<string> Parse(bool allowEscapedDoubleQuotes, string arguments = null) {
Regex regex;
bool removeFirst = false;
if (arguments == null) {
arguments = Environment.CommandLine;
removeFirst = true;
}
List<string> args = new List<string>();
regex = new Regex(
allowEscapedDoubleQuotes ? "\"((\\\\\"|[^\"])*)\"+|[^\\s]+" : "\"([^\"]*)\"+|[^\\s]+",
RegexOptions.None
);
foreach (Match match in regex.Matches(arguments)) {
if (match.Success) {
string s = match.Value.Trim();
if (match.Groups[1].Success) {
if (allowEscapedDoubleQuotes) {
s = match.Groups[1].Value.Replace("\\\"", "\"");
}
else {
s = match.Groups[1].Value;
}
}
else {
s = match.Groups[0].Value.Trim();
}
args.Add(s);
}
}
if (removeFirst) {
args.RemoveAt(0);
}
return args;
}
}
}