Skip to content

Commit

Permalink
cmd: add 'help' subcommand to zpool and zfs
Browse files Browse the repository at this point in the history
'program help subcommand' is a reasonably common pattern for
multifunction command-line programs. This commit adds support for that
style to the zpool and zfs commands.

When run as 'zpool help [<topic>]' or 'zfs help [<topic>]', executes the
'man' program on the PATH with the most likely manpage name for the
requested topic: "zpool-<topic>" or "zfs-<topic>" for subcommands, or
"zpool<topic>" or "zfs<topic>" for the "concepts" and "props" topics.
If no topic is supplied, uses the top "zpool" or "zfs" pages.

Reviewed-by: Brian Behlendorf <[email protected]>
Reviewed-by: Kay Pedersen <[email protected]>
Signed-off-by: Rob Norris <[email protected]>
Closes openzfs#15288
  • Loading branch information
robn authored and behlendorf committed Sep 20, 2023
1 parent 1a2b8a1 commit 3b7ec5e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
30 changes: 30 additions & 0 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ static int zfs_do_zone(int argc, char **argv);
static int zfs_do_unzone(int argc, char **argv);
#endif

static int zfs_do_help(int argc, char **argv);

/*
* Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
*/
Expand Down Expand Up @@ -606,6 +608,9 @@ usage(boolean_t requested)
(void) fprintf(fp,
gettext("\nFor the delegated permission list, run: %s\n"),
"zfs allow|unallow");
(void) fprintf(fp,
gettext("\nFor further help on a command or topic, "
"run: %s\n"), "zfs help [<topic>]");
}

/*
Expand Down Expand Up @@ -8726,6 +8731,25 @@ zfs_do_version(int argc, char **argv)
return (zfs_version_print() != 0);
}

/* Display documentation */
static int
zfs_do_help(int argc, char **argv)
{
char page[MAXNAMELEN];
if (argc < 3 || strcmp(argv[2], "zfs") == 0)
strcpy(page, "zfs");
else if (strcmp(argv[2], "concepts") == 0 ||
strcmp(argv[2], "props") == 0)
snprintf(page, sizeof (page), "zfs%s", argv[2]);
else
snprintf(page, sizeof (page), "zfs-%s", argv[2]);

execlp("man", "man", page, NULL);

fprintf(stderr, "couldn't run man program: %s", strerror(errno));
return (-1);
}

int
main(int argc, char **argv)
{
Expand Down Expand Up @@ -8781,6 +8805,12 @@ main(int argc, char **argv)
if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
return (zfs_do_version(argc, argv));

/*
* Special case 'help'
*/
if (strcmp(cmdname, "help") == 0)
return (zfs_do_help(argc, argv));

if ((g_zfs = libzfs_init()) == NULL) {
(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
return (1);
Expand Down
31 changes: 31 additions & 0 deletions cmd/zpool/zpool_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ static int zpool_do_version(int, char **);

static int zpool_do_wait(int, char **);

static int zpool_do_help(int argc, char **argv);

static zpool_compat_status_t zpool_do_load_compat(
const char *, boolean_t *);

Expand Down Expand Up @@ -538,6 +540,10 @@ usage(boolean_t requested)
(void) fprintf(fp, "%s",
get_usage(command_table[i].usage));
}

(void) fprintf(fp,
gettext("\nFor further help on a command or topic, "
"run: %s\n"), "zpool help [<topic>]");
} else {
(void) fprintf(fp, gettext("usage:\n"));
(void) fprintf(fp, "%s", get_usage(current_command->usage));
Expand Down Expand Up @@ -11051,6 +11057,25 @@ zpool_do_version(int argc, char **argv)
return (zfs_version_print() != 0);
}

/* Display documentation */
static int
zpool_do_help(int argc, char **argv)
{
char page[MAXNAMELEN];
if (argc < 3 || strcmp(argv[2], "zpool") == 0)
strcpy(page, "zpool");
else if (strcmp(argv[2], "concepts") == 0 ||
strcmp(argv[2], "props") == 0)
snprintf(page, sizeof (page), "zpool%s", argv[2]);
else
snprintf(page, sizeof (page), "zpool-%s", argv[2]);

execlp("man", "man", page, NULL);

fprintf(stderr, "couldn't run man program: %s", strerror(errno));
return (-1);
}

/*
* Do zpool_load_compat() and print error message on failure
*/
Expand Down Expand Up @@ -11118,6 +11143,12 @@ main(int argc, char **argv)
if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
return (zpool_do_version(argc, argv));

/*
* Special case 'help'
*/
if (strcmp(cmdname, "help") == 0)
return (zpool_do_help(argc, argv));

if ((g_zfs = libzfs_init()) == NULL) {
(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
return (1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ usage:
For the property list, run: zfs set|get
For the delegated permission list, run: zfs allow|unallow")
For the delegated permission list, run: zfs allow|unallow
For further help on a command or topic, run: zfs help [<topic>]")
cnt=0
for cmd in ${neg_cmds[@]}; do
log_mustnot zfs program $cmd $TESTPOOL $TESTZCP $TESTDS 2>&1
Expand Down

0 comments on commit 3b7ec5e

Please sign in to comment.