start unifying help handling
This commit is contained in:
parent
f5e65274f0
commit
9d50d4cb25
26
src/_usage.c
Normal file
26
src/_usage.c
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @file _usage.c
|
||||
* @brief standardize help/usage output across all our programs
|
||||
* @internal
|
||||
*
|
||||
* Copyright 2007 Gentoo Foundation
|
||||
* Released under the GPLv2
|
||||
*/
|
||||
|
||||
#ifndef APPLET
|
||||
# error you forgot to define APPLET
|
||||
#endif
|
||||
|
||||
static void usage (int exit_status)
|
||||
{
|
||||
int i;
|
||||
printf ("Usage: " APPLET " [options]\n\n");
|
||||
printf ("Options: [" getoptstring "]\n");
|
||||
for (i = 0; longopts[i].name; ++i)
|
||||
printf (" -%c, --%s\n", longopts[i].val, longopts[i].name);
|
||||
exit (exit_status);
|
||||
}
|
||||
|
||||
#define case_RC_COMMON_GETOPT \
|
||||
case 'h': usage (EXIT_SUCCESS); \
|
||||
default: usage (EXIT_FAILURE);
|
@ -5,6 +5,8 @@
|
||||
Copyright 2007 Gentoo Foundation
|
||||
*/
|
||||
|
||||
#define APPLET "fstabinfo"
|
||||
|
||||
#include <errno.h>
|
||||
#include <getopt.h>
|
||||
#include <libgen.h>
|
||||
@ -54,6 +56,17 @@ static struct mntent *getmntfile (FILE *fp, const char *file)
|
||||
}
|
||||
#endif
|
||||
|
||||
#define getoptstring "f:m:o:p:h"
|
||||
static struct option longopts[] = {
|
||||
{ "fstype", 1, NULL, 'f'},
|
||||
{ "mountcmd", 1, NULL, 'm'},
|
||||
{ "opts", 1, NULL, 'o'},
|
||||
{ "passno", 1, NULL, 'p'},
|
||||
{ "help", 0, NULL, 'h'},
|
||||
{ NULL, 0, NULL, 0}
|
||||
};
|
||||
#include "_usage.c"
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
#ifdef HAVE_GETMNTENT
|
||||
@ -67,15 +80,7 @@ int main (int argc, char **argv)
|
||||
int n = 0;
|
||||
char c;
|
||||
|
||||
static struct option longopts[] = {
|
||||
{ "fstype", 1, NULL, 'f'},
|
||||
{ "mountcmd", 1, NULL, 'm'},
|
||||
{ "opts", 1, NULL, 'o'},
|
||||
{ "passno", 1, NULL, 'p'},
|
||||
{ NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
while ((c = getopt_long (argc, argv, "f:m:o:p:",
|
||||
while ((c = getopt_long (argc, argv, getoptstring,
|
||||
longopts, (int *) 0)) != -1)
|
||||
{
|
||||
#ifdef HAVE_GETMNTENT
|
||||
@ -133,9 +138,12 @@ int main (int argc, char **argv)
|
||||
break;
|
||||
}
|
||||
|
||||
case 'h':
|
||||
END_ENT;
|
||||
usage (EXIT_SUCCESS);
|
||||
default:
|
||||
END_ENT;
|
||||
exit (EXIT_FAILURE);
|
||||
usage (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
END_ENT;
|
||||
@ -146,4 +154,3 @@ int main (int argc, char **argv)
|
||||
|
||||
exit (result);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
Copyright 2007 Gentoo Foundation
|
||||
*/
|
||||
|
||||
#define APPLET "mountinfo"
|
||||
|
||||
#include <sys/types.h>
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
#include <sys/param.h>
|
||||
@ -139,16 +141,7 @@ static struct option longopts[] = {
|
||||
{ "help", 0, NULL, 'h'},
|
||||
{ NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
static void usage (int exit_status)
|
||||
{
|
||||
int i;
|
||||
printf ("Usage: mountinfo [options]\n\n");
|
||||
printf ("Options:\n");
|
||||
for (i = 0; longopts[i].name; ++i)
|
||||
printf (" -%c, --%s\n", longopts[i].val, longopts[i].name);
|
||||
exit (exit_status);
|
||||
}
|
||||
#include "_usage.c"
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
@ -219,11 +212,7 @@ int main (int argc, char **argv)
|
||||
reverse = true;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
usage (EXIT_SUCCESS);
|
||||
|
||||
default:
|
||||
usage (EXIT_FAILURE);
|
||||
case_RC_COMMON_GETOPT
|
||||
}
|
||||
|
||||
while (optind < argc) {
|
||||
|
@ -5,6 +5,8 @@
|
||||
Released under the GPLv2
|
||||
*/
|
||||
|
||||
#define APPLET "rc-status"
|
||||
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -51,6 +53,17 @@ static void print_service (char *service)
|
||||
ebracket (cols, color, status);
|
||||
}
|
||||
|
||||
#define getoptstring "alsuh"
|
||||
const struct option longopts[] = {
|
||||
{"all", 0, NULL, 'a'},
|
||||
{"list", 0, NULL, 'l'},
|
||||
{"servicelist", 0, NULL, 's'},
|
||||
{"unused", 0, NULL, 'u'},
|
||||
{"help", 0, NULL, 'h'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
#include "_usage.c"
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
char **levels = NULL;
|
||||
@ -62,15 +75,7 @@ int main (int argc, char **argv)
|
||||
int i;
|
||||
int j;
|
||||
|
||||
const struct option longopts[] = {
|
||||
{"all", 0, NULL, 'a'},
|
||||
{"list", 0, NULL, 'l'},
|
||||
{"servicelist", 0, NULL, 's'},
|
||||
{"unused", 0, NULL, 'u'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1)
|
||||
while ((c = getopt_long(argc, argv, getoptstring, longopts, &option_index)) != -1)
|
||||
switch (c) {
|
||||
case 'a':
|
||||
levels = rc_get_runlevels ();
|
||||
@ -103,10 +108,8 @@ int main (int argc, char **argv)
|
||||
rc_strlist_free (levels);
|
||||
rc_strlist_free (services);
|
||||
exit (EXIT_SUCCESS);
|
||||
case '?':
|
||||
exit (EXIT_FAILURE);
|
||||
default:
|
||||
exit (EXIT_FAILURE);
|
||||
|
||||
case_RC_COMMON_GETOPT
|
||||
}
|
||||
|
||||
while (optind < argc)
|
||||
|
@ -6,6 +6,8 @@
|
||||
* Distributed under the terms of the GNU General Public License v2
|
||||
*/
|
||||
|
||||
#define APPLET "runscript"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/signal.h>
|
||||
#include <sys/stat.h>
|
||||
@ -808,6 +810,19 @@ static void svc_restart (const char *service, bool deps)
|
||||
}
|
||||
}
|
||||
|
||||
#define getoptstring "dCDNqvh"
|
||||
static struct option longopts[] = {
|
||||
{ "debug", 0, NULL, 'd'},
|
||||
{ "nocolor", 0, NULL, 'C'},
|
||||
{ "nocolour", 0, NULL, 'C'},
|
||||
{ "nodeps", 0, NULL, 'D'},
|
||||
{ "quiet", 0, NULL, 'q'},
|
||||
{ "verbose", 0, NULL, 'v'},
|
||||
{ "help", 0, NULL, 'h'},
|
||||
{ NULL, 0, NULL, 0}
|
||||
};
|
||||
#include "_usage.c"
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
const char *service = argv[1];
|
||||
@ -818,17 +833,6 @@ int main (int argc, char **argv)
|
||||
int retval;
|
||||
char c;
|
||||
|
||||
static struct option longopts[] = {
|
||||
{ "debug", 0, NULL, 'd'},
|
||||
{ "help", 0, NULL, 'h'},
|
||||
{ "nocolor", 0, NULL, 'C'},
|
||||
{ "nocolour", 0, NULL, 'C'},
|
||||
{ "nodeps", 0, NULL, 'D'},
|
||||
{ "quiet", 0, NULL, 'q'},
|
||||
{ "verbose", 0, NULL, 'v'},
|
||||
{ NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
/* Show help if insufficient args */
|
||||
if (argc < 3) {
|
||||
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
|
||||
@ -922,7 +926,7 @@ int main (int argc, char **argv)
|
||||
argv++;
|
||||
|
||||
/* Right then, parse any options there may be */
|
||||
while ((c = getopt_long (argc, argv, "dhCDNqv",
|
||||
while ((c = getopt_long (argc, argv, getoptstring,
|
||||
longopts, (int *) 0)) != -1)
|
||||
switch (c) {
|
||||
case 'd':
|
||||
|
Loading…
Reference in New Issue
Block a user