在学习webbench这个开源小项目时遇到了getopt_long()这个函数,就转查到了getopt()这个函数,功能基本一致。

函数参数和返回值

int getopt(int argc, char * const argv[], const char *optstring);
说一下什么是选项,什么是参数。
argc和argv就不说了, 这个就是在命令行输入时的选项,通过main函数的参数传入

字符串 optstring 可以下列元素:

  1. 单个字符,表示选项,
  2. 单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
  3. 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。

    • getopt处理以’-’开头的命令行参数,如optstring=”ab:c::d::”,命令行为 getopt.exe -a -b host -ckeke -d haha
    • 在这个命令行参数中,-a和-h就是选项元素,去掉’-‘,a,b,c就是选项。host是b的参数,keke是c的参数。但haha并不是d的参数,因为它们中间有空格隔开。
    • 还要注意的是默认情况下getopt会重新排列命令行参数的顺序,所以到最后所有不包含选项的命令行参数都排到最后。如getopt.exe -a ima -b host -ckeke -d haha, 都最后命令行参数的顺序是: -a -b host -ckeke -d ima haha
    • 如果optstring中的字符串以’+’加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数,getopt就会停止,返回-1。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
    int result;
    opterr = 0;  //使getopt不行stderr输出错误信息
    while ((result = getopt(argc, argv, "ab:c::")) != -1)
        switch (result)
        case 'a':
            printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg);
            break;
        case 'b':
            printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg);
            break;
        case 'c':
            printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg);
            break;
        case '?':
            printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg);
            break;
        default:
            printf("default, result=%c\n", result);
            break;
        printf("argv[%d]=%s\n", optind, argv[optind]);
    printf("result=-1, optind=%d\n", optind);   //看看最后optind的位置
    for (result = optind; result < argc; result++)
        printf("-----argv[%d]=%s\n", result, argv[result]);
    //看看最后的命令行参数,看顺序是否改变了哈。
    for (result = 1; result < argc; result++)
        printf("\nat the end-----argv[%d]=%s\n", result, argv[result]);
    return 0;

unistd里有个 optind 变量,每次getopt后,这个索引指向argv里当前分析的字符串的下一个索引,因此
argv[optind]就能得到下个字符串,通过判断是否以 ‘-‘开头就可。下面是个测试程序

#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
    int tmp = 4;
    while ((tmp = getopt(argc, argv, "abck")) != -1)
        printf("-%c\t", tmp);
        int opt = optind;
        while (opt < argc)
            if (argv[opt][0] != '-')
                printf("%s\t", argv[opt]);
                opt++;
                break;
        printf("\n");
    getchar();

再来讲我自己遇到的东西

先看代码:

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]){
    char res;
    try{
        res = getopt(argc, argv, "a:b");
        switch (res){
        case 'a':
            printf("option:a,optarg:%s\n", optarg);
            break;
        case 'b':
            printf("option:b\n");
            break;
        default:
            printf("res:%c\n", res);
            printf("optopt:%c\n", optopt);
            break;
    catch (...){
        printf("error!\n");

再看测试输出
这里写图片描述

在学习webbench这个开源小项目时遇到了getopt_long()这个函数,就转查到了getopt()这个函数,功能基本一致。函数参数和返回值int getopt(int argc, char * const argv[], const char *optstring); 说一下什么是选项,什么是参数。 argc和argv就不说了, 这个就是在命令行输入时的选项,通过main函数的参数传入字
转 自http://www.diybl.com/course/3_program/c++/cppjs/20091031/180765.html getopt被用来解析命令行选项参数。就不用自己写东东处理argv了。 #include extern char *optarg;  //选项的参数指针 extern int optind,   //下一次调用getopt的时,从optind存
命令行参数(getopt_long用法)optarg,optind getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下:  #include        int getopt_long(int argc, char * const argv[],                   const char *optstring,
一、getopts命令简介 getopts命令是用来解析Shell脚本命令行参数的工具,getopts命令参数包含需要被识别的选项字符,如果选项字符后面跟着一个冒号,则表明该字符选项需要一个命令行参数(选项字符与对应的命令行参数之间以空格分隔)(注意:冒号&问号不能被用作为选项字符)。getopts命令每次被调用时,它会将下一个选项字符放置到变量中,OPTARG则可以拿到参数值;如果option前面加冒号,则代表忽略错误 1.1 命令使用格式 getopts optstring name [arg.
getopt() 头文件 #include&lt;unistd.h&gt; 定义函数 int getopt(int argc,char * const argv[ ],const char * optstring); extern char *optarg; extern int optind, opterr, optopt; getopt() 所设置的全局变量包括:
getopt()用来解析命令行参数 getopt(int argc,char **argv,const char *shotopts) 第一个参数为命令参数的个数;第二个参数存命令参数;第三个参数为所有可能的参数字符串optstring,仅支持短参数 参数ab:c::d::代表。/getopt -a -b host -chello -d world(world不是参数值,因为-d后...
- opterr:如果设置为非零值,则 getopt() 会将错误信息输出到 stderr。 - optind:指向下一个要处理的命令行参数的索引。 - optopt:如果 getopt() 遇到了无法识别的选项字符,则该变量保存该字符。 - optarg:如果选项需要参数,则该变量指向该参数的字符串。 Windows下Docker启动Elasticsearch报max virtual memory areas vm.max_map_count [65530] is too low, increase CSDN-Ada助手: AI 写作助手上线啦!限免 4 天,快来创作试试功能吧~https://editor.csdn.net/md/?not_checkout=1&utm_source=blog_comment_recall redis哨兵部署 konsy_dong: 每一台都部署 redis哨兵部署 yyyyyyysdfg: 请问哨兵是部署在主从集群外的某一台redis服务器中,还是部署在主从集群中的某一个从节点? docker搭建kafka集群 weixin_53076693: 我写的服务器的ip加外部端口号 docker搭建kafka集群 weixin_53076693: 你好 我用springboot客戶端链接这个kafka集群出现错误java.net.ConnectException: Connection refused: no further information怎么办呀 Windows下Docker启动Elasticsearch报max virtual memory areas vm.max_map_count [65530] is too low, increase LettuceConnectionConfiguration源码解析 redis哨兵部署