(root)/
util-linux-2.39/
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  /*
      33   * FIXME: remove this comment.
      34   * Other usage() constants that are not demonstrated below:
      35   * USAGE_FUNCTIONS USAGE_COMMANDS USAGE_COLUMNS
      36   */
      37  static void __attribute__((__noreturn__)) usage(void)
      38  {
      39  	fputs(USAGE_HEADER, stdout);
      40  	printf(_(" %s [options] file...\n"), program_invocation_short_name);
      41  
      42  	fputs(USAGE_SEPARATOR, stdout);
      43  	puts(_("Short program description."));
      44  
      45  	fputs(USAGE_OPTIONS, stdout);
      46  	puts(_(" -n, --no-argument       option does not use argument"));
      47  	puts(_("     --optional[=<arg>]  option argument is optional"));
      48  	puts(_(" -r, --required <arg>    option requires an argument"));
      49  	puts(_(" -z                      no long option"));
      50  	puts(_("     --xyzzy             a long option only"));
      51  	puts(_(" -e, --extremely-long-long-option\n"
      52  	       "                         use next line for description when needed"));
      53  	puts(_(" -l, --long-explanation  an example of very verbose, and chatty option\n"
      54  	       "                           description on two, or multiple lines, where the\n"
      55  	       "                           consecutive lines are intended by two spaces"));
      56  	puts(_(" -f, --foobar            next option description resets indent"));
      57  	fputs(USAGE_SEPARATOR, stdout);
      58  	printf(USAGE_HELP_OPTIONS(25)); /* char offset to align option descriptions */
      59  	printf(USAGE_MAN_TAIL("fixme-command-name(1)"));
      60  	exit(EXIT_SUCCESS);
      61  }
      62  
      63  int main(int argc, char **argv)
      64  {
      65  	int c;
      66  
      67  	enum {
      68  		OPT_XYZZY = CHAR_MAX + 1,
      69  		OPT_OPTIONAL	/* see howto-man-page.txt about short option */
      70  	};
      71  	static const struct option longopts[] = {
      72  		{ "no-argument",                no_argument,       NULL, 'n'          },
      73  		{ "optional",                   optional_argument, NULL, OPT_OPTIONAL },
      74  		{ "required",                   required_argument, NULL, 'r'          },
      75  		{ "extremely-long-long-option", no_argument,       NULL, 'e'          },
      76  		{ "xyzzy",                      no_argument,       NULL, OPT_XYZZY    },
      77  		{ "long-explanation",           no_argument,       NULL, 'l'          },
      78  		{ "foobar",                     no_argument,       NULL, 'f'          },
      79  		{ "version",                    no_argument,       NULL, 'V'          },
      80  		{ "help",                       no_argument,       NULL, 'h'          },
      81  		{ NULL, 0, NULL, 0 }
      82  	};
      83  
      84  	setlocale(LC_ALL, "");
      85  	bindtextdomain(PACKAGE, LOCALEDIR);
      86  	textdomain(PACKAGE);
      87  	close_stdout_atexit();
      88  
      89  	while ((c = getopt_long(argc, argv, "nr:zelfVh", longopts, NULL)) != -1)
      90  		switch (c) {
      91  		case 'n':
      92  		case OPT_OPTIONAL:
      93  		case 'r':
      94  		case 'z':
      95  		case OPT_XYZZY:
      96  		case 'e':
      97  		case 'l':
      98  		case 'f':
      99  			break;
     100  		case 'V':
     101  			print_version(EXIT_SUCCESS);
     102  		case 'h':
     103  			usage();
     104  		default:
     105  			errtryhelp(EXIT_FAILURE);
     106  		}
     107  
     108  	return EXIT_SUCCESS;
     109  }