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

Posted landv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[linux][c/c++]代码片段01相关的知识,希望对你有一定的参考价值。

 

#include <stdio.h>
#include <unistd.h>
 
void usage() {
    printf("Usage:
");
    printf("	OptDemo [-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
", optarg);
                break;
            case b:
                printf("opt is b, oprarg is: %s
", optarg);
                break;
            case c:
                printf("opt is c, oprarg is: %s
", optarg);
                break;
            case ?:
                printf("发生错误时提示用户正确的使用方式
");
                usage(); // 提示使用说明
                printf("
");
                break;
        }
    }
    return 0;
}

 

以上是关于[linux][c/c++]代码片段01的主要内容,如果未能解决你的问题,请参考以下文章