首页 > 编程语言 > 详细

[linux][c/c++]代码片段01

时间:2019-11-08 13:43:05      阅读:72      评论:0      收藏:0      [点我收藏+]

 

#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;
}

 

[linux][c/c++]代码片段01

原文:https://www.cnblogs.com/landv/p/11819168.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!