getopt函数

函数定义:

#include <unistd.h>  
extern char *optarg;  
extern int optind, opterr, optopt;  

#include <getopt.h>
int getopt(int argc, char * const argv[],const char *optstring);  
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);  
int getopt_long_only(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);

三者的区别是
getopt()只支持短格式选项
getopt_long()既支持短格式选项,又支持长格式选项
getopt_long_only()用法和getopt_long()完全一样,唯一的区别在输入长选项的时候可以不用输入–而使用-。一般情况下,使用getopt_long()来完成命令行选项以及参数的获取。

main函数

int main(int argc, char *argv[])

getopt

支持持段格式,比如gcc -h这里的-h就是短格式
int getopt(int argc, char * const argv[],const char *optstring); 函数前两个参数是main函数的参数,直接传入即可。
第三个参数是一个字符串,形如:ho:l::
h表示-h,后边没有参数
o:表示-o xxx,后边要带参数
l::表示-l或者-l xxx,后边的参数可带可不带,并非所有编译器都支持两个冒号。

代码例子:

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


void parseArgs(int argc, char* argv[]){
    int opt;
    while ((opt = getopt(argc, argv, "ho:l::")) != -1) {
        switch(opt){
            case 'h' :
                cout << "help" << endl;
                break;
            case 'o' :
                cout << optarg << endl;
                //optarg是unistd.h中定义好的变量,就是这里 -o 后边的参数
                cout << "-o " << optarg << endl;
                break;
            //case 'l' :
            //cout << "-l " << optarg << endl;
            //  break;
            default:
                break;
        }
    }
}


//main Function
int main (int argc, char *argv[])
{
    parseArgs(argc, argv);
    return 0;
}

程序输出:

getopt_long

int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
第三个参数optstring是与getopt函数完全一样的,用于短选项。
与getopt相比,getopt_long多一个参数:longopts

struct option 
{  
     const char *name;  
     int         has_arg;  
     int        *flag;  
     int         val;  
};  

getopt_long代码示例:

#include <iostream>
#include "getopt.h"

using namespace std;

void parseArgs(int argc, char* argv[]);


int main(int argc, char *argv[]){
    parseArgs(argc, argv);

    return 0;
}


void parseArgs(int argc, char* argv[]){
    int opt;
    static struct option long_options[] = {
        { "help", no_argument,   NULL, 'h' },
        { "xxx", no_argument,   NULL, 1 },
        { "yyy", required_argument,   NULL, 2 },
        //最后一个元素必须是这样的
        { NULL,   0,                   NULL, 0 },
    };
    while ((opt = getopt_long_only(argc, argv, "h", long_options, NULL)) != -1) {
        switch (opt) {
            case 'h':
                cout << "help" << endl;
                break;
            case 1:
                cout << "-xxx " << optarg << endl;
                break;
            case 2:
                cout << "-yyy " << optarg << endl;
                break;
            default:
                cout << "parse opt default\n";
        }
    }
}

程序输出

Windows使用getopt函数

从gnu中拷贝了头文件于c文件,直接使用即可:
https://gitee.com/feipeng8848/getopt-in-msvc

posted @ 2022-06-27 16:10  feipeng8848  阅读(103)  评论(0编辑  收藏  举报