2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
26 #define USAGE_SHORT 1U
28 #define USAGE_OPTIONS 4U
29 #define USAGE_LISTING 8U
31 static int do_usage_one_command(const char * const *usagestr,
32 unsigned int flags, FILE *outf)
36 if (!usagestr || !*usagestr)
39 fprintf(outf, "%s%s", (flags & USAGE_LISTING) ? " " : "usage: ",
42 /* a short one-line description (mandatory) */
43 if ((flags & USAGE_SHORT) == 0)
49 if (flags & USAGE_LISTING)
54 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
56 /* a long (possibly multi-line) description (optional) */
57 if (!*usagestr || ((flags & USAGE_LONG) == 0))
62 while (*usagestr && **usagestr)
63 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
65 /* options (optional) */
66 if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
70 * options (if present) should always (even if there is no long
71 * description) be prepended with an empty line, skip it
77 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
82 static int usage_command_internal(const char * const *usagestr,
83 const char *token, int full, int lst,
84 int alias, FILE *outf)
86 unsigned int flags = 0;
92 flags |= USAGE_LONG | USAGE_OPTIONS;
94 flags |= USAGE_LISTING;
96 ret = do_usage_one_command(usagestr, flags, outf);
99 fprintf(outf, "No usage for '%s'\n", token);
102 fprintf(outf, "No short description for '%s'\n", token);
109 static void usage_command_usagestr(const char * const *usagestr,
110 const char *token, int full, int err)
112 FILE *outf = err ? stderr : stdout;
115 ret = usage_command_internal(usagestr, token, full, 0, 0, outf);
120 void usage_command(const struct cmd_struct *cmd, int full, int err)
122 usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
125 void usage(const char * const *usagestr)
127 usage_command_usagestr(usagestr, NULL, 1, 1);
131 static void usage_command_group_internal(const struct cmd_group *grp, int full,
134 const struct cmd_struct *cmd = grp->commands;
137 for (; cmd->token; cmd++) {
138 if (cmd->flags & CMD_HIDDEN)
141 if (full && cmd != grp->commands)
150 usage_command_internal(cmd->usagestr, cmd->token, full,
151 1, cmd->flags & CMD_ALIAS, outf);
152 if (cmd->flags & CMD_ALIAS)
157 /* this is an entry point to a nested command group */
159 if (!full && cmd != grp->commands)
162 usage_command_group_internal(cmd->next, full, outf);
169 void usage_command_group_short(const struct cmd_group *grp)
171 const char * const *usagestr = grp->usagestr;
173 const struct cmd_struct *cmd;
175 if (usagestr && *usagestr) {
176 fprintf(outf, "usage: %s\n", *usagestr++);
178 fprintf(outf, " or: %s\n", *usagestr++);
183 fprintf(outf, "Command groups:\n");
184 for (cmd = grp->commands; cmd->token; cmd++) {
185 if (cmd->flags & CMD_HIDDEN)
191 fprintf(outf, " %-16s %s\n", cmd->token, cmd->next->infostr);
194 fprintf(outf, "\nCommands:\n");
195 for (cmd = grp->commands; cmd->token; cmd++) {
196 if (cmd->flags & CMD_HIDDEN)
202 fprintf(outf, " %-16s %s\n", cmd->token, cmd->usagestr[1]);
206 fprintf(stderr, "For an overview of a given command use 'btrfs command --help'\n");
207 fprintf(stderr, "or 'btrfs [command...] --help --full' to print all available options.\n");
208 fprintf(stderr, "Any command name can be shortened as far as it stays unambiguous,\n");
209 fprintf(stderr, "however it is recommended to use full command names in scripts.\n");
210 fprintf(stderr, "All command groups have their manual page named 'btrfs-<group>'.\n");
213 void usage_command_group(const struct cmd_group *grp, int full, int err)
215 const char * const *usagestr = grp->usagestr;
216 FILE *outf = err ? stderr : stdout;
218 if (usagestr && *usagestr) {
219 fprintf(outf, "usage: %s\n", *usagestr++);
221 fprintf(outf, " or: %s\n", *usagestr++);
225 usage_command_group_internal(grp, full, outf);
229 fprintf(outf, "%s\n", grp->infostr);
232 void help_unknown_token(const char *arg, const struct cmd_group *grp)
234 fprintf(stderr, "%s: unknown token '%s'\n", get_argv0_buf(), arg);
235 usage_command_group(grp, 0, 1);
239 void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
241 const struct cmd_struct *cmd = grp->commands;
243 fprintf(stderr, "%s: ambiguous token '%s'\n", get_argv0_buf(), arg);
244 fprintf(stderr, "\nDid you mean one of these ?\n");
246 for (; cmd->token; cmd++) {
247 if (!prefixcmp(cmd->token, arg))
248 fprintf(stderr, "\t%s\n", cmd->token);
254 void help_command_group(const struct cmd_group *grp, int argc, char **argv)
259 if (!strcmp(argv[1], "--full"))
263 usage_command_group(grp, full, 0);
266 int prefixcmp(const char *str, const char *prefix)
268 for (; ; str++, prefix++)
271 else if (*str != *prefix)
272 return (unsigned char)*prefix - (unsigned char)*str;