getopt_long

    int next_option;

    const char *const short_options = "vho:";//代表可以有三种命令行参数,-v,-h,-o,o后面跟一个冒号代表-o后面需要跟参数,如 -v -h -o test

    const struct option long_options[] = { 
    {"version",0,NULL,'v'},//第一列是长参数,第二列是1代表后面需要跟参数,0代表不需要跟参数,第四列是短参数,也即-v和--version等价,-h和--help等价,-o和--output等价
    {"help",0,NULL,'h'},
    {"output",1,NULL,'o'},
    {0,0,0,0}
    };  

    while((next_option = getopt_long(argc,argv,short_options,long_options,NULL)) != -1) 
    {   
        switch(next_option)
        {   
            case 'h':
                cout<<"help"<<endl;
                break;
            case 'v':
                cout<<"this version is test"<<endl;
                break;
            case 'o':
                cout<<"output file name is "<<optarg<<endl;//optarg参数在getopt.h中已经定义,自动获取-o后面跟着的参数
                break;
        }   
    }   

    for(int i = optind;i<argc;i++)//若出现ovh之外的参数,optind记录该参数位置
    {   
        cout<<argv[i]<<" ";
    }   
    cout<<endl;

 

转载于:https://www.cnblogs.com/buptlss/p/3544456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值