]> git.ipfire.org Git - thirdparty/util-linux.git/blob - Documentation/boilerplate.c
Use --help suggestion on invalid option
[thirdparty/util-linux.git] / Documentation / boilerplate.c
1 /* Please use this file as a template when introducing new command to
2 * util-linux package.
3 * -- remove above */
4 /*
5 * fixme-command-name - purpose of it
6 *
7 * Copyright (c) 20nn Example Commercial, Inc
8 * Written by Your Name <you@example.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it would be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <unistd.h>
27
28 #include "c.h"
29 #include "closestream.h"
30 #include "nls.h"
31
32 static void __attribute__((__noreturn__)) usage(FILE *out)
33 {
34 fputs(USAGE_HEADER, out);
35 fprintf(out, _(" %s [options] file...\n"), program_invocation_short_name);
36 fputs(USAGE_OPTIONS, out);
37 fputs(_(" -n, --no-argument option does not use argument\n"), out);
38 fputs(_(" --optional[=<arg>] option argument is optional\n"), out);
39 fputs(_(" -r, --required <arg> option requires an argument\n"), out);
40 fputs(_(" -z no long option\n"), out);
41 fputs(_(" --xyzzy a long option only\n"), out);
42 fputs(_(" -e, --extremely-long-long-option\n"), out);
43 fputs(_(" use next line for description when needed\n"), out);
44 fputs(_(" -l, --long-explanation an example of very verbose, and chatty option\n"), out);
45 fputs(_(" description on two, or multiple lines, where the\n"), out);
46 fputs(_(" consecutive lines are intended by two spaces\n"), out);
47 fputs(_(" -f, --foobar next option description resets indent\n"), out);
48 fputs(USAGE_SEPARATOR, out);
49 fputs(USAGE_HELP, out);
50 fputs(USAGE_VERSION, out);
51 fprintf(out, USAGE_MAN_TAIL("fixme-command-name(1)"));
52 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
53 }
54
55 int main(int argc, char **argv)
56 {
57 int c;
58
59 enum {
60 OPT_XYZZY = CHAR_MAX + 1,
61 OPT_OPTIONAL /* see howto-man-page.txt about short option */
62 };
63 static const struct option longopts[] = {
64 {"no-argument", no_argument, NULL, 'n'},
65 {"required", required_argument, NULL, 'r'},
66 {"xyzzy", no_argument, NULL, OPT_XYZZY},
67 {"extremely-long-long-option", no_argument, NULL, 'e'},
68 {"long-explanation", no_argument, NULL, 'l'},
69 {"foobar", no_argument, NULL, 'f'},
70 {"version", no_argument, NULL, 'V'},
71 {"help", no_argument, NULL, 'h'},
72 {NULL, 0, NULL, 0}
73 };
74
75 setlocale(LC_ALL, "");
76 bindtextdomain(PACKAGE, LOCALEDIR);
77 textdomain(PACKAGE);
78 atexit(close_stdout);
79
80 while ((c = getopt_long(argc, argv, "nr:elfVh", longopts, NULL)) != -1)
81 switch (c) {
82 case 'n':
83 case OPT_OPTIONAL:
84 case 'r':
85 case OPT_XYZZY:
86 case 'e':
87 case 'l':
88 case 'f':
89 break;
90 case 'V':
91 printf(UTIL_LINUX_VERSION);
92 return EXIT_SUCCESS;
93 case 'h':
94 usage(stdout);
95 default:
96 errtryhelp(EXIT_FAILURE);
97 }
98
99 return EXIT_SUCCESS;
100 }