C getopt - <integer>
4 answers
If you do not intend to use the -1parameter 0as an argument, there is no answer to it. getoptonly for processing parameters that correspond to the standard syntax settings of POSIX utilities. Perhaps you can use GNU for this purpose getopt_long, or you can just write your own parser argv(this is easy).
: , , , . -, , , , - getopt . , getopt, , , -123 -1 23 ( , ), -1 , argv eaten -1, .
+3
, , c .
, unix (, Unix Haters (PDF-)).
, . (ish) getopt ( Bell). GNU getopt getopt_long. , -.
, ( ), "" , . , tail , , , , ...
+3
, , (, -n -5), --.
, , getopt (3) BSD . , optreset, POSIX glibc:
int ch;
long num;
char *numarg;
while ((ch = getopt(argc, argv, "0123456789")) != -1) {
switch (ch) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
numarg = argv[optind - 1];
if (numarg[0] == '-' && numarg[1] == ch && numarg[2] == '\0') {
num = '0' - ch;
} else if ((numarg = argv[optind]) != NULL && numarg[1] == ch) {
char *ep;
int numoptind = optind;
num = strtol(numarg, &ep, 10);
if (*ep != '\0') {
fprintf(stderr, "illegal number -- %s\n", numarg);
return EXIT_FAILURE;
}
/* Advance getopt internal state to next argument. */
while (optind == numoptind) {
ch = getopt(argc, argv, "0123456789");
assert(ch >= '0' && ch <= '9');
}
} else {
fprintf(stderr, "number after other options -- %s\n", numarg);
return EXIT_FAILURE;
}
break;
default:
/* Unrecognized option character. Error printed by getopt. */
return EXIT_FAILURE;
}
}
getopt getopt_long .
0