-
Notifications
You must be signed in to change notification settings - Fork 123
/
CommandLine.cs
145 lines (121 loc) · 5.56 KB
/
CommandLine.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
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
// original source: https://www.codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser
using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
namespace CommandLine
{
public static class Arguments
{
public static bool TryGetOptions(string[] args, bool inConsole, out string mode, out ushort port, out bool https)
{
var arguments = Parse(args);
var validArgs = true;
mode = arguments["m"] ?? arguments["mode"] ?? "start";
https = arguments["http"] == null && arguments["https"] != null;
var portString = arguments["p"] ?? arguments["port"] ?? "8080";
if (mode != "start" && mode != "attach" && mode != "kill")
{
if (inConsole)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid mode; Allowed values are start | attach | kill");
Console.ResetColor();
}
validArgs = false;
}
if (!ushort.TryParse(portString, out port) || port < 80)
{
if (inConsole)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid port number specified.");
Console.ResetColor();
}
validArgs = false;
}
if (arguments["h"] != null || arguments["help"] != null) validArgs = false;
if (inConsole && !validArgs)
{
Console.WriteLine();
Console.WriteLine(" Mode Argument Options (Defaults to start)");
Console.WriteLine(" -m | --mode start -> Start the SPA server and proxy to that.");
Console.WriteLine(" -m | --mode attach -> Attach to existing SPA server");
Console.WriteLine(" -m | --mode kill -> Shutdown any existing SPA server on the specified port (used after debugging in VS Code)");
Console.WriteLine();
Console.WriteLine(" Port Argument (Defaults to 8080)");
Console.WriteLine(" -p | --port 8080 -> Specify what port to start or attach to, minimum of 80");
Console.WriteLine();
Console.WriteLine(" HTTPS (Defaults to false)");
Console.WriteLine(" -https -> Uses HTTPS");
Console.WriteLine();
}
return validArgs;
}
public static StringDictionary Parse(string[] args)
{
var parameters = new StringDictionary();
Regex splitter = new Regex(@"^-{1,2}|^/|=|:",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex remover = new Regex(@"^['""]?(.*?)['""]?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
string parameter = null;
string[] parts;
// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples:
// -param1 value1 --param2 /param3:"Test-:-work"
// /param4=happy -param5 '--=nice=--'
foreach (string txt in args)
{
// Look for new parameters (-,/ or --) and a
// possible enclosed value (=,:)
parts = splitter.Split(txt, 3);
switch (parts.Length)
{
// Found a value (for the last parameter
// found (space separator))
case 1:
if (parameter != null)
{
if (!parameters.ContainsKey(parameter))
{
parts[0] =
remover.Replace(parts[0], "$1");
parameters.Add(parameter, parts[0]);
}
parameter = null;
}
// else Error: no parameter waiting for a value (skipped)
break;
// Found just a parameter
case 2:
// The last parameter is still waiting.
// With no value, set it to true.
if (parameter != null && !parameters.ContainsKey(parameter))
parameters.Add(parameter, "true");
parameter = parts[1];
break;
// Parameter with enclosed value
case 3:
// The last parameter is still waiting.
// With no value, set it to true.
if (parameter != null && !parameters.ContainsKey(parameter))
parameters.Add(parameter, "true");
parameter = parts[1];
// Remove possible enclosing characters (",')
if (!parameters.ContainsKey(parameter))
{
parts[2] = remover.Replace(parts[2], "$1");
parameters.Add(parameter, parts[2]);
}
parameter = null;
break;
}
}
// In case a parameter is still waiting
if (parameter != null && !parameters.ContainsKey(parameter))
parameters.Add(parameter, "true");
return parameters;
}
}
}