mirror of
https://github.com/kdave/btrfs-progs
synced 2025-03-31 23:57:26 +00:00
btrfs-progs: help: add helper for unrecognized option error message
Currently any unrecognized option does not print very usable message and only dumps the whole help. Other common utilities (eg. from the util-linux suite) print a short message and point to help. And we're going to do the same. Example: $ btrfs device add --unknown device path btrfs device add: unrecognized option '--unknown' Try 'btrfs device add --help' for more information Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
bc78d1f217
commit
309719fb97
44
help.c
44
help.c
@ -238,6 +238,50 @@ void usage_command(const struct cmd_struct *cmd, int full, int err)
|
||||
usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
|
||||
}
|
||||
|
||||
__attribute__((noreturn))
|
||||
void usage_unknown_option(const char * const *usagestr, char **argv)
|
||||
{
|
||||
int i;
|
||||
int c;
|
||||
int prev = 0;
|
||||
|
||||
/*
|
||||
* Guess the command prefix, until the first option or argument
|
||||
* specifier
|
||||
*/
|
||||
i = 0;
|
||||
do {
|
||||
c = usagestr[0][i];
|
||||
if (c == '<' || c == '[' || (prev == ' ' && c == '-')) {
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
prev = c;
|
||||
i++;
|
||||
} while (c);
|
||||
|
||||
/*
|
||||
* Example:
|
||||
*
|
||||
* $ btrfs device add --unknown device path
|
||||
* btrfs device add: unrecognized option '--unknown'
|
||||
* Try 'btrfs device add --help' for more information
|
||||
*/
|
||||
|
||||
fprintf(stderr, "%.*s: ", i, usagestr[0]);
|
||||
if (!optopt) {
|
||||
/*
|
||||
* There's no better way to get the exact unrecognized token
|
||||
* from getopt
|
||||
*/
|
||||
fprintf(stderr, "unrecognized option '%s'\n", argv[optind - 1]);
|
||||
} else {
|
||||
fprintf(stderr, "invalid option '%c'\n", optopt);
|
||||
}
|
||||
fprintf(stderr, "Try '%.*s --help' for more information\n", i, usagestr[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
__attribute__((noreturn))
|
||||
void usage(const char * const *usagestr)
|
||||
{
|
||||
|
3
help.h
3
help.h
@ -55,6 +55,9 @@
|
||||
struct cmd_struct;
|
||||
struct cmd_group;
|
||||
|
||||
__attribute__((noreturn))
|
||||
void usage_unknown_option(const char * const *usagestr, char **argv);
|
||||
|
||||
__attribute__((noreturn))
|
||||
void usage(const char * const *usagestr);
|
||||
void usage_command(const struct cmd_struct *cmd, int full, int err);
|
||||
|
Loading…
Reference in New Issue
Block a user