From b6ab1374d7504ff4c7badbcd9ef3970f931034ff Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Tue, 2 Sep 2025 14:07:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(commands):=20=E6=B7=BB=E5=8A=A0=E5=88=97?= =?UTF-8?q?=E5=87=BA=E6=89=80=E6=9C=89=E5=8F=AF=E7=94=A8=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现一个commands命令用于列出LeonOS中所有可用的程序命令,支持--help和--verbose选项 --- .../lua/rom/programs/commands.lua | 64 +++++++++++++++++++ installer.lua | 2 +- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 data/computercraft/lua/rom/programs/commands.lua diff --git a/data/computercraft/lua/rom/programs/commands.lua b/data/computercraft/lua/rom/programs/commands.lua new file mode 100644 index 0000000..4a8c992 --- /dev/null +++ b/data/computercraft/lua/rom/programs/commands.lua @@ -0,0 +1,64 @@ +-- commands.lua: List all available commands +local shell = require("shell") +local fs = require("fs") + +-- 帮助信息函数 +local function printHelp() + print("Usage: commands [options]") + print("Options:") + print(" --help, -h Show this help message") + print(" --verbose, -v Show command file paths") + print(" +Lists all available commands in LeonOS.") +end + +-- 主函数 +local function main(args) + -- 处理命令行参数 + local showHelp = false + local verbose = false + + for _, arg in ipairs(args) do + if arg == "--help" or arg == "-h" then + showHelp = true + elseif arg == "--verbose" or arg == "-v" then + verbose = true + end + end + + if showHelp then + printHelp() + return + end + + -- 获取命令列表 + local programDir = "/rom/programs" + local files = fs.list(programDir) + local commands = {} + + for _, file in ipairs(files) do + if file:sub(-4) == ".lua" then + local cmdName = file:sub(1, -5) + table.insert(commands, {name = cmdName, path = programDir .. "/" .. file}) + end + end + + -- 排序命令列表 + table.sort(commands, function(a, b) + return a.name < b.name + end) + + -- 显示命令列表 + print("Available commands (" .. #commands .. "):") + for _, cmd in ipairs(commands) do + if verbose then + print(string.format(" %-15s - %s", cmd.name, cmd.path)) + else + print(string.format(" %-15s", cmd.name)) + end + end +end + +-- 运行主函数 +local args = {...} +main(args) \ No newline at end of file diff --git a/installer.lua b/installer.lua index a09ed23..a57049d 100644 --- a/installer.lua +++ b/installer.lua @@ -1,5 +1,5 @@ -- LeonOS installer -local INSTALLER_VERSION = "0.3.7 Beta 4" +local INSTALLER_VERSION = "0.3.7 Beta 5" local DEFAULT_ROM_DIR = "/leonos" print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")