From c6b5813f7258bbccc8368fcdc0e1ba94219a7843 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Wed, 8 Feb 2023 16:27:53 -0800 Subject: Add -help and -version options Signed-off-by: Alan Coopersmith --- man/oclock.man | 6 ++++++ oclock.c | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/man/oclock.man b/man/oclock.man index bfe8ebb..2213d88 100644 --- a/man/oclock.man +++ b/man/oclock.man @@ -75,6 +75,12 @@ fit the outline of the clock. .TP 8 .B \-transparent causes the clock to consist only of the jewel, the hands, and the border. +.TP 8 +.B \-help +prints a usage message and exits. +.TP 8 +.B \-version +prints version info and exits. .SH COLORS If you would like your clock to be viewable in color, include the following in the #ifdef COLOR section you read with xrdb: diff --git a/oclock.c b/oclock.c index b9273ba..2a436b1 100644 --- a/oclock.c +++ b/oclock.c @@ -46,7 +46,7 @@ in this Software without prior written authorization from The Open Group. static void die ( Widget w, XtPointer client_data, XtPointer call_data ); static void save ( Widget w, XtPointer client_data, XtPointer call_data ); -static void usage ( void ); +static void usage ( int exitval ); static void quit ( Widget w, XEvent *event, String *params, Cardinal *num_params ); @@ -70,7 +70,7 @@ static void save(Widget w, XtPointer client_data, XtPointer call_data) /* Exit with message describing command line format */ -static void usage(void) +static void usage(int exitval) { fprintf(stderr, "usage: oclock\n" @@ -78,8 +78,10 @@ static void usage(void) " [-display [{host}]:[{vs}]]\n" " [-fg {color}] [-bg {color}] [-bd {color}] [-bw {pixels}]\n" " [-minute {color}] [-hour {color}] [-jewel {color}]\n" -" [-backing {backing-store}] [-shape] [-noshape] [-transparent]\n"); - exit(1); +" [-backing {backing-store}] [-shape] [-noshape] [-transparent]\n" +" [-help] [-version]\n" + ); + exit(exitval); } /* Command line options table. Only resources are entered here...there is a @@ -107,6 +109,22 @@ main(int argc, char *argv[]) Arg arg[2]; int i; + /* Handle args that don't require opening a display */ + for (int n = 1; n < argc; n++) { + const char *argn = argv[n]; + /* accept single or double dash for -help & -version */ + if (argn[0] == '-' && argn[1] == '-') { + argn++; + } + if (strcmp(argn, "-help") == 0) { + usage(0); + } + if (strcmp(argn, "-version") == 0) { + puts(PACKAGE_STRING); + exit(0); + } + } + toplevel = XtOpenApplication(&xtcontext, "Clock", options, XtNumber(options), &argc, argv, NULL, sessionShellWidgetClass, NULL, 0); @@ -116,7 +134,7 @@ main(int argc, char *argv[]) fprintf(stderr, " %s", argv[n]); } fputs("\n\n", stderr); - usage(); + usage(1); } XtAddCallback(toplevel, XtNsaveCallback, save, NULL); XtAddCallback(toplevel, XtNdieCallback, die, NULL); -- cgit v1.2.3