From 64c2722f61cb719f374325c3de07d0ded9785cd3 Mon Sep 17 00:00:00 2001 From: Maurizio Lombardi Date: Mon, 28 Jul 2025 17:55:04 +0200 Subject: [PATCH] nvmetcli: allow running arbitrary commands non-interactively This patch refactors the command-line argument handling to allow for the execution of any valid shell command directly from the system's command line, without entering the interactive mode. Previously, only a limited, hardcoded set of commands (`save`, `restore`, `clear`, `ls`) could be run non-interactively. This change removes the special case for the `ls` command and introduces a generic mechanism that concatenates all command-line arguments and executes them as a single command within the `nvmetcli` shell. This enhancement improves the scriptability of the tool. For example, users can now run complex commands like: $ nvmetcli /subsystems create nqn.2024-07.org.example:sub1 $ nvmetcli /ports ls The usage information has been updated to reflect this new functionality. Signed-off-by: Maurizio Lombardi Reviewed-by: Nilay Shroff Signed-off-by: Ales Novak Signed-off-by: Daniel Wagner --- nvmetcli | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/nvmetcli b/nvmetcli index c565510..bec4897 100755 --- a/nvmetcli +++ b/nvmetcli @@ -764,7 +764,8 @@ def usage(): print("syntax: %s save [file_to_save_to]" % sys.argv[0]) print(" %s restore [file_to_restore_from]" % sys.argv[0]) print(" %s clear" % sys.argv[0]) - print(" %s ls" % sys.argv[0]) + print(" %s --help (Print this information)" % sys.argv[0]) + print(" %s (Run shell command and exit)" % sys.argv[0]) sys.exit(-1) @@ -801,14 +802,14 @@ def clear(unused): nvme.Root().clear_existing() -def ls(unused): +def execute_cmd(cmd): shell = configshell.ConfigShell('~/.nvmetcli') UIRootNode(shell) - shell.run_cmdline("ls") + shell.run_cmdline(cmd) sys.exit(0) -funcs = dict(save=save, restore=restore, clear=clear, ls=ls) +funcs = dict(save=save, restore=restore, clear=clear) def main(): @@ -816,23 +817,23 @@ def main(): print("%s: must run as root." % sys.argv[0], file=sys.stderr) sys.exit(-1) - if len(sys.argv) > 3: - usage() - - if len(sys.argv) == 2 or len(sys.argv) == 3: + if len(sys.argv) > 1: if sys.argv[1] == "--help": usage() - if sys.argv[1] not in funcs.keys(): - usage() + if sys.argv[1] in funcs.keys(): + if len(sys.argv) > 3: + usage() + if len(sys.argv) == 3: + savefile = sys.argv[2] + else: + savefile = None - if len(sys.argv) == 3: - savefile = sys.argv[2] + funcs[sys.argv[1]](savefile) + return else: - savefile = None - - funcs[sys.argv[1]](savefile) - return + concat_args = ' '.join(sys.argv[1:]) + execute_cmd(concat_args) try: shell = configshell.ConfigShell('~/.nvmetcli')