getopt的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014303647/article/details/87467086

提取命令行的参数使用getopt系统函数

例子:

#include<unistd.h>
#include<iostream>
#include<getopt.h>
#include<stdlib.h>
#include<string>
using namespace std;

int main(int argc,char *argv[])
{
    const char *str = "t:l:p:";
    int opt;
    while((opt = getopt(argc,argv,str))!=-1)
    {
        switch(opt)
        {
            case 't':
            {
                int threadNum = atoi(optarg);
                cout << threadNum <<endl;
                break;
            }
            case 'l':
            {
                string logpath = optarg;
                cout << logpath << endl;
                break;
            }
            case 'p':
            {
                int port = atoi(optarg);
                cout << port << endl;
                break;
            }
        //    printf("%d %s %d\n",threadNum,logpath,port);
        }
    }
    return 0;
}

截图:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u014303647/article/details/87467086