#include <stdio.h>
#include <unistd.h>
void usage() {
printf("Usage:\n");
printf("\tOptDemo [-a] [-b] [-c message]");
}
int main(int argc, char *argv[]) {
int o;
const char *optstring = "abc::"; // 有三个选项-abc,其中c选项后有两个冒号,表示后面可选参数
while ((o = getopt(argc, argv, optstring)) != -1) {
switch (o) {
case 'a':
printf("opt is a, oprarg is: %s\n", optarg);
break;
case 'b':
printf("opt is b, oprarg is: %s\n", optarg);
break;
case 'c':
printf("opt is c, oprarg is: %s\n", optarg);
break;
case '?':
printf("发生错误时提示用户正确的使用方式\n");
usage(); // 提示使用说明
printf("\n");
break;
}
}
return 0;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有