AT&T Home | AT&T Labs | Research
AT&T Labs, Inc. - Research

The Yoix® Scripting Language

Home | What's New | Grammar | Documentation | Download | License | YDAT | YWAIT | Byzgraf | FAQs
getopt (Array args, String letters [, Array words]) typedict.Option
getopt (Array args, Array words)  
 
Returns an int that represents the next option in args that is matched by letters or words, -1 when no more options are left, and ? if there was an error. letters describe single character options. words, which must be an array of strings, describe long options that are introduced by -- when both letters and words are not NULL, otherwise - or -- can be used. The special option -- is always recognized, skipped, and forces getopt to return -1, which means it can be used to separate options from non-option arguments.

An option letter or long option name that is followed by a single colon in letters or words requires an argument, two colons means the argument is optional, and no colon means the option does not take an argument. An option's argument is always stored in optarg, which will be NULL when no argument was found. Long options in args use an equal sign to separate an option name from its argument. A required argument can be separated from an option letter or word (i.e., it can follow the option in the args array), but an optional argument can not.

When getopt recognizes a long option that is described by an entry in words it saves the name of that option in optword, otherwise optword will be NULL. By default getopt returns - when a long option is recognized, however an entry in words that contains = tells getopt to return the single character that follows the equal sign.
 
 Example:   Put the program,
import yoix.*.*;

Option option;
int    ch;

while ((ch = option.getopt(global.argv, "a:bcx:y:")) != -1) {
    switch (ch) {
        case 'a':
        case 'x':
        case 'y':
            printf("ch=%c, optarg=%s\n", ch, option.optarg);
            break;

        case 'b':
        case 'c':
            printf("ch=%c\n", ch);
            break;

        case '?':
            fprintf(stderr, "Error: %s\n", option.opterror);
            break;
    }
}

printf("Remaining arguments: %O\n", global.argv + option.optind);
in a file named getopt.yx, run the interpreter by typing,
yoix getopt.yx -x12.3 -bc arg1 arg2
and
ch=x, optarg=12.3
ch=b
ch=c
Remaining arguments: Array[5:3]
    ^"getopt.yx"
    ^"-x12.3"
    ^"-bc"
   >^"arg1"
    ^"arg2"
prints on standard output. Change the program to
import yoix.*.*;

Option option;
int    ch;

Array words = {
    "help",       // no arg, return '-'
    "info",       // no arg, return '-'
    "test:",      // required arg, return '-'
    "run=r::",    // optional arg, return 'r'
};

while ((ch = option.getopt(global.argv, words)) != -1) {
    switch (ch) {
        case 'r':
            printf("word=%s, arg=%O\n", option.optword,
                    option.optarg);
            break;

        case '-':
            switch (option.optword) {
                case "help":
                    printf("word=%s, arg=%O\n", option.optword,
                            option.optarg);
                    break;

                case "info":
                    printf("word=%s, arg=%O\n", option.optword,
                            option.optarg);
                    break;

                default:
                    printf("word=%s, arg=%O\n", option.optword,
                            option.optarg);
                    break;
            }
            break;

        case '?':
            fprintf(stderr, "Error: %s\n", option.opterror);
            break;
    }
}

printf("Remaining arguments: %O\n", global.argv + option.optind);
and type
yoix getopt.yx --help -info --test=12 arg1 arg2
and
word=help, arg=NULL:OBJECT
word=info, arg=NULL:OBJECT
word=test, arg=12
Remaining arguments: Array[6:4]
    ^"/tmp/xxx"
    ^"--help"
    ^"-info"
    ^"--test=12"
   >^"arg1"
    ^"arg2"
prints on standard output. Notice that we could use - or -- to introduce the long options because there were no single character options for getopt to recognize.
 
 Return:   int
 
 See Also:   Option

 

Yoix is a registered trademark of AT&T Intellectual Property.