2012-02-03 19:00:17 +00:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "kerncompat.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-01-25 15:35:00 +00:00
|
|
|
#include <getopt.h>
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/help.h"
|
2022-09-15 21:15:17 +00:00
|
|
|
#include "common/string-utils.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "cmds/commands.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
#define USAGE_SHORT 1U
|
|
|
|
#define USAGE_LONG 2U
|
|
|
|
#define USAGE_OPTIONS 4U
|
|
|
|
#define USAGE_LISTING 8U
|
2018-03-06 02:29:30 +00:00
|
|
|
#define USAGE_FORMAT 16U
|
|
|
|
|
2019-06-21 14:14:09 +00:00
|
|
|
const struct format_desc output_formats[2] = {
|
2018-03-06 02:29:30 +00:00
|
|
|
{ .value = CMD_FORMAT_TEXT, .name = "text" },
|
2019-06-21 14:14:09 +00:00
|
|
|
{ .value = CMD_FORMAT_JSON, .name = "json" },
|
2018-03-06 02:29:30 +00:00
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2017-01-25 15:35:00 +00:00
|
|
|
static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
|
|
|
|
|
|
|
|
void fixup_argv0(char **argv, const char *token)
|
|
|
|
{
|
|
|
|
int len = strlen(argv0_buf);
|
|
|
|
|
|
|
|
snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
|
|
|
|
argv[0] = argv0_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_argv0(char **argv)
|
|
|
|
{
|
|
|
|
strncpy(argv0_buf, argv[0], sizeof(argv0_buf));
|
|
|
|
argv0_buf[sizeof(argv0_buf) - 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_argc_exact(int nargs, int expected)
|
|
|
|
{
|
2019-03-01 19:57:05 +00:00
|
|
|
if (nargs != expected)
|
|
|
|
fprintf(stderr, "%s: exactly %d argument%s expected, %d given\n",
|
|
|
|
argv0_buf, expected,
|
|
|
|
expected > 1 ? "s" : "", nargs);
|
2017-01-25 15:35:00 +00:00
|
|
|
|
|
|
|
return nargs != expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_argc_min(int nargs, int expected)
|
|
|
|
{
|
|
|
|
if (nargs < expected) {
|
2019-03-01 19:57:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: not enough arguments: %d but at least %d expected\n",
|
|
|
|
argv0_buf, nargs, expected);
|
2017-01-25 15:35:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_argc_max(int nargs, int expected)
|
|
|
|
{
|
|
|
|
if (nargs > expected) {
|
2019-03-01 19:57:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: too many arguments: %d but at most %d expected\n",
|
|
|
|
argv0_buf, nargs, expected);
|
2017-01-25 15:35:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-03-07 02:42:01 +00:00
|
|
|
void clean_args_no_options(const struct cmd_struct *cmd, int argc, char *argv[])
|
2017-01-25 15:35:00 +00:00
|
|
|
{
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int c = getopt_long(argc, argv, "", long_options, NULL);
|
|
|
|
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
default:
|
2018-03-07 02:42:01 +00:00
|
|
|
if (cmd->usagestr)
|
2023-02-13 05:26:32 +00:00
|
|
|
usage(cmd, 1);
|
2017-01-25 15:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same as clean_args_no_options but pass through arguments that could look
|
2018-11-26 17:01:42 +00:00
|
|
|
* like short options. Eg. resize which takes a negative resize argument like
|
2017-01-25 15:35:00 +00:00
|
|
|
* '-123M' .
|
|
|
|
*
|
|
|
|
* This accepts only two forms:
|
|
|
|
* - "-- option1 option2 ..."
|
|
|
|
* - "option1 option2 ..."
|
|
|
|
*/
|
2018-03-07 02:42:01 +00:00
|
|
|
void clean_args_no_options_relaxed(const struct cmd_struct *cmd,
|
|
|
|
int argc, char *argv[])
|
2017-01-25 15:35:00 +00:00
|
|
|
{
|
|
|
|
if (argc <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "--") == 0)
|
|
|
|
optind = 2;
|
|
|
|
}
|
|
|
|
|
2018-03-06 02:29:30 +00:00
|
|
|
const char *output_format_name(unsigned int value)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(output_formats); i++) {
|
|
|
|
if (output_formats[i].value == value)
|
|
|
|
return output_formats[i].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2023-01-11 17:06:41 +00:00
|
|
|
static void hpad(int len, FILE *outf)
|
|
|
|
{
|
|
|
|
while (len-- > 0)
|
|
|
|
fputc(' ', outf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void format_text(const char *line, FILE *outf)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (*line) {
|
|
|
|
while (*line && *line == ' ')
|
|
|
|
line++;
|
|
|
|
while (*line && *line != ' ') {
|
|
|
|
fputc(*line, outf);
|
|
|
|
line++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i > HELPINFO_DESC_WIDTH) {
|
|
|
|
if (*line) {
|
|
|
|
fputc('\n', outf);
|
|
|
|
line++;
|
|
|
|
hpad(HELPINFO_DESC_PREFIX, outf);
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
hpad(1, outf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
static int do_usage_one_command(const char * const *usagestr,
|
2018-03-06 02:29:30 +00:00
|
|
|
unsigned int flags, unsigned int cmd_flags,
|
|
|
|
FILE *outf)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2023-01-12 21:18:50 +00:00
|
|
|
int pad = HELPINFO_PREFIX_WIDTH;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
if (!usagestr || !*usagestr)
|
|
|
|
return -1;
|
|
|
|
|
2017-10-17 17:39:47 +00:00
|
|
|
if (flags & USAGE_LISTING)
|
2023-01-12 21:18:50 +00:00
|
|
|
hpad(HELPINFO_PREFIX_WIDTH, outf);
|
|
|
|
else
|
|
|
|
fputs("usage: ", outf);
|
2017-10-17 17:39:47 +00:00
|
|
|
|
|
|
|
if (strchr(*usagestr, '\n') == NULL) {
|
|
|
|
fputs(*usagestr, outf);
|
|
|
|
} else {
|
|
|
|
const char *c = *usagestr;
|
|
|
|
|
|
|
|
for (c = *usagestr; *c; c++) {
|
|
|
|
fputc(*c, outf);
|
2023-01-12 21:18:50 +00:00
|
|
|
if (*c == '\n') {
|
|
|
|
if (flags & USAGE_LISTING)
|
|
|
|
hpad(HELPINFO_PREFIX_WIDTH, outf);
|
|
|
|
else
|
|
|
|
hpad(HELPINFO_LISTING_WIDTH, outf);
|
|
|
|
}
|
2017-10-17 17:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
usagestr++;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
/* a short one-line description (mandatory) */
|
|
|
|
if ((flags & USAGE_SHORT) == 0)
|
|
|
|
return 0;
|
|
|
|
else if (!*usagestr)
|
|
|
|
return -2;
|
2015-06-26 14:44:29 +00:00
|
|
|
fputc('\n', outf);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
if (flags & USAGE_LISTING)
|
2023-01-12 21:18:50 +00:00
|
|
|
pad = HELPINFO_LISTING_WIDTH;
|
2012-02-03 19:00:17 +00:00
|
|
|
else
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
2023-01-12 21:18:50 +00:00
|
|
|
hpad(pad, outf);
|
|
|
|
fprintf(outf, "%s\n", *usagestr++);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
/* a long (possibly multi-line) description (optional) */
|
|
|
|
if (!*usagestr || ((flags & USAGE_LONG) == 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (**usagestr)
|
|
|
|
fputc('\n', outf);
|
2023-01-12 21:18:50 +00:00
|
|
|
while (*usagestr && **usagestr) {
|
|
|
|
hpad(pad, outf);
|
|
|
|
fprintf(outf, "%s\n", *usagestr++);
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
/* options (optional) */
|
|
|
|
if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* options (if present) should always (even if there is no long
|
|
|
|
* description) be prepended with an empty line, skip it
|
|
|
|
*/
|
|
|
|
usagestr++;
|
|
|
|
fputc('\n', outf);
|
2019-06-21 14:11:07 +00:00
|
|
|
|
|
|
|
while (*usagestr) {
|
2019-11-25 10:39:02 +00:00
|
|
|
if (strcmp(*usagestr, HELPINFO_INSERT_FORMAT) == 0) {
|
2019-06-21 14:11:07 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We always support text, that's on by default for all
|
|
|
|
* commands
|
|
|
|
*/
|
2023-01-12 21:18:50 +00:00
|
|
|
hpad(pad, outf);
|
2023-02-22 00:37:18 +00:00
|
|
|
fprintf(outf, "%-*s where TYPE is: %s",
|
|
|
|
HELPINFO_OPTION_WIDTH,
|
|
|
|
"--format TYPE",
|
2023-01-12 21:18:50 +00:00
|
|
|
output_formats[0].name);
|
2019-06-21 14:11:07 +00:00
|
|
|
for (i = 1; i < ARRAY_SIZE(output_formats); i++) {
|
|
|
|
if (cmd_flags & output_formats[i].value)
|
|
|
|
fprintf(outf, ", %s",
|
|
|
|
output_formats[i].name);
|
|
|
|
}
|
|
|
|
fputc('\n', outf);
|
2023-01-11 17:06:41 +00:00
|
|
|
} else if (*usagestr[0] == HELPINFO_OPTION[0]) {
|
|
|
|
const char *tmp = *usagestr + 1;
|
|
|
|
const char *text_marker = strchr(*usagestr, HELPINFO_DESC[0]);
|
|
|
|
const char *text = text_marker + 1;
|
|
|
|
int optlen = (int)(text_marker - tmp - 1);
|
|
|
|
|
|
|
|
hpad(HELPINFO_PREFIX_WIDTH, outf);
|
|
|
|
while (tmp < text_marker)
|
|
|
|
fputc(*tmp++, outf);
|
|
|
|
|
|
|
|
if (optlen > HELPINFO_OPTION_WIDTH) {
|
|
|
|
fputc('\n', outf);
|
|
|
|
hpad(HELPINFO_DESC_PREFIX, outf);
|
|
|
|
} else {
|
|
|
|
hpad(HELPINFO_OPTION_WIDTH + HELPINFO_OPTION_MARGIN - optlen - 1,
|
|
|
|
outf);
|
|
|
|
}
|
|
|
|
format_text(text, outf);
|
|
|
|
fputc('\n', outf);
|
2019-06-21 14:11:07 +00:00
|
|
|
} else {
|
2023-01-12 21:18:50 +00:00
|
|
|
hpad(pad, outf);
|
|
|
|
fprintf(outf, "%s\n", *usagestr);
|
2019-06-21 14:11:07 +00:00
|
|
|
}
|
|
|
|
usagestr++;
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usage_command_internal(const char * const *usagestr,
|
2018-03-06 02:29:30 +00:00
|
|
|
const char *token, unsigned cmd_flags,
|
|
|
|
bool full,
|
|
|
|
bool lst, bool alias, FILE *outf)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2015-06-24 16:09:17 +00:00
|
|
|
unsigned int flags = 0;
|
2012-02-03 19:00:17 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-06-24 16:09:17 +00:00
|
|
|
if (!alias)
|
|
|
|
flags |= USAGE_SHORT;
|
2012-02-03 19:00:17 +00:00
|
|
|
if (full)
|
2018-03-06 02:29:30 +00:00
|
|
|
flags |= USAGE_LONG | USAGE_OPTIONS | USAGE_FORMAT;
|
2012-02-03 19:00:17 +00:00
|
|
|
if (lst)
|
|
|
|
flags |= USAGE_LISTING;
|
|
|
|
|
2018-03-06 02:29:30 +00:00
|
|
|
ret = do_usage_one_command(usagestr, flags, cmd_flags, outf);
|
2012-02-03 19:00:17 +00:00
|
|
|
switch (ret) {
|
|
|
|
case -1:
|
|
|
|
fprintf(outf, "No usage for '%s'\n", token);
|
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
fprintf(outf, "No short description for '%s'\n", token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage_command_usagestr(const char * const *usagestr,
|
2018-03-06 02:29:30 +00:00
|
|
|
const char *token, unsigned int flags,
|
|
|
|
bool full, bool err)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
|
|
|
FILE *outf = err ? stderr : stdout;
|
|
|
|
int ret;
|
|
|
|
|
2018-03-06 02:29:30 +00:00
|
|
|
ret = usage_command_internal(usagestr, token, flags,
|
|
|
|
full, false, false, outf);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (!ret)
|
|
|
|
fputc('\n', outf);
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:49:00 +00:00
|
|
|
void usage_command(const struct cmd_struct *cmd, bool full, bool err)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2018-03-06 02:29:30 +00:00
|
|
|
usage_command_usagestr(cmd->usagestr, cmd->token,
|
|
|
|
cmd->flags, full, err);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 19:47:00 +00:00
|
|
|
__attribute__((noreturn))
|
2018-03-07 02:54:38 +00:00
|
|
|
void usage_unknown_option(const struct cmd_struct *cmd, char **argv)
|
2019-03-01 19:47:00 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int c;
|
|
|
|
int prev = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Guess the command prefix, until the first option or argument
|
|
|
|
* specifier
|
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
do {
|
2018-03-07 02:54:38 +00:00
|
|
|
c = cmd->usagestr[0][i];
|
2019-03-01 19:47:00 +00:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2018-03-07 02:54:38 +00:00
|
|
|
fprintf(stderr, "%.*s: ", i, cmd->usagestr[0]);
|
2019-03-01 19:47:00 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-03-07 02:54:38 +00:00
|
|
|
fprintf(stderr, "Try '%.*s --help' for more information\n", i,
|
|
|
|
cmd->usagestr[0]);
|
2019-03-01 19:47:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-09-23 19:08:43 +00:00
|
|
|
__attribute__((noreturn))
|
2023-02-13 05:26:32 +00:00
|
|
|
void usage(const struct cmd_struct *cmd, int error)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2018-03-06 02:29:30 +00:00
|
|
|
usage_command_usagestr(cmd->usagestr, NULL, 0, true, true);
|
2023-02-13 05:26:32 +00:00
|
|
|
exit(error);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 21:49:00 +00:00
|
|
|
static void usage_command_group_internal(const struct cmd_group *grp, bool full,
|
2012-02-03 19:00:17 +00:00
|
|
|
FILE *outf)
|
|
|
|
{
|
2018-03-07 02:07:03 +00:00
|
|
|
int i;
|
2012-02-03 19:00:17 +00:00
|
|
|
int do_sep = 0;
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
for (i = 0; grp->commands[i]; i++) {
|
|
|
|
const struct cmd_struct *cmd = grp->commands[i];
|
2015-06-24 16:09:16 +00:00
|
|
|
if (cmd->flags & CMD_HIDDEN)
|
2012-02-03 19:00:17 +00:00
|
|
|
continue;
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
if (full && i)
|
2012-02-03 19:00:17 +00:00
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
if (!cmd->next) {
|
|
|
|
if (do_sep) {
|
|
|
|
fputc('\n', outf);
|
|
|
|
do_sep = 0;
|
|
|
|
}
|
|
|
|
|
2018-03-06 02:29:30 +00:00
|
|
|
usage_command_internal(cmd->usagestr, cmd->token,
|
|
|
|
cmd->flags, full,
|
2018-03-05 21:49:00 +00:00
|
|
|
true, cmd->flags & CMD_ALIAS,
|
|
|
|
outf);
|
2016-12-08 18:27:50 +00:00
|
|
|
if (cmd->flags & CMD_ALIAS)
|
|
|
|
putchar('\n');
|
2012-02-03 19:00:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is an entry point to a nested command group */
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
if (!full && i)
|
2012-02-03 19:00:17 +00:00
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
usage_command_group_internal(cmd->next, full, outf);
|
|
|
|
|
|
|
|
if (!full)
|
|
|
|
do_sep = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 16:54:04 +00:00
|
|
|
void usage_command_group_short(const struct cmd_group *grp)
|
|
|
|
{
|
|
|
|
const char * const *usagestr = grp->usagestr;
|
|
|
|
FILE *outf = stdout;
|
|
|
|
const struct cmd_struct *cmd;
|
2018-03-07 02:07:03 +00:00
|
|
|
int i;
|
2015-06-08 16:54:04 +00:00
|
|
|
|
|
|
|
if (usagestr && *usagestr) {
|
|
|
|
fprintf(outf, "usage: %s\n", *usagestr++);
|
|
|
|
while (*usagestr)
|
|
|
|
fprintf(outf, " or: %s\n", *usagestr++);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
fprintf(outf, "Command groups:\n");
|
2018-03-07 02:07:03 +00:00
|
|
|
for (i = 0; grp->commands[i]; i++) {
|
|
|
|
cmd = grp->commands[i];
|
2015-06-24 16:09:16 +00:00
|
|
|
if (cmd->flags & CMD_HIDDEN)
|
2015-06-08 16:54:04 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!cmd->next)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fprintf(outf, " %-16s %s\n", cmd->token, cmd->next->infostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(outf, "\nCommands:\n");
|
2018-03-07 02:07:03 +00:00
|
|
|
for (i = 0; grp->commands[i]; i++) {
|
|
|
|
cmd = grp->commands[i];
|
2015-06-24 16:09:16 +00:00
|
|
|
if (cmd->flags & CMD_HIDDEN)
|
2015-06-08 16:54:04 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cmd->next)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fprintf(outf, " %-16s %s\n", cmd->token, cmd->usagestr[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', outf);
|
|
|
|
fprintf(stderr, "For an overview of a given command use 'btrfs command --help'\n");
|
|
|
|
fprintf(stderr, "or 'btrfs [command...] --help --full' to print all available options.\n");
|
|
|
|
fprintf(stderr, "Any command name can be shortened as far as it stays unambiguous,\n");
|
|
|
|
fprintf(stderr, "however it is recommended to use full command names in scripts.\n");
|
|
|
|
fprintf(stderr, "All command groups have their manual page named 'btrfs-<group>'.\n");
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:49:00 +00:00
|
|
|
void usage_command_group(const struct cmd_group *grp, bool full, bool err)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2012-02-08 15:45:54 +00:00
|
|
|
const char * const *usagestr = grp->usagestr;
|
2012-02-03 19:00:17 +00:00
|
|
|
FILE *outf = err ? stderr : stdout;
|
|
|
|
|
2012-02-08 15:45:54 +00:00
|
|
|
if (usagestr && *usagestr) {
|
|
|
|
fprintf(outf, "usage: %s\n", *usagestr++);
|
|
|
|
while (*usagestr)
|
|
|
|
fprintf(outf, " or: %s\n", *usagestr++);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', outf);
|
2012-02-03 19:00:17 +00:00
|
|
|
usage_command_group_internal(grp, full, outf);
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
if (grp->infostr)
|
|
|
|
fprintf(outf, "%s\n", grp->infostr);
|
|
|
|
}
|
|
|
|
|
2017-09-23 19:08:43 +00:00
|
|
|
__attribute__((noreturn))
|
2012-02-03 19:00:17 +00:00
|
|
|
void help_unknown_token(const char *arg, const struct cmd_group *grp)
|
|
|
|
{
|
2019-03-11 10:20:56 +00:00
|
|
|
fprintf(stderr, "%s: unknown token '%s'\n", argv0_buf, arg);
|
2018-03-05 21:49:00 +00:00
|
|
|
usage_command_group(grp, false, true);
|
2012-02-03 19:00:17 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-09-23 19:08:43 +00:00
|
|
|
__attribute__((noreturn))
|
2012-02-03 19:00:17 +00:00
|
|
|
void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
|
|
|
|
{
|
2018-03-07 02:07:03 +00:00
|
|
|
int i;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2019-03-11 10:20:56 +00:00
|
|
|
fprintf(stderr, "%s: ambiguous token '%s'\n", argv0_buf, arg);
|
2012-02-03 19:00:17 +00:00
|
|
|
fprintf(stderr, "\nDid you mean one of these ?\n");
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
for (i = 0; grp->commands[i]; i++) {
|
|
|
|
const struct cmd_struct *cmd = grp->commands[i];
|
2012-02-03 19:00:17 +00:00
|
|
|
if (!prefixcmp(cmd->token, arg))
|
|
|
|
fprintf(stderr, "\t%s\n", cmd->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void help_command_group(const struct cmd_group *grp, int argc, char **argv)
|
|
|
|
{
|
2018-03-05 21:49:00 +00:00
|
|
|
bool full = false;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
if (!strcmp(argv[1], "--full"))
|
|
|
|
full = 1;
|
2015-06-21 16:23:19 +00:00
|
|
|
/* The option --box is handled in the caller */
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 21:49:00 +00:00
|
|
|
usage_command_group(grp, full, false);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2016-01-14 09:29:13 +00:00
|
|
|
|