- 论坛徽章:
- 0
|
Format:
int getopt(int argc, char *const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
global variables:
1. optind:
a. the index of the next element to be processed in argv.
b. initialized to 1: at the beginning or scan a new argument vector.
2. opterr:
a. the return value is '?' when getopt() didn't recognize the option character.
b. when set to 0, it may prevent the error message to print.
3. optopt:
a. store the illegal option
rules:
1. argument:
a. optstring
1. contained the legitimate optons characters.
2. character which followed by a colon requires an argument.
* 3. two colons mean an option takes an optional arg.
4. "-oarg" means "-o arg" (GNU extension)
5. "W;" --> "-W foo" means "--foo"
6. '+' in the first character of optstring or the evironment variable POSIXLY_CORRECT is set, option processing stops as soon as s nooption argument is encountered.
7. '-' in the first character, each nonoption argv-element is handled as if it were the argument of an option with character code 1
2. return value:
a. return '?':
1. encounter illegal option
2. detect a missing option argument (the first character of optstring isn't a colon)
b. return ':':
1. detect a missing option argument, and the first charater of optstring is a colon.
c. return -1:
1. all command-line options have been parsed.
d. return option character:
1. option was successfully found. |
|