[PATCH 0/3] vsh: Fix handling of '--help' for all commands

Brown paper box was applied due to lack of paper bags. Peter Krempa (3): vsh: Fix '--help' option for virsh/virt-admin virshtest: Add tests for '--help' NEWS: Mention '--help' bug in virsh and virt-admin NEWS.rst | 15 +++++++ tests/virshtest.c | 3 ++ tests/virshtestdata/help-option.in | 4 ++ tests/virshtestdata/help-option.out | 64 +++++++++++++++++++++++++++++ tools/vsh.c | 5 ++- 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/virshtestdata/help-option.in create mode 100644 tests/virshtestdata/help-option.out -- 2.45.0

The refactor of the libvirt tools command parser introduced a bug where the '--help' option would cause an error: $ virsh list --help error: command 'list' doesn't support option --help rather than printing the help for the command as the help option is supposed to be handled separately from the real options. Re-introduce the separate handling to the new parser code. Fixes: 5540c3d2415c194b206f8946cf74b13648163332 Reported-by: Lili Zhu <lizhu@redhat.com> Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tools/vsh.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/vsh.c b/tools/vsh.c index e74045c24e..61a3066f49 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -1655,7 +1655,10 @@ vshCommandParse(vshControl *ctl, /* lookup the option. Note that vshCmdGetOption also resolves aliases * and thus the value possibly contained in the alias */ - if (!(opt = vshCmdGetOption(ctl, cmd, optionname, &optionvalue, report))) { + if (STREQ(optionname, "help")) { + cmd->helpOptionSeen = true; + g_clear_pointer(&optionvalue, g_free); + } else if (!(opt = vshCmdGetOption(ctl, cmd, optionname, &optionvalue, report))) { if (STRNEQ(cmd->def->name, "help")) goto out; -- 2.45.0

Add test cases for help handling. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/virshtest.c | 3 ++ tests/virshtestdata/help-option.in | 4 ++ tests/virshtestdata/help-option.out | 64 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 tests/virshtestdata/help-option.in create mode 100644 tests/virshtestdata/help-option.out diff --git a/tests/virshtest.c b/tests/virshtest.c index 03d499b759..b86c39285a 100644 --- a/tests/virshtest.c +++ b/tests/virshtest.c @@ -309,6 +309,9 @@ mymain(void) DO_TEST("#unbalanced; 'quotes\"\necho a # b"); DO_TEST("\\# ignored;echo a\n'#also' ignored"); + /* test of the --help option handling */ + DO_TEST_SCRIPT("help-option", NULL, VIRSH_DEFAULT, "-q"); + /* test of splitting in vshStringToArray */ DO_TEST_SCRIPT("echo-split", NULL, VIRSH_DEFAULT, "-q"); diff --git a/tests/virshtestdata/help-option.in b/tests/virshtestdata/help-option.in new file mode 100644 index 0000000000..655952eefb --- /dev/null +++ b/tests/virshtestdata/help-option.in @@ -0,0 +1,4 @@ +help echo +echo --help +echo --help=data +echo --help data diff --git a/tests/virshtestdata/help-option.out b/tests/virshtestdata/help-option.out new file mode 100644 index 0000000000..5b7510b612 --- /dev/null +++ b/tests/virshtestdata/help-option.out @@ -0,0 +1,64 @@ + NAME + echo - echo arguments. Used for internal testing. + + SYNOPSIS + echo [--shell] [--xml] [--split] [--err] [--prefix <string>] [<string>]... + + DESCRIPTION + Echo back arguments, possibly with quoting. Used for internal testing. + + OPTIONS + --shell escape for shell use + --xml escape for XML use + --split split each argument on ','; ',,' is an escape sequence + --err output to stderr + --prefix <string> prefix the message + [--string] <string>... arguments to echo + NAME + echo - echo arguments. Used for internal testing. + + SYNOPSIS + echo [--shell] [--xml] [--split] [--err] [--prefix <string>] [<string>]... + + DESCRIPTION + Echo back arguments, possibly with quoting. Used for internal testing. + + OPTIONS + --shell escape for shell use + --xml escape for XML use + --split split each argument on ','; ',,' is an escape sequence + --err output to stderr + --prefix <string> prefix the message + [--string] <string>... arguments to echo + NAME + echo - echo arguments. Used for internal testing. + + SYNOPSIS + echo [--shell] [--xml] [--split] [--err] [--prefix <string>] [<string>]... + + DESCRIPTION + Echo back arguments, possibly with quoting. Used for internal testing. + + OPTIONS + --shell escape for shell use + --xml escape for XML use + --split split each argument on ','; ',,' is an escape sequence + --err output to stderr + --prefix <string> prefix the message + [--string] <string>... arguments to echo + NAME + echo - echo arguments. Used for internal testing. + + SYNOPSIS + echo [--shell] [--xml] [--split] [--err] [--prefix <string>] [<string>]... + + DESCRIPTION + Echo back arguments, possibly with quoting. Used for internal testing. + + OPTIONS + --shell escape for shell use + --xml escape for XML use + --split split each argument on ','; ',,' is an escape sequence + --err output to stderr + --prefix <string> prefix the message + [--string] <string>... arguments to echo -- 2.45.0

Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- NEWS.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/NEWS.rst b/NEWS.rst index bca18e8c43..b6985980ba 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -37,6 +37,21 @@ v10.4.0 (unreleased) * **Bug fixes** + * virsh/virt-admin: Fix ``--help`` option for all commands + + A bug introduced in `v10.3.0 (2024-05-02)`_ caused that the attempt to print + help for any command by using the ``--help`` option in ``virsh`` and + ``virt-admin`` would print:: + + $ virsh list --help + error: command 'list' doesn't support option --help + + instead of the help output. A workaround for the affected version is to use + the help command:: + + $ virsh help list + + v10.3.0 (2024-05-02) ==================== -- 2.45.0

On Wed, May 15, 2024 at 09:04:37AM +0200, Peter Krempa wrote:
Brown paper box was applied due to lack of paper bags.
Peter Krempa (3): vsh: Fix '--help' option for virsh/virt-admin virshtest: Add tests for '--help' NEWS: Mention '--help' bug in virsh and virt-admin
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
NEWS.rst | 15 +++++++ tests/virshtest.c | 3 ++ tests/virshtestdata/help-option.in | 4 ++ tests/virshtestdata/help-option.out | 64 +++++++++++++++++++++++++++++ tools/vsh.c | 5 ++- 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/virshtestdata/help-option.in create mode 100644 tests/virshtestdata/help-option.out
-- 2.45.0
participants (2)
-
Martin Kletzander
-
Peter Krempa