btrfs-progs: fix show super unknown flag output
[btrfs-progs-unstable/devel.git] / help.c
blobfab942b4018039e39d7d3c5ac8da0367eabcd48c
1 /*
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.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "commands.h"
22 #include "utils.h"
24 static char argv0_buf[ARGV0_BUF_SIZE];
26 #define USAGE_SHORT 1U
27 #define USAGE_LONG 2U
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)
34 int pad = 4;
36 if (!usagestr || !*usagestr)
37 return -1;
39 fprintf(outf, "%s%s\n", (flags & USAGE_LISTING) ? " " : "usage: ",
40 *usagestr++);
42 /* a short one-line description (mandatory) */
43 if ((flags & USAGE_SHORT) == 0)
44 return 0;
45 else if (!*usagestr)
46 return -2;
48 if (flags & USAGE_LISTING)
49 pad = 8;
50 else
51 fputc('\n', outf);
53 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
55 /* a long (possibly multi-line) description (optional) */
56 if (!*usagestr || ((flags & USAGE_LONG) == 0))
57 return 0;
59 if (**usagestr)
60 fputc('\n', outf);
61 while (*usagestr && **usagestr)
62 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
64 /* options (optional) */
65 if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
66 return 0;
69 * options (if present) should always (even if there is no long
70 * description) be prepended with an empty line, skip it
72 usagestr++;
74 fputc('\n', outf);
75 while (*usagestr)
76 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
78 return 0;
81 static int usage_command_internal(const char * const *usagestr,
82 const char *token, int full, int lst,
83 FILE *outf)
85 unsigned int flags = USAGE_SHORT;
86 int ret;
88 if (full)
89 flags |= USAGE_LONG | USAGE_OPTIONS;
90 if (lst)
91 flags |= USAGE_LISTING;
93 ret = do_usage_one_command(usagestr, flags, outf);
94 switch (ret) {
95 case -1:
96 fprintf(outf, "No usage for '%s'\n", token);
97 break;
98 case -2:
99 fprintf(outf, "No short description for '%s'\n", token);
100 break;
103 return ret;
106 static void usage_command_usagestr(const char * const *usagestr,
107 const char *token, int full, int err)
109 FILE *outf = err ? stderr : stdout;
110 int ret;
112 ret = usage_command_internal(usagestr, token, full, 0, outf);
113 if (!ret)
114 fputc('\n', outf);
117 void usage_command(const struct cmd_struct *cmd, int full, int err)
119 usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
122 void usage(const char * const *usagestr)
124 usage_command_usagestr(usagestr, NULL, 1, 1);
125 exit(1);
128 static void usage_command_group_internal(const struct cmd_group *grp, int full,
129 FILE *outf)
131 const struct cmd_struct *cmd = grp->commands;
132 int do_sep = 0;
134 for (; cmd->token; cmd++) {
135 if (cmd->hidden)
136 continue;
138 if (full && cmd != grp->commands)
139 fputc('\n', outf);
141 if (!cmd->next) {
142 if (do_sep) {
143 fputc('\n', outf);
144 do_sep = 0;
147 usage_command_internal(cmd->usagestr, cmd->token, full,
148 1, outf);
149 continue;
152 /* this is an entry point to a nested command group */
154 if (!full && cmd != grp->commands)
155 fputc('\n', outf);
157 usage_command_group_internal(cmd->next, full, outf);
159 if (!full)
160 do_sep = 1;
164 void usage_command_group(const struct cmd_group *grp, int full, int err)
166 const char * const *usagestr = grp->usagestr;
167 FILE *outf = err ? stderr : stdout;
169 if (usagestr && *usagestr) {
170 fprintf(outf, "usage: %s\n", *usagestr++);
171 while (*usagestr)
172 fprintf(outf, " or: %s\n", *usagestr++);
175 fputc('\n', outf);
176 usage_command_group_internal(grp, full, outf);
177 fputc('\n', outf);
179 if (grp->infostr)
180 fprintf(outf, "%s\n", grp->infostr);
183 void help_unknown_token(const char *arg, const struct cmd_group *grp)
185 fprintf(stderr, "%s: unknown token '%s'\n", argv0_buf, arg);
186 usage_command_group(grp, 0, 1);
187 exit(1);
190 void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
192 const struct cmd_struct *cmd = grp->commands;
194 fprintf(stderr, "%s: ambiguous token '%s'\n", argv0_buf, arg);
195 fprintf(stderr, "\nDid you mean one of these ?\n");
197 for (; cmd->token; cmd++) {
198 if (!prefixcmp(cmd->token, arg))
199 fprintf(stderr, "\t%s\n", cmd->token);
202 exit(1);
205 void help_command_group(const struct cmd_group *grp, int argc, char **argv)
207 int full = 0;
209 if (argc > 1) {
210 if (!strcmp(argv[1], "--full"))
211 full = 1;
214 usage_command_group(grp, full, 0);