"Richard L. Hamilton" <rlhamil at smart.net> wrote:
> Is there any firm guidance on the use of long options (or the addition of
> them to commands with short options, esp. in the case where there
> might be more long options than short options)?  Because whether it is this

Let me continue my last reply with some rules for commands that seem to be 
needed in case long options are added:

-       Every program needs to implement a "-help" option.
        The program also needs to recognoze "--help" for the same purpose
        to avoid confusion for GNU users.

-       As there is an officiel long option called "-help", any combination of
        valid single char options that result in the string "-help" is not 
        recognized as combination of single char options.

-       If the program (before the convertion for long options) did implement
        "program -h" as a way to get help, "program -help" should become an 
alias. 

-       "program -unonkwn-option" prints at least a short syntax and a hint that
        "program -help" should be used for more information.

-       "program -help" or "program --help" prints an overview for all options,
        one line per option.

        Here is an example for this kind of online help:

        mkisofs -help
        Usage: mkisofs [options] [-find] file... [find expression]
        Options:
          -find file... [find expr.]  Option separator: Use find command line 
to the right
          -posix-H                    Follow symbolic links encountered on 
command line
          -posix-L                    Follow all symbolic links
          -posix-P                    Do not follow symbolic links (default)
          -abstract FILE              Set Abstract filename
          -A ID, -appid ID            Set Application ID
          -biblio FILE                Set Bibliographic filename
          -cache-inodes               Cache inodes (needed to detect hard links)
          -rrip110                    Create old Rock Ridge V 1.10
          -rrip112                    Create new Rock Ridge V 1.12 (default)

If the function getargs() is used instead of getopt(), command line
parsing becomes reentrant and thread safe as there are no global variables.
Options between file names may be implemented by not using a for() loop to 
scan the file type arguments but alternatingly calling getargs() and getfiles()
until the return is NOARGS. In this case, "--" is only valid for the next 
argument.

A simple program that implements long options (but otherwise is 100% POSIX
compliant) may look this way:

#include <stdio.h>
#include <stdlib.h>
#include <schily/schily.h>
#include <schily/getargs.h>

void
usage(int ex)
{
        fprintf(stderr, "Usage: myprog [options] file...\n");
        fprintf(stderr, "Options:\n");
        fprintf(stderr, "-help          print this help\n");
        fprintf(stderr, "-verbose, -v   increment verbose level\n");
        fprintf(stderr, "-repeat #      repeat it # of times\n");
        exit(ex);
}

void
doit(int i, char *name)
{
        printf("Arg %d is '%s'\n", i, name);
}

int
main(int ac, char *av[])
{
        int             cac  = ac;
        char *const     *cav = av;
        BOOL            help = FALSE;
        int             verbose = 0;
        int             repeat = 0;
        int             i;

        save_args(ac, av);

        cac--, cav++;   /* Skip av[0] */

        ret = getargs(&cac, &cav, "help,verbose+,v+,repeat#",
                                        &help,
                                        &verbose, &verbose,
                                        &repeat);
        if (ret < 0) {
                fprintf(stderr, "Bad Option '%s' (%s).\n",
                                cav[0], getargerror(ret));
                usage(1);
        }
        if (help)
                usage(0);

        if (verbose > 1)
                printf("repeating %d times\n", repeat);

        for (i = 0; i < cac; i++) {
                doit(i, cav[i]);
        }
        return (0);
}
J?rg

-- 
 EMail:joerg at schily.isdn.cs.tu-berlin.de (home) J?rg Schilling D-13353 Berlin
       js at cs.tu-berlin.de                (uni)  
       schilling at fokus.fraunhofer.de     (work) Blog: 
http://schily.blogspot.com/
 URL:  http://cdrecord.berlios.de/old/private/ ftp://ftp.berlios.de/pub/schily

Reply via email to