command line options

Arthur David Olson ado at elsie.UUCP
Fri Apr 8 00:59:26 AEST 1988


Here are three things I want from a command line option handler.

1.  I want all the information about a particular option to appear in one
    place.  So instead of an interface where I set up a table such as

>	static Option desc[] = {
>		O_flg ('f', flag),
>		O_int ('i', number),
>		O_str ('s', string),
>		O_chr ('c', character),
>		O_dbl ('d', dbl),
>		O_directive ("remaining: 1-2"),
>		O_directive
>		("usage: [-f] [-i nn] [-cC] [-d float] [-s string] src [dst]"),
>		O_end,
>	};

    (see the referenced article), I want an interface where I set up a table
    such as

	static Option desc[] = {
		O_flg('f', flag),
		O_int('i', number, "nn"),
		O_str('s', string, "string"),
		O_chr('c', character, "C"),
		O_dbl('d', dbl, "float"),
		O_directive("remaining: 1-2", "src [dst]"),
		O_end,
	};

    This helps ensure that usage messages match what the program actually does,
    and eases the business of conditionally compiling options into a program.

2.  I want some uniform way of getting programs to output their usage
    messages.  Here at elsie, the convention we use is that if a (locally
    written) program's only argument is "=", as in the command line
				zic =
    or
				zic -l =
    (for the benefit of csh/ksh aliasing fanatics) the program outputs its
    usage message.  The particular convention used is unimportant to me; what
    counts is having a uniform way to ask for the usage message, and an easy
    way to achieve uniformity is to push the logic into the command line option
    handler.

3.  I want to be able to find out if "--" was used on the command line.

    Pike's Position is that
		Programs that accept multiple filenames
		should do nothing if given no arguments.
    (The name and wording are mine; see page 186 of "The UNIX Programming
    Environment" for details.)  This ensures that if you use a command such as
			cat `find * -type f -print`
    to concatenate all the files in a directory and its subdirectories,
    you won't get surprised when there are NO files in the directory and
    cat starts reading from its standard input.

    My preference is to provide a uniform way of telling programs that you
    want them to do nothing if there are no arguments; one way I see of doing
    this is to use the presence of "--" on the command line for that purpose.
    So, for example, you'd
		cat -- `find * -type f -print`
    to avoid having cat do anything if find didn't find any files.
    With such a mechanism in place, you can avoid having to write
		cat -
    when you want to use cat as a filter, and still avoid surprises when you
    want to give cat a (potentially empty) list of file names.
-- 
	ado at ncifcrf.gov			ADO is a trademark of Ampex.



More information about the Comp.lang.c mailing list