首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Getopt()错误检查

Getopt()错误检查
EN

Stack Overflow用户
提问于 2017-12-06 10:28:12
回答 0查看 1.4K关注 0票数 2

我正在尝试让getopt能够处理某些标志,但我在使用getopt实现错误处理时遇到了问题。我想要防止重复的标志,例如'-s 1 -s 1',并且对标志'-s 1 2‘有太多的参数,这两个都应该退出程序。

代码语言:javascript
复制
 int opt; //command flags
 while((opt = getopt(argc, argv, "s:f:")) != -1){
 switch(opt){
  case 's':
    printf("%s\n", optarg);
    printf("%i\n", optind);
    break;
  case 'f':
    printf("%s\n", optarg);
    printf("%i\n", optind);
    break;
  default:
    //unknown command flags
    fprintf(stderr, "Usage:  fred [ -s symbol-table-file ] [ -f fred-program-file ]\n");
    return EXIT_FAILURE;
  }
}

参数太多(例如,program -s f1 -f f2 hello)。重复的标志(例如program -s f1 -s f2)。两者都应该退出程序

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47665881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档