Whamcloud - gitweb
LU-16392 utils: use --list-commands for bash completion 84/49484/9
authorThomas Bertschinger <bertschinger@lanl.gov>
Wed, 21 Dec 2022 16:52:50 +0000 (11:52 -0500)
committerOleg Drokin <green@whamcloud.com>
Fri, 3 Feb 2023 06:50:48 +0000 (06:50 +0000)
commitb4cc570ad11c1c07a6e1d825787ccc62c1245ca1
treeebb3cbfe4645824e9f4c74a63cb62a16176fa523
parent2aa39b4470f61f3ce9d5c307b5faa9cb600cfb61
LU-16392 utils: use --list-commands for bash completion

The CLI utils lctl and lfs currently use a pseudo option
--non-existent-option to generate a list of completions. However, this
was broken when the help output for an invalid command was changed.
Using --list-commands instead means that the format of the help output
can be kept succinct.

However, currently there are 2 issues that make --list-commands
unsuitable.

First, --list-commands truncates long commands. This commit resolves
this by not truncating long commands, and removing the fixed-length
char buffer and writing directly to stdout so that the line length
can overflow slightly if needed.

Second, --list-commands recursively displays sub-commands. For
example, for `lctl`, it will display `pcc add`, `pcc del`, etc in
additon to just `pcc`. The bash completion tools would view these
as separate tokens and thus would inappropriately suggest `add`,
`del`, etc. as completions for `lctl`. This commit removes the
recursive behavior.

Removing the recursive behavior resolves an unrelated bug with the
recursion that can be observed for `lctl`, where a number of
top-level commands are skipped following recursion into a previous
sub-command, equal to the number of subcommands processed in the
recursive call. Specifically, the commands in the section "device
setup", e.g. `attach`, `detach`, were not displayed following the
recursive call into `pcc`.

Finally, this commit changes the command parser to recognize --help
and print the list of commands when this argument is seen.

Fixes: bc69a8d058 ("LU-8621 utils: cmd help to stdout or short cmd error")
Signed-off-by: Thomas Bertschinger <bertschinger@lanl.gov>
Change-Id: Ib6e139402b9cd18e5a54b8fd3d6a2652d301e736
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/49484
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Vitaliy Kuznetsov <vkuznetsov@ddn.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
libcfs/include/libcfs/util/parser.h
libcfs/libcfs/util/parser.c
lnet/utils/lnetctl.c
lnet/utils/lst.c
lustre/scripts/bash-completion/lustre
lustre/tests/sanity.sh
lustre/utils/lctl.c
lustre/utils/lfs.c