mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
拆分代码6
This commit is contained in:
50
shell/Commands/Script/ComCommand.cs
Normal file
50
shell/Commands/Script/ComCommand.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS.Commands.Script
|
||||
{
|
||||
public static class ComCommand
|
||||
{
|
||||
public static void ExecuteCommandFile(string args, CMLeonOS.FileSystem fileSystem, Shell shell, Action<string> showError, Action<string> showWarning)
|
||||
{
|
||||
if (string.IsNullOrEmpty(args))
|
||||
{
|
||||
showError("Usage: com <filename.cm>");
|
||||
return;
|
||||
}
|
||||
|
||||
string filePath = fileSystem.GetFullPath(args);
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
showError($"File not found: {args}");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string[] lines = fileSystem.ReadFile(filePath).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
|
||||
|
||||
if (lines.Length == 0 || (lines.Length == 1 && string.IsNullOrWhiteSpace(lines[0])))
|
||||
{
|
||||
showWarning("Command file is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("#"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
shell.ExecuteCommand(line);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
showError($"Error executing command file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user