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.
27 #define USAGE_SHORT 1U
29 #define USAGE_OPTIONS 4U
30 #define USAGE_LISTING 8U
32 static char argv0_buf
[ARGV0_BUF_SIZE
] = "btrfs";
34 const char *get_argv0_buf(void)
39 void fixup_argv0(char **argv
, const char *token
)
41 int len
= strlen(argv0_buf
);
43 snprintf(argv0_buf
+ len
, sizeof(argv0_buf
) - len
, " %s", token
);
47 void set_argv0(char **argv
)
49 strncpy(argv0_buf
, argv
[0], sizeof(argv0_buf
));
50 argv0_buf
[sizeof(argv0_buf
) - 1] = 0;
53 int check_argc_exact(int nargs
, int expected
)
56 fprintf(stderr
, "%s: too few arguments\n", argv0_buf
);
58 fprintf(stderr
, "%s: too many arguments\n", argv0_buf
);
60 return nargs
!= expected
;
63 int check_argc_min(int nargs
, int expected
)
65 if (nargs
< expected
) {
66 fprintf(stderr
, "%s: too few arguments\n", argv0_buf
);
73 int check_argc_max(int nargs
, int expected
)
75 if (nargs
> expected
) {
76 fprintf(stderr
, "%s: too many arguments\n", argv0_buf
);
84 * Preprocess @argv with getopt_long to reorder options and consume the "--"
86 * Unknown short and long options are reported, optionally the @usage is printed
89 void clean_args_no_options(int argc
, char *argv
[], const char * const *usagestr
)
91 static const struct option long_options
[] = {
96 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
110 * Same as clean_args_no_options but pass through arguments that could look
111 * like short options. Eg. reisze which takes a negative resize argument like
114 * This accepts only two forms:
115 * - "-- option1 option2 ..."
116 * - "option1 option2 ..."
118 void clean_args_no_options_relaxed(int argc
, char *argv
[], const char * const *usagestr
)
123 if (strcmp(argv
[1], "--") == 0)
127 static int do_usage_one_command(const char * const *usagestr
,
128 unsigned int flags
, FILE *outf
)
132 if (!usagestr
|| !*usagestr
)
135 fprintf(outf
, "%s%s", (flags
& USAGE_LISTING
) ? " " : "usage: ",
138 /* a short one-line description (mandatory) */
139 if ((flags
& USAGE_SHORT
) == 0)
145 if (flags
& USAGE_LISTING
)
150 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
152 /* a long (possibly multi-line) description (optional) */
153 if (!*usagestr
|| ((flags
& USAGE_LONG
) == 0))
158 while (*usagestr
&& **usagestr
)
159 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
161 /* options (optional) */
162 if (!*usagestr
|| ((flags
& USAGE_OPTIONS
) == 0))
166 * options (if present) should always (even if there is no long
167 * description) be prepended with an empty line, skip it
173 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
178 static int usage_command_internal(const char * const *usagestr
,
179 const char *token
, int full
, int lst
,
180 int alias
, FILE *outf
)
182 unsigned int flags
= 0;
186 flags
|= USAGE_SHORT
;
188 flags
|= USAGE_LONG
| USAGE_OPTIONS
;
190 flags
|= USAGE_LISTING
;
192 ret
= do_usage_one_command(usagestr
, flags
, outf
);
195 fprintf(outf
, "No usage for '%s'\n", token
);
198 fprintf(outf
, "No short description for '%s'\n", token
);
205 static void usage_command_usagestr(const char * const *usagestr
,
206 const char *token
, int full
, int err
)
208 FILE *outf
= err
? stderr
: stdout
;
211 ret
= usage_command_internal(usagestr
, token
, full
, 0, 0, outf
);
216 void usage_command(const struct cmd_struct
*cmd
, int full
, int err
)
218 usage_command_usagestr(cmd
->usagestr
, cmd
->token
, full
, err
);
221 void usage(const char * const *usagestr
)
223 usage_command_usagestr(usagestr
, NULL
, 1, 1);
227 static void usage_command_group_internal(const struct cmd_group
*grp
, int full
,
230 const struct cmd_struct
*cmd
= grp
->commands
;
233 for (; cmd
->token
; cmd
++) {
234 if (cmd
->flags
& CMD_HIDDEN
)
237 if (full
&& cmd
!= grp
->commands
)
246 usage_command_internal(cmd
->usagestr
, cmd
->token
, full
,
247 1, cmd
->flags
& CMD_ALIAS
, outf
);
248 if (cmd
->flags
& CMD_ALIAS
)
253 /* this is an entry point to a nested command group */
255 if (!full
&& cmd
!= grp
->commands
)
258 usage_command_group_internal(cmd
->next
, full
, outf
);
265 void usage_command_group_short(const struct cmd_group
*grp
)
267 const char * const *usagestr
= grp
->usagestr
;
269 const struct cmd_struct
*cmd
;
271 if (usagestr
&& *usagestr
) {
272 fprintf(outf
, "usage: %s\n", *usagestr
++);
274 fprintf(outf
, " or: %s\n", *usagestr
++);
279 fprintf(outf
, "Command groups:\n");
280 for (cmd
= grp
->commands
; cmd
->token
; cmd
++) {
281 if (cmd
->flags
& CMD_HIDDEN
)
287 fprintf(outf
, " %-16s %s\n", cmd
->token
, cmd
->next
->infostr
);
290 fprintf(outf
, "\nCommands:\n");
291 for (cmd
= grp
->commands
; cmd
->token
; cmd
++) {
292 if (cmd
->flags
& CMD_HIDDEN
)
298 fprintf(outf
, " %-16s %s\n", cmd
->token
, cmd
->usagestr
[1]);
302 fprintf(stderr
, "For an overview of a given command use 'btrfs command --help'\n");
303 fprintf(stderr
, "or 'btrfs [command...] --help --full' to print all available options.\n");
304 fprintf(stderr
, "Any command name can be shortened as far as it stays unambiguous,\n");
305 fprintf(stderr
, "however it is recommended to use full command names in scripts.\n");
306 fprintf(stderr
, "All command groups have their manual page named 'btrfs-<group>'.\n");
309 void usage_command_group(const struct cmd_group
*grp
, int full
, int err
)
311 const char * const *usagestr
= grp
->usagestr
;
312 FILE *outf
= err
? stderr
: stdout
;
314 if (usagestr
&& *usagestr
) {
315 fprintf(outf
, "usage: %s\n", *usagestr
++);
317 fprintf(outf
, " or: %s\n", *usagestr
++);
321 usage_command_group_internal(grp
, full
, outf
);
325 fprintf(outf
, "%s\n", grp
->infostr
);
328 void help_unknown_token(const char *arg
, const struct cmd_group
*grp
)
330 fprintf(stderr
, "%s: unknown token '%s'\n", get_argv0_buf(), arg
);
331 usage_command_group(grp
, 0, 1);
335 void help_ambiguous_token(const char *arg
, const struct cmd_group
*grp
)
337 const struct cmd_struct
*cmd
= grp
->commands
;
339 fprintf(stderr
, "%s: ambiguous token '%s'\n", get_argv0_buf(), arg
);
340 fprintf(stderr
, "\nDid you mean one of these ?\n");
342 for (; cmd
->token
; cmd
++) {
343 if (!prefixcmp(cmd
->token
, arg
))
344 fprintf(stderr
, "\t%s\n", cmd
->token
);
350 void help_command_group(const struct cmd_group
*grp
, int argc
, char **argv
)
355 if (!strcmp(argv
[1], "--full"))
359 usage_command_group(grp
, full
, 0);