From 140f3a075990a7409409bc0a25bb81c0f620f84d Mon Sep 17 00:00:00 2001 From: kevans Date: Thu, 22 Feb 2018 01:57:38 +0000 Subject: [PATCH] lualoader: Attach cli command functions to cli module Instead of the global namespace, let's attach these to the cli module. Other users, including the "local" module, can attach functions to the cli module at will to add other cli commands and things will still Just Work. This distills down the candidates for functions that may be invoked via the cli to a minimal set (boot, autoboot, arguments), rather than any function that happens to live in the global lua namespace. --- stand/lua/cli.lua | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/stand/lua/cli.lua b/stand/lua/cli.lua index b68e1eb987ce..c95d09a5fad6 100644 --- a/stand/lua/cli.lua +++ b/stand/lua/cli.lua @@ -64,24 +64,6 @@ local parse_boot_args = function(argv, with_kernel) end end --- Globals - -function boot(...) - local cmd_name, argv = cli.arguments(...) - local kernel, argstr = parse_boot_args(argv) - if kernel ~= nil then - loader.perform("unload") - config.selectkernel(kernel) - end - core.boot(argstr) -end - -function autoboot(...) - local cmd_name, argv = cli.arguments(...) - local argstr = parse_boot_args(argv, false) - core.autoboot(argstr) -end - -- Declares a global function cli_execute that attempts to dispatch the -- arguments passed as a lua function. This gives lua a chance to intercept -- builtin CLI commands like "boot" @@ -94,7 +76,7 @@ function cli_execute(...) end local cmd_name = argv[1] - local cmd = _G[cmd_name] + local cmd = cli[cmd_name] if cmd ~= nil and type(cmd) == "function" then -- Pass argv wholesale into cmd. We could omit argv[0] since the -- traditional reasons for including it don't necessarily apply, @@ -109,6 +91,23 @@ end -- Module exports +function cli.boot(...) + local cmd_name, argv = cli.arguments(...) + local kernel, argstr = parse_boot_args(argv) + if kernel ~= nil then + loader.perform("unload") + config.selectkernel(kernel) + end + core.boot(argstr) +end + +function cli.autoboot(...) + local cmd_name, argv = cli.arguments(...) + local argstr = parse_boot_args(argv, false) + core.autoboot(argstr) +end + +-- Used for splitting cli varargs into cmd_name and the rest of argv function cli.arguments(...) local argv = {...} local cmd_name = ""