From ea8d082f0135b7a9b008367de4f5ee554526bb40 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Tue, 28 Jan 2025 15:24:21 +0100 Subject: [PATCH] btrfs-progs: help: recognize --help option among other options There's a report that handling of --help in connection with other option is confusing in some cases: Various sub-commands to btrfs claim --help is an unrecognized option if there are any other options on the CLI. Examples of --help being unrecognized. $ btrfs filesystem defragment -v --help btrfs filesystem defragment: unrecognized option '--help' Try 'btrfs filesystem defragment --help' for more information $ btrfs balance start -v --help $ btrfs balabtrfs balance start -v --help btrfs balance start: unrecognized option '--help' Try 'btrfs balance start --help' for more information Alternatively, some sub-commands support --help even if there are extra options on the CLI: $ btrfs filesystem df -v --help usage: btrfs filesystem df [options] Show space usage information for a mount point -b|--raw raw numbers in bytes -h|--human-readable human friendly numbers, base 1024 (default) -H human friendly numbers, base 1000 --iec use 1024 as a base (KiB, MiB, GiB, TiB) --si use 1000 as a base (kB, MB, GB, TB) -k|--kbytes show sizes in KiB, or kB with --si -m|--mbytes show sizes in MiB, or MB with --si -g|--gbytes show sizes in GiB, or GB with --si -t|--tbytes show sizes in TiB, or TB with --si Global options: --format TYPE where TYPE is: text Update clean_args_no_options() to detect unrecognized options properly and do not show full help text (which usued to be done in the past but is confusing and not pointing to the problem). There's also a mixup of global verbosity options and command-specific verbosity options (now deprecated), so in some commands they are recognized. Also handle the --help option for commands without options so the following works: $ btrfs fi df -v --help / btrfs filesystem df: invalid option 'v' Try 'btrfs filesystem df --help' for more information $ btrfs fi df -H --help / usage: btrfs filesystem df [options] Show space usage information for a mount point -b|--raw raw numbers in byte ... Issue: #889 Signed-off-by: David Sterba --- common/help.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/common/help.c b/common/help.c index 0ee00a3a1..a77b47ed5 100644 --- a/common/help.c +++ b/common/help.c @@ -87,13 +87,14 @@ int check_argc_max(int nargs, int expected) /* * Preprocess @argv with getopt_long to reorder options and consume the "--" * option separator. - * Unknown short and long options are reported, optionally the @usage is printed - * before exit. + * Unknown short and long options are reported. Also consume the --help + * option in case it's for a command without any options. */ void clean_args_no_options(const struct cmd_struct *cmd, int argc, char *argv[]) { static const struct option long_options[] = { - {NULL, 0, NULL, 0} + { "help", no_argument, NULL, GETOPT_VAL_HELP }, + { NULL, 0, NULL, 0 } }; while (1) { @@ -103,9 +104,13 @@ void clean_args_no_options(const struct cmd_struct *cmd, int argc, char *argv[]) break; switch (c) { - default: + case GETOPT_VAL_HELP: if (cmd->usagestr) usage(cmd, 1); + break; + default: + if (cmd->usagestr) + usage_unknown_option(cmd, argv); } } }