-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgHandler.cs
More file actions
255 lines (219 loc) · 7.32 KB
/
ArgHandler.cs
File metadata and controls
255 lines (219 loc) · 7.32 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasingPathPositionCalculator;
public class ArgHandler
{
// 子命令处理
public static void HandleGroupCommand(string[] args)
{
string subCommand = args[1].ToLower();
switch (subCommand)
{
case "create":
HandleGroupCreate(args);
break;
case "list":
Program.GroupList();
break;
case "remove":
HandleGroupRemove(args);
break;
case "export":
HandleGroupExport(args);
break;
default:
Console.WriteLine($"未知的 group 子命令: {subCommand}");
Console.WriteLine("输入\"help\"以获取帮助。");
break;
}
}
private static void HandleGroupCreate(string[] args)
{
string name = "";
bool withOpen = false;
for (int i = 2; i < args.Length; i++)
{
if (args[i] == "-o")
{
withOpen = true;
break;
}
else if (!args[i].StartsWith("-"))
name += args[i] + "_";
}
Program.GroupCreate(name, withOpen);
}
private static void HandleGroupRemove(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("错误:remove 命令需要指定 UID 或 使用'all'");
return;
}
string target = args[2].ToLower();
if (target == "all")
{
Console.Write("确定要删除所有路径组吗?(y/N): ");
string? confirm = Console.ReadLine()?.Trim().ToLower();
if (confirm == "y" || confirm == "yes")
{
Program.GroupRemoveAll();
Console.WriteLine("已删除所有路径组!");
}
else
{
Console.WriteLine("取消删除操作!");
}
return;
}
// 解析 UID 和选项
bool quite = false;
short uid = Utils.ParseInput<short>(Console.ReadLine(), "整数", x => x > 0, "UID必然大于0!");
for (int i = 3; i < args.Length; i++)
{
if (args[i] == "-q")
quite = true;
}
if(!Program.GroupRemove(uid, quite))
Console.WriteLine($"UID{uid} 不存在!");
}
private static void HandleGroupExport(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("错误:export 命令需要指定 UID 或 使用'all'");
return;
}
string target = args[2].ToLower();
if (target == "all")
{
bool divided = false;
for (int i = 3; i < args.Length; i++)
{
if (args[i] == "-d")
divided = true;
}
Program.ExportGroupAll(divided);
}
else
{
short uid = Utils.ParseInput<short>(Console.ReadLine(), "整数", x => x > 0, "UID必然大于0!");
Program.ExportGroup(uid);
}
}
public static void HandleLogCommand(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine(Log.GetCurrentStatus());
return;
}
string subCommand = args[1].ToLower();
bool realtime = args.Length > 2 && args[2].ToLower() == "intime";
switch (subCommand)
{
case "normal":
Log.SetLevel(LogLevel.NORMAL);
break;
case "warning":
Log.SetLevel(LogLevel.WARNING);
break;
case "fatal":
Log.SetLevel(LogLevel.FATAL);
break;
case "debug":
Log.SetLevel(LogLevel.DEBUG, realtime);
break;
case "show":
Utils.OpenExplorer(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log"));
break;
case "status":
Console.WriteLine(Log.GetCurrentStatus());
break;
default:
Console.WriteLine("未知的日志级别。可用选项: normal, warning, fatal, debug [intime]");
break;
}
}
public static void HandleClearCommand(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("参数数量不足,请指定要删除的目标");
return;
}
string target = args[1].ToLower();
switch (target)
{
case "log":
ClearDirectory("log", "日志");
break;
case "tmp":
ClearDirectory("tmp", "临时");
break;
default:
Console.WriteLine($"未知的目标 '{target}'");
break;
}
}
private static void ClearDirectory(string directoryName, string displayName)
{
try
{
string programDirectory = AppDomain.CurrentDomain.BaseDirectory;
string targetDirectory = Path.Combine(programDirectory, directoryName);
// 检查目录是否存在,如果不存在则创建并返回
if (!Directory.Exists(targetDirectory))
{
Directory.CreateDirectory(targetDirectory);
Console.WriteLine($"{displayName}目录不存在,已创建新目录: {targetDirectory}");
return;
}
// 获取目录中的所有文件
string[] files = Directory.GetFiles(targetDirectory);
if (files.Length == 0)
{
Console.WriteLine($"{displayName}目录中没有任何文件");
return;
}
// 显示将要删除的文件列表
Console.WriteLine($"找到 {files.Length} 个{displayName}文件:");
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
Console.WriteLine($" {Path.GetFileName(file)} ({fileInfo.Length} bytes, 修改时间: {fileInfo.LastWriteTime})");
}
// 确认操作
Console.Write($"确定要删除所有{displayName}文件吗?(y/N): ");
string confirmation = Console.ReadLine()?.Trim().ToLower() ?? "N";
if (confirmation != "y" && confirmation != "yes")
{
Console.WriteLine("操作已取消");
return;
}
// 删除文件
int deletedCount = 0;
foreach (string file in files)
{
try
{
File.Delete(file);
deletedCount++;
Console.WriteLine($"已删除: {Path.GetFileName(file)}");
}
catch (Exception ex)
{
Console.WriteLine($"删除文件失败 {Path.GetFileName(file)}: {ex.Message}");
}
}
Console.WriteLine($"成功删除 {deletedCount} 个{displayName}文件");
}
catch (Exception ex)
{
Console.WriteLine($"清除{displayName}目录时发生错误: {ex.Message}");
}
}
}