getopt_long的使用

头文件:#include
函数原型:

int getopt_long(int argc, char* constargv[], 
                     const char*optstring,
                     const struct option*longopts, 
                     int*longindex);

参数解析:

  1. argc、argv直接从main函数中获取。
  2. opting是选项参数组成的字符串,由下列元素组成:
    (1)单个字符,表示选项,
    (2)单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
    (3)单个字符后跟两个冒号,表示该选项后可以有参数也可以没有参数。如果有参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
  3. struct option结构体
struct option {
const char *name; //name表示的是长参数名
int has_arg;
//has_arg有3个值,no_argument(或者是0),表示该参数后面不跟参数值
// required_argument(或者是1),表示该参数后面一定要跟个参数值
// optional_argument(或者是2),表示该参数后面可以跟,也可以不跟参数值
int *flag;    
//用来决定,getopt_long()的返回值到底是什么。如果这个指针为NULL,那么getopt_long()返回该结构val字段中的数值。如果该指针不为NULL,getopt_long()会使得它所指向的变量中填入val字段中的数值,并且getopt_long()返回0。如果flag不是NULL,但未发现长选项,那么它所指向的变量的数值不变。
int val;    
//和flag联合决定返回值 这个值是发现了长选项时的返回值,或者flag不是 NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如1 或0;另一方面,如 果flag是NULL,那么val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的参数相同。

返回值:

  1. 如getopt_long 返回-1,表示argv[]中的所有选项被解析出。
  2. 如果成功找到了选项,getopt()会返回选项字符。
  3. 如果遇到一个缺少参数的选项字符,返回值就要取决于optstring的第一个字符了:如果是’:’,则返回’:’,否则返回’?’。

内部定义的一些全局变量:
extern char* optarg;
extern int optind, opterr, optopt;
参数optarg:指向当前选项参数的指针
参数optind:再次调用getopt时的下一个argv指针索引
参数optopt:表示最后一个未知选项

例子:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "client.h"
#include "server.h"
int FRAME_PER_PACKET = 0;
static struct option long_options[] = {
{ "server", 0, 0, 's'},
{ "client_self", 0, 0, 'c'},
{ "client_send", 0, 0, 'd'},
{ "client_recv", 0, 0, 'v'},
{ "PER_FREAM_PACKET", 1, 0, 'p'},
{ "auto test from 1 to 10", 0, 0, 'A'},
{0, 0, 0, 0},
};
static char optstring[] = "scdvp:A";

#define print_opt_help(opt_index, help_str)             \
do {                                \
    printf("\t---%s\t\t-%c\t%s", long_options[opt_index].name, (char)long_options[opt_index].val, help_str); \
} while (0)

void usage() {
    printf("\nUsage:\n");
print_opt_help(0, "action server\n");
print_opt_help(1, "action client self send and self recv\n");
print_opt_help(2, "action client send\n");
print_opt_help(3, "action client recv\n");
print_opt_help(4, "set PER_FREAM_PACKET num\n");
print_opt_help(5, "auto test from 1 to 10\n");

printf("\n\nExamples:\n");
printf("\t./net-test -s\t---\taction server\n");
printf("\t./net-test -c -p 10\t---\taction client PER_FREAM_PACKET is 10\n");
}
int main(int argc, char *argv[]){
    int c = 0;
    client_act client_act_t;
    while(1){
        c = getopt_long(argc, argv, optstring, long_options, NULL);
        switch (c){
            case 's':
                printf("start server\n");
                server_fun();
                break;
            case 'c':
                printf("start client self send and recv test\n");
                                client_act_t = RECV_AND_SEND;
                client_fun(client_act_t);
                break;
            case 'd':
                printf("start client send test\n");
                client_act_t = SEND_ONLY;
                client_fun(client_act_t);
                break;
        case 'v':
                printf("start client recv test\n");
                client_act_t = RECV_ONLY;
                client_fun(client_act_t);
                break;
        case 'A':
                printf("start client auto test\n");
                client_fun(client_act_t);
                break;
        case 'p':
                FRAME_PER_PACKET = atoi(optarg);
                printf("set PER_FREAM_PACKET is %d\n", FRAME_PER_PACKET);
                break;
        default :
                usage();
                return -1;
        }
    }
    return 0;
}

也可参考RTMP源码:

你可能感兴趣的:(杂项)