-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cs
More file actions
113 lines (100 loc) · 3.66 KB
/
Copy pathcommand.cs
File metadata and controls
113 lines (100 loc) · 3.66 KB
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
using System.Text.RegularExpressions;
internal class CommandLineOptions
{
public int Port { get; private set; } = -1;
public string? FooterHtml { get; private set; }
public bool ShouldExit { get; private set; }
public static CommandLineOptions Parse(string[] args, string version)
{
var opt = new CommandLineOptions();
string? rawFooter = null;
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-p":
if (i + 1 < args.Length && int.TryParse(args[i + 1], out int p))
{
opt.Port = p;
i++;
}
break;
case "-c":
if (i + 1 < args.Length)
{
rawFooter = args[i + 1];
i++;
}
break;
case "-h":
case "--help":
ShowHelp(version);
opt.ShouldExit = true;
return opt;
}
}
if (opt.Port <= 0)
{
opt.Port = 5000;
Console.WriteLine();
Console.Write("no set port, or have error, so we will try using 5000 port");
Console.WriteLine();
}
if (opt.Port > 65535)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("error: port must 1~65535, and you can using -p to set port.");
Console.WriteLine("if not set port, we will try using 5000 port");
Console.ResetColor();
opt.ShouldExit = true;
return opt;
}
Show(version, opt.Port);
if (!string.IsNullOrWhiteSpace(rawFooter))
{
Console.WriteLine();
Console.WriteLine("you using -c, so we will add this at foter text");
Console.WriteLine(rawFooter);
Console.WriteLine();
if (Regex.IsMatch(rawFooter, @"[<>&""']"))
{
opt.FooterHtml = rawFooter;
}
else
{
Console.WriteLine("tip£ºwe not find something like <>&\"' in your parameters(-c). So we will add <p> </p> for you.");
Console.WriteLine();
opt.FooterHtml = "<p>" + rawFooter + "</p>";
}
}
return opt;
}
private static void ShowHelp(string version)
{
Console.WriteLine();
Console.WriteLine($"WebStatus {version}");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" WebStatus [-p <port>] [-c <text>] [-h]");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" -p <port> Set listen port (default: 5000)");
Console.WriteLine(" -c <text> Custom footer text shown at page bottom");
Console.WriteLine(" -h Show this help and exit");
Console.WriteLine();
}
private static void Show(string version, int port)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(" ");
Console.WriteLine("======================================");
Console.WriteLine($" WebStatus {version}");
Console.WriteLine(" by Da_nuo | 2026");
Console.WriteLine(" For internal use only. Do not distribute without permission.");
Console.WriteLine(" you can use -h to know more");
Console.WriteLine($" Listen : {port}");
Console.WriteLine("======================================");
Console.WriteLine(" ");
Console.ResetColor();
}
}