标题: The Summarize of getopt() Function in POSIX [打印本页] 作者: yifei_wang 时间: 2011-10-18 22:56 标题: The Summarize of getopt() Function in POSIX 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.作者: yifei_wang 时间: 2011-10-19 11:01 本帖最后由 yifei_wang 于 2011-10-19 18:08 编辑
fix the lib function "getopt()":
1. remove the fourth argument;
2. add two error processing conditions;
3. update the coding style.
flash, fs, led patch.
only document "flash" calls the function getopt().作者: yifei_wang 时间: 2011-10-19 18:35
update the using of "getopt()" function in each app