]> git.ipfire.org Git - thirdparty/util-linux.git/blame - Documentation/boilerplate.c
Merge branch 'PR/dmesg-timestamps' of github.com:karelzak/util-linux-work
[thirdparty/util-linux.git] / Documentation / boilerplate.c
CommitLineData
a97b307a
SK
1/* Please use this file as a template when introducing new command to
2 * util-linux package.
3 * -- remove above */
4/*
38d9e7c8
KZ
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
a97b307a
SK
12 * Copyright (c) 20nn Example Commercial, Inc
13 * Written by Your Name <you@example.com>
14 *
6df1d92b 15 * fixme-command-name - purpose of it
a97b307a 16 */
a97b307a
SK
17#include <getopt.h>
18#include <stdio.h>
19#include <unistd.h>
20
21#include "c.h"
22#include "closestream.h"
23#include "nls.h"
24
610d0fbc
WP
25/*
26 * FIXME: remove this comment.
27 * Other usage() constants that are not demonstrated below:
28 * USAGE_FUNCTIONS USAGE_COMMANDS USAGE_COLUMNS
29 */
86be6a32 30static void __attribute__((__noreturn__)) usage(void)
a97b307a 31{
dc05716f 32 fputs(USAGE_HEADER, stdout);
bad4c729 33 fprintf(stdout, _(" %s [options] file...\n"), program_invocation_short_name);
9e258f38 34
dc05716f 35 fputs(USAGE_SEPARATOR, stdout);
bad4c729 36 fputs(_("Short program description."), stdout);
9e258f38 37
dc05716f 38 fputs(USAGE_OPTIONS, stdout);
bad4c729
MY
39 fputs(_(" -n, --no-argument option does not use argument"), stdout);
40 fputs(_(" --optional[=<arg>] option argument is optional"), stdout);
41 fputs(_(" -r, --required <arg> option requires an argument"), stdout);
42 fputs(_(" -z no long option"), stdout);
43 fputs(_(" --xyzzy a long option only"), stdout);
44 fputs(_(" -e, --extremely-long-long-option\n"
45 " use next line for description when needed"), stdout);
46 fputs(_(" -l, --long-explanation an example of very verbose, and chatty option\n"
47 " description on two, or multiple lines, where the\n"
48 " consecutive lines are intended by two spaces"), stdout);
49 fputs(_(" -f, --foobar next option description resets indent"), stdout);
dc05716f 50 fputs(USAGE_SEPARATOR, stdout);
bad4c729
MY
51 fprintf(stdout, USAGE_HELP_OPTIONS(25)); /* char offset to align option descriptions */
52 fprintf(stdout, USAGE_MAN_TAIL("fixme-command-name(1)"));
86be6a32 53 exit(EXIT_SUCCESS);
a97b307a
SK
54}
55
56int main(int argc, char **argv)
57{
58 int c;
59
60 enum {
326c5c93
SK
61 OPT_XYZZY = CHAR_MAX + 1,
62 OPT_OPTIONAL /* see howto-man-page.txt about short option */
a97b307a
SK
63 };
64 static const struct option longopts[] = {
f636ef1f
SK
65 { "no-argument", no_argument, NULL, 'n' },
66 { "optional", optional_argument, NULL, OPT_OPTIONAL },
67 { "required", required_argument, NULL, 'r' },
68 { "extremely-long-long-option", no_argument, NULL, 'e' },
69 { "xyzzy", no_argument, NULL, OPT_XYZZY },
70 { "long-explanation", no_argument, NULL, 'l' },
71 { "foobar", no_argument, NULL, 'f' },
72 { "version", no_argument, NULL, 'V' },
73 { "help", no_argument, NULL, 'h' },
87918040 74 { NULL, 0, NULL, 0 }
a97b307a
SK
75 };
76
77 setlocale(LC_ALL, "");
78 bindtextdomain(PACKAGE, LOCALEDIR);
79 textdomain(PACKAGE);
2c308875 80 close_stdout_atexit();
a97b307a 81
6cb55736 82 while ((c = getopt_long(argc, argv, "nr:zelfVh", longopts, NULL)) != -1)
a97b307a
SK
83 switch (c) {
84 case 'n':
326c5c93 85 case OPT_OPTIONAL:
a97b307a 86 case 'r':
6cb55736 87 case 'z':
a97b307a
SK
88 case OPT_XYZZY:
89 case 'e':
90 case 'l':
91 case 'f':
92 break;
93 case 'V':
2c308875 94 print_version(EXIT_SUCCESS);
a97b307a 95 case 'h':
86be6a32 96 usage();
a97b307a 97 default:
677ec86c 98 errtryhelp(EXIT_FAILURE);
a97b307a
SK
99 }
100
101 return EXIT_SUCCESS;
102}