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.
25 #define USAGE_SHORT 1U
27 #define USAGE_OPTIONS 4U
28 #define USAGE_LISTING 8U
30 static int do_usage_one_command(const char * const *usagestr
,
31 unsigned int flags
, FILE *outf
)
35 if (!usagestr
|| !*usagestr
)
38 fprintf(outf
, "%s%s", (flags
& USAGE_LISTING
) ? " " : "usage: ",
41 /* a short one-line description (mandatory) */
42 if ((flags
& USAGE_SHORT
) == 0)
48 if (flags
& USAGE_LISTING
)
53 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
55 /* a long (possibly multi-line) description (optional) */
56 if (!*usagestr
|| ((flags
& USAGE_LONG
) == 0))
61 while (*usagestr
&& **usagestr
)
62 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
64 /* options (optional) */
65 if (!*usagestr
|| ((flags
& USAGE_OPTIONS
) == 0))
69 * options (if present) should always (even if there is no long
70 * description) be prepended with an empty line, skip it
76 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
81 static int usage_command_internal(const char * const *usagestr
,
82 const char *token
, int full
, int lst
,
83 int alias
, FILE *outf
)
85 unsigned int flags
= 0;
91 flags
|= USAGE_LONG
| USAGE_OPTIONS
;
93 flags
|= USAGE_LISTING
;
95 ret
= do_usage_one_command(usagestr
, flags
, outf
);
98 fprintf(outf
, "No usage for '%s'\n", token
);
101 fprintf(outf
, "No short description for '%s'\n", token
);
108 static void usage_command_usagestr(const char * const *usagestr
,
109 const char *token
, int full
, int err
)
111 FILE *outf
= err
? stderr
: stdout
;
114 ret
= usage_command_internal(usagestr
, token
, full
, 0, 0, outf
);
119 void usage_command(const struct cmd_struct
*cmd
, int full
, int err
)
121 usage_command_usagestr(cmd
->usagestr
, cmd
->token
, full
, err
);
124 void usage(const char * const *usagestr
)
126 usage_command_usagestr(usagestr
, NULL
, 1, 1);
130 static void usage_command_group_internal(const struct cmd_group
*grp
, int full
,
133 const struct cmd_struct
*cmd
= grp
->commands
;
136 for (; cmd
->token
; cmd
++) {
137 if (cmd
->flags
& CMD_HIDDEN
)
140 if (full
&& cmd
!= grp
->commands
)
149 usage_command_internal(cmd
->usagestr
, cmd
->token
, full
,
150 1, cmd
->flags
& CMD_ALIAS
, outf
);
154 /* this is an entry point to a nested command group */
156 if (!full
&& cmd
!= grp
->commands
)
159 usage_command_group_internal(cmd
->next
, full
, outf
);
166 void usage_command_group_short(const struct cmd_group
*grp
)
168 const char * const *usagestr
= grp
->usagestr
;
170 const struct cmd_struct
*cmd
;
172 if (usagestr
&& *usagestr
) {
173 fprintf(outf
, "usage: %s\n", *usagestr
++);
175 fprintf(outf
, " or: %s\n", *usagestr
++);
180 fprintf(outf
, "Command groups:\n");
181 for (cmd
= grp
->commands
; cmd
->token
; cmd
++) {
182 if (cmd
->flags
& CMD_HIDDEN
)
188 fprintf(outf
, " %-16s %s\n", cmd
->token
, cmd
->next
->infostr
);
191 fprintf(outf
, "\nCommands:\n");
192 for (cmd
= grp
->commands
; cmd
->token
; cmd
++) {
193 if (cmd
->flags
& CMD_HIDDEN
)
199 fprintf(outf
, " %-16s %s\n", cmd
->token
, cmd
->usagestr
[1]);
203 fprintf(stderr
, "For an overview of a given command use 'btrfs command --help'\n");
204 fprintf(stderr
, "or 'btrfs [command...] --help --full' to print all available options.\n");
205 fprintf(stderr
, "Any command name can be shortened as far as it stays unambiguous,\n");
206 fprintf(stderr
, "however it is recommended to use full command names in scripts.\n");
207 fprintf(stderr
, "All command groups have their manual page named 'btrfs-<group>'.\n");
210 void usage_command_group(const struct cmd_group
*grp
, int full
, int err
)
212 const char * const *usagestr
= grp
->usagestr
;
213 FILE *outf
= err
? stderr
: stdout
;
215 if (usagestr
&& *usagestr
) {
216 fprintf(outf
, "usage: %s\n", *usagestr
++);
218 fprintf(outf
, " or: %s\n", *usagestr
++);
222 usage_command_group_internal(grp
, full
, outf
);
226 fprintf(outf
, "%s\n", grp
->infostr
);
229 void help_unknown_token(const char *arg
, const struct cmd_group
*grp
)
231 fprintf(stderr
, "%s: unknown token '%s'\n", get_argv0_buf(), arg
);
232 usage_command_group(grp
, 0, 1);
236 void help_ambiguous_token(const char *arg
, const struct cmd_group
*grp
)
238 const struct cmd_struct
*cmd
= grp
->commands
;
240 fprintf(stderr
, "%s: ambiguous token '%s'\n", get_argv0_buf(), arg
);
241 fprintf(stderr
, "\nDid you mean one of these ?\n");
243 for (; cmd
->token
; cmd
++) {
244 if (!prefixcmp(cmd
->token
, arg
))
245 fprintf(stderr
, "\t%s\n", cmd
->token
);
251 void help_command_group(const struct cmd_group
*grp
, int argc
, char **argv
)
256 if (!strcmp(argv
[1], "--full"))
260 usage_command_group(grp
, full
, 0);
263 int prefixcmp(const char *str
, const char *prefix
)
265 for (; ; str
++, prefix
++)
268 else if (*str
!= *prefix
)
269 return (unsigned char)*prefix
- (unsigned char)*str
;