Skip to content

Commit 61fd2d5

Browse files
committed
Added command line parameters
These will allow for some new debugging options in VS Code
1 parent 7d7f776 commit 61fd2d5

File tree

3 files changed

+168
-8
lines changed

3 files changed

+168
-8
lines changed

CommandLine.cs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// original source: https://www.codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser
2+
3+
using System;
4+
using System.Collections.Specialized;
5+
using System.Text.RegularExpressions;
6+
7+
namespace CommandLine
8+
{
9+
public static class Arguments
10+
{
11+
12+
public static bool TryGetOptions(string[] args, bool inConsole, out string mode, out ushort port, out bool https)
13+
{
14+
var arguments = Parse(args);
15+
var validArgs = true;
16+
17+
mode = arguments["m"] ?? arguments["mode"] ?? "start";
18+
https = arguments["http"] == null && arguments["https"] != null;
19+
var portString = arguments["p"] ?? arguments["port"] ?? "8080";
20+
21+
if (mode != "start" && mode != "attach" && mode != "kill")
22+
{
23+
if (inConsole)
24+
{
25+
Console.ForegroundColor = ConsoleColor.Red;
26+
Console.WriteLine("Invalid mode; Allowed values are start | attach | kill");
27+
Console.ResetColor();
28+
}
29+
validArgs = false;
30+
}
31+
32+
if (!ushort.TryParse(portString, out port) || port < 80)
33+
{
34+
if (inConsole)
35+
{
36+
Console.ForegroundColor = ConsoleColor.Red;
37+
Console.WriteLine("Invalid port number specified.");
38+
Console.ResetColor();
39+
}
40+
validArgs = false;
41+
}
42+
43+
if (arguments["h"] != null || arguments["help"] != null) validArgs = false;
44+
45+
if (inConsole && !validArgs)
46+
{
47+
Console.WriteLine();
48+
Console.WriteLine(" Mode Argument Options (Defaults to start)");
49+
Console.WriteLine(" -m | --mode start -> Start the SPA server and proxy to that.");
50+
Console.WriteLine(" -m | --mode attach -> Attach to existing SPA server");
51+
Console.WriteLine(" -m | --mode kill -> Shutdown any existing SPA server on the specified port (used after debugging in VS Code)");
52+
Console.WriteLine();
53+
Console.WriteLine(" Port Argument (Defaults to 8080)");
54+
Console.WriteLine(" -p | --port 8080 -> Specify what port to start or attach to, minimum of 80");
55+
Console.WriteLine();
56+
Console.WriteLine(" HTTPS (Defaults to false)");
57+
Console.WriteLine(" -https -> Uses HTTPS");
58+
Console.WriteLine();
59+
60+
}
61+
62+
return validArgs;
63+
64+
}
65+
66+
public static StringDictionary Parse(string[] args)
67+
{
68+
var parameters = new StringDictionary();
69+
Regex splitter = new Regex(@"^-{1,2}|^/|=|:",
70+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
71+
72+
Regex remover = new Regex(@"^['""]?(.*?)['""]?$",
73+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
74+
75+
string parameter = null;
76+
string[] parts;
77+
78+
// Valid parameters forms:
79+
// {-,/,--}param{ ,=,:}((",')value(",'))
80+
// Examples:
81+
// -param1 value1 --param2 /param3:"Test-:-work"
82+
// /param4=happy -param5 '--=nice=--'
83+
foreach (string txt in args)
84+
{
85+
// Look for new parameters (-,/ or --) and a
86+
// possible enclosed value (=,:)
87+
parts = splitter.Split(txt, 3);
88+
89+
switch (parts.Length)
90+
{
91+
// Found a value (for the last parameter
92+
// found (space separator))
93+
case 1:
94+
if (parameter != null)
95+
{
96+
if (!parameters.ContainsKey(parameter))
97+
{
98+
parts[0] =
99+
remover.Replace(parts[0], "$1");
100+
101+
parameters.Add(parameter, parts[0]);
102+
}
103+
parameter = null;
104+
}
105+
// else Error: no parameter waiting for a value (skipped)
106+
break;
107+
108+
// Found just a parameter
109+
case 2:
110+
// The last parameter is still waiting.
111+
// With no value, set it to true.
112+
if (parameter != null && !parameters.ContainsKey(parameter))
113+
parameters.Add(parameter, "true");
114+
115+
parameter = parts[1];
116+
break;
117+
118+
// Parameter with enclosed value
119+
case 3:
120+
// The last parameter is still waiting.
121+
// With no value, set it to true.
122+
if (parameter != null && !parameters.ContainsKey(parameter))
123+
parameters.Add(parameter, "true");
124+
125+
parameter = parts[1];
126+
127+
// Remove possible enclosing characters (",')
128+
if (!parameters.ContainsKey(parameter))
129+
{
130+
parts[2] = remover.Replace(parts[2], "$1");
131+
parameters.Add(parameter, parts[2]);
132+
}
133+
134+
parameter = null;
135+
break;
136+
}
137+
}
138+
// In case a parameter is still waiting
139+
if (parameter != null && !parameters.ContainsKey(parameter))
140+
parameters.Add(parameter, "true");
141+
142+
return parameters;
143+
}
144+
}
145+
}

Program.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
52
using Microsoft.AspNetCore.Hosting;
6-
using Microsoft.Extensions.Configuration;
73
using Microsoft.Extensions.Hosting;
8-
using Microsoft.Extensions.Logging;
4+
using VueCliMiddleware;
95

106
namespace AspNetCoreVueStarter
117
{
128
public class Program
139
{
1410
public static void Main(string[] args)
1511
{
12+
if (!CommandLine.Arguments.TryGetOptions(args, true, out string mode, out ushort port, out bool https)) return;
13+
14+
if (mode == "kill") {
15+
Console.WriteLine($"Killing process serving port {port}...");
16+
PidUtils.KillPort(port, true, true);
17+
return;
18+
}
19+
1620
CreateHostBuilder(args).Build().Run();
1721
}
1822

Startup.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public void ConfigureServices(IServiceCollection services)
3434
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3535
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3636
{
37+
38+
_ = CommandLine.Arguments.TryGetOptions(System.Environment.GetCommandLineArgs(), false, out string mode, out ushort port, out bool https);
39+
3740
if (env.IsDevelopment())
3841
{
3942
app.UseDeveloperExceptionPage();
@@ -45,7 +48,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4548
app.UseHsts();
4649
}
4750

48-
// app.UseHttpsRedirection();
51+
if (https) app.UseHttpsRedirection();
52+
4953
app.UseStaticFiles();
5054
if (!env.IsDevelopment())
5155
{
@@ -73,11 +77,18 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
7377

7478
if (env.IsDevelopment())
7579
{
80+
7681
// run npm process with client app
77-
spa.UseVueCli(npmScript: "serve", port: 8080, forceKill: true);
82+
if (mode == "start") {
83+
spa.UseVueCli(npmScript: "serve", port: port, forceKill: true, https: https);
84+
}
85+
7886
// if you just prefer to proxy requests from client app, use proxy to SPA dev server instead,
7987
// app should be already running before starting a .NET client:
80-
// spa.UseProxyToSpaDevelopmentServer("http://localhost:8080"); // your Vue app port
88+
// run npm process with client app
89+
if (mode == "attach") {
90+
spa.UseProxyToSpaDevelopmentServer($"{(https ? "https" : "http")}://localhost:{port}"); // your Vue app port
91+
}
8192
}
8293
});
8394
}

0 commit comments

Comments
 (0)