GetOpt formatting improvements

Brandon S. Allbery KF8NH allbery at
Mon Jun 30 03:08:02 EDT 2008

On 2008 Jun 30, at 2:00, Johan Tibell wrote:

> On Sun, Jun 29, 2008 at 11:46 PM, Duncan Coutts
> <duncan.coutts at> wrote:
>> So if people think this is sensible I'll send three patches:
>>    1. word-wrap the description to fit in 80 columns
>>    2. use less padding between columns
>>    3. omit the args on short options when there is also a
>>       corresponding long option
> What does GNU getopt do in all these cases? I'd prefer if the programs
> I use on the command line are consistent in their `--help' display and
> GNU getopt seems to be the standard here. Trying to follow what other
> people do also makes shell scripting easier.

Options are indented by two spaces.
Short options are separated from long by ", ", and that column is left  
empty if there is no corresponding short option.
Option descriptions begin one space after the longest option that  
doesn't extend past column 30, and word wrap with wrapped lines  
indented by two additional spaces.  If any options go past column 30,  
their descriptions start one space after the options without  
attempting to align with the other descriptions, except that wrapped  
lines are aligned.

Practical example:  GNU sort

> Ordering options:
> Mandatory arguments to long options are mandatory for short options  
> too.
>   -b, --ignore-leading-blanks ignore leading blanks
>   -d, --dictionary-order      consider only blanks and alphanumeric  
> characters
>   -f, --ignore-case           fold lower case to upper case characters
>   -g, --general-numeric-sort  compare according to general numerical  
> value
>   -i, --ignore-nonprinting    consider only printable characters
>   -M, --month-sort            compare (unknown) < `JAN' < ... < `DEC'
>   -n, --numeric-sort          compare according to string numerical  
> value
>   -r, --reverse               reverse the result of comparisons
> Other options:
>   -c, --check               check whether input is sorted; do not sort
>   -k, --key=POS1[,POS2]     start a key at POS1, end it at POS 2  
> (origin 1)
>   -m, --merge               merge already sorted files; do not sort
>   -o, --output=FILE         write result to FILE instead of standard  
> output
>   -s, --stable              stabilize sort by disabling last-resort  
> comparison
>   -S, --buffer-size=SIZE    use SIZE for main memory buffer
>   -t, --field-separator=SEP use SEP instead of non-blank to blank  
> transition
>   -T, --temporary-directory=DIR  use DIR for temporaries, not  
> $TMPDIR or /tmp;
>                               multiple options specify multiple  
> directories
>   -u, --unique              with -c, check for strict ordering;
>                               without -c, output only the first of  
> an equal run
>   -z, --zero-terminated     end lines with 0 byte, not newline
>       --help     display this help and exit
>       --version  output version information and exit

Note --help and --version (no short options; also, they're boilerplate  
and apparently GNU getopt makes no attempt to match their  
descriptions' indentations with the other options, instead using a  
tab) and --temporary-directory (ends at column 31; word wrap matches  
the following option); also note the option groups are aligned  

Also note the explanatory text about option arguments before the first  
option group.

brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at
system administrator [openafs,heimdal,too many hats] allbery at
electrical and computer engineering, carnegie mellon university    KF8NH

More information about the Libraries mailing list