Subject: Re: usage() function.
To: Simon Burge <simonb@netbsd.org>
From: Matthias Buelow <mkb@altair.mayn.de>
List: tech-userlevel
Date: 10/28/1999 02:38:10
Simon Burge wrote:

>While writing YATP (Yet Another Test Program) I thought about making
>a usage() function that took the usage arguments of a program as a
>parameter.  It would be called with something like:
>
>	usage("[-a num] -b -c rara");
>
>and would be little more than:
>
>	usage(char *args)
>	{
>		extern char *__progrname;
>
>		fprintf(stderr, "usage: %s %s\n", __progrname, args);
>		exit(1);
>	}
>
>Would this seem like a useful addition (probably to libutil)?

I would think it's a little bit too inflexible, for example, I
prefer to strip a leading pathname from the program name printed
or maybe I wouldn't want that in another program; I maybe want
to have a more complicated printf-style interface (with varargs
and so on) and then I'd have to do the bulk for this functionality
before calling this usage() anyways.

Also perhaps I don't want it to exit at all.
Then, there are a lot of programs having a function "usage()"
(part of your argument) and defining one in a library, even in
one that probably isn't used for portable software, creates
name clashes with existing programs.  And since it's non-standard,
for portable software you'd have to replicate the 7 lines anyways
and I don't think it's worth the effort then having them in
a library in first place.

IMHO, putting a couple of lines in your program like:

	char *myname, *foo;
	myname = NULL != (foo = strrchr(argv[0], '/')) ? foo+1 : argv[0];
	fprintf(stderr, "%s: [-a num] -b -c rara", NULL!=myname ? myname : "");

is really effortless and more powerful (to me, anyways) :).
With usage() as above, you'd still have to get the program's name somehow,
for other messages your program might produce; with my in-line proposal,
you can store myname for later use.