美文网首页
linux c "struct option"解析命令参数

linux c "struct option"解析命令参数

作者: d9ead1a8ce9c | 来源:发表于2018-05-08 15:38 被阅读0次
在c程序应用程序执行的时,常常配有一些参数,如果参数少我们可以使用arvg,arvc来实现,如果参数很多并且需要传入参数复杂我们可以使用strcut option 来实现。

1. 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中出现的这个选项的参数相同。

}

2. getopt_long 介绍

1、getopt_long 函数解析命令行参数,argc、argv是调用main函数时传入的参数。传入的'-'开始的字符被解析为选项,getopt_long 一次执行解析出一个option,如果循环执行,可以将argv中的全部option解析出来;
2、在getopt_long 的执行中,每次进入都会更新getopt_long变量,该变量指向下一个argv参数;
3、如getopt_long 返回-1,表示argv[]中的所有选项被解析出,optind指向第一个非选项的argument元素;这里要注意,在getopt_long 执行过程中会将单独的argument交换到argv数组的后面,option选项提前,如:cmd -a file1 -b file2,如果a/b均为不带参数的选项,这最终argv数组变为:cmd -a -b file1 file2;
4、optstring指定选项合法的选项,一个字符代表一个选项,在字符后面加一个':'表示该选项带一个参数,字符后带两个':'表示该选项带可选参数(参数可有可无),若有参数,optarg指向该该参数,否则optarg为0;
5、前面说了getopt_long 会进行argv顺序的调整,但也可以通过设置optstring改变它的方式,这里有两种:
 1) 如果optstring的第一个参数是'+'或者POSIXLY_CORRECT被设置,则getopt_long 在原argv的顺序上遇到第一个非选项就返回-1;
 2) 如果optstring的第一个参数是'-',则会将所有的非选项当选项处理,并且返回1,用字符代码1表示该选项;
6、如果getopt_long 不能识别一个选项字符,它会打印一个错误消息到stderr上,并将该字符存放到optopt中,返回'?';调用程序可以设置opterr=0设置不打印错误信息;注意:要使能打印错误信息,optstring的第一个字符(或者在第一个字符是+/-之后)不能是':',否则也不会打印错误;
7、如果optstring中指定了option需要参数,但在命令行没有参数,那么getopt_long 将返回'?',如果在optstring的第一个字符(或者在第一个字符是+/-之后)是':',那么将返回':';

3. 代码示例

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <linux/fb.h>
#include <getopt.h>
#include <signal.h>
#include <errno.h>
#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;
}

相关文章

网友评论

      本文标题:linux c "struct option"解析命令参数

      本文链接:https://www.haomeiwen.com/subject/jhairftx.html