标签:while stderr char 示例 break nis ret etop mina
解析进程的入参。函数原型
int getopt(int argc,char * const argv[ ],const char * optstring);
解释一下optstring,这是选项字符串,其中后接:
的表示需要解析后面的入参。全部解析结束之后getopt
返回-1。
以下示例节选自nurses 5.7。
#include <stdio.h>
#include <unistd.h>
static void usage(void)
{
static const char *tbl[] =
{
""
,"Options:"
," -c set control characters"
," -s size of file"
," -t string time"
," -V print curses-version"
," -w set window-size"
};
unsigned n;
(void) fprintf(stderr, "Usage: %s [options] [terminal]\n", "test");
for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); ++n)
fprintf(stderr, "%s\n", tbl[n]);
return;
/* NOTREACHED */
}
int main (int argc, char **argv)
{
int ch = 0, opt_c = 0, size = 0;
char *time = NULL;
while ((ch = getopt(argc, argv, "cs:t:w")) != -1)
{
switch (ch)
{
case ‘c‘:
opt_c = 1;
break;
case ‘s‘:
size = atoi(optarg);
break;
case ‘t‘:
time = optarg;
break;
case ‘?‘:
default:
usage();
}
}
printf("opt_c is %d\n", opt_c);
printf("size is %d\n", size);
printf("time is %s\n", time);
return 0;
}
标签:while stderr char 示例 break nis ret etop mina
原文地址:https://www.cnblogs.com/R0124/p/12859283.html