This blog talk about how to use 'C' Programming language to make a tool about parse command parameters.
The mainly function is getopt_long()
Function analyses:
#include <getopt.h> //getopt_long()
int getopt_long (int ___argc, char *const *___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind);
c = getopt_long(argc, argv, long_options, long_options, &option_index);
argc:how much parameters
argv:parameters
Shortopts:
#define short_options "vhf:p:grwR:V:QDI:e"
Longopts:
static struct option long_options[] =
{
{OPT_N_HELP, no_argument, 0, OPT_HELP},
{OPT_N_VERSION, no_argument, 0, OPT_VERSION},
{OPT_N_LIST, no_argument, 0, OPT_LIST},
{OPT_N_DEV, required_argument, 0, OPT_DEV},
{OPT_N_DUMP, no_argument, 0, OPT_DUMP},
{OPT_N_COUNT, required_argument, 0, OPT_COUNT},
{OPT_N_TESTALL, no_argument, 0, OPT_TESTALL},
{OPT_N_READ, no_argument, 0, OPT_READ},
{OPT_N_WRITE, no_argument, 0, OPT_WRITE},
{OPT_N_TYPE, required_argument, 0, OPT_TYPE},
{OPT_N_DATA, required_argument, 0, OPT_DATA},
{0, 0, 0, 0},
};
The long options struct below:
struct option
{
const char *name;//long options
int has_arg;//whether need parameters?
int *flag;
int val;
};
# define no_argument 0 //no need parameters
# define required_argument 1 //must point parameters
# define optional_argument 2 //selectable parameters
Flag and val,mainly two condition:
(1)、flag is NULL,build a bridge between long options and short options。
(2)、flag is not Null,I do not how to use......,please tell me how to use the parameters?
Ok,next is how to programming the simple parsing parameters tool, I think you can unsterstand my code .
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#define true 1
#define false 0
#define OPT_READ 'r'
#define OPT_WRITE 'w'
#define OPT_DEV 'd'
#define OPT_REG 'R'
#define OPT_COUNT 'C'
#define OPT_WIDTH 'W'
#define OPT_OFFSET 'O'
#define OPT_DATA 'D'
#define OPT_ADDR 'A'
#define OPT_VERSION 'v'
#define OPT_VERBOSE 'V'
#define OPT_HELP 'h'
#define OPT_TYPE 't'
#define OPT_LIST 'l'
#define OPT_TESTALL '1'
#define OPT_DUMP '2'
#define OPT_UPDATE '3'
#define OPT_BUS '4'
#define OPT_NAME '5'
#define OPT_CONFIG '6'
#define OPT_PATH '7'
#define OPT_TEST '8'
#define OPT_N_DEV "dev"
#define OPT_N_READ "read"
#define OPT_N_WRITE "write"
#define OPT_N_COUNT "count"
#define OPT_N_WIDTH "width"
#define OPT_N_DATA "data"
#define OPT_N_ADDR "addr"
#define OPT_N_LIST "list"
#define OPT_N_CHAR "char"
#define OPT_N_TEST "test"
#define OPT_N_VERSION "version"
#define OPT_N_VERBOSE "verbose"
#define OPT_N_HELP "help"
#define OPT_N_TYPE "type"
#define OPT_N_TESTALL "all"
#define OPT_N_DUMP "dump"
static struct option long_options[] =
{
{OPT_N_HELP, no_argument, 0, OPT_HELP},
{OPT_N_VERSION, no_argument, 0, OPT_VERSION},
{OPT_N_LIST, no_argument, 0, OPT_LIST},
{OPT_N_DEV, required_argument, 0, OPT_DEV},
{OPT_N_DUMP, no_argument, 0, OPT_DUMP},
{OPT_N_COUNT, required_argument, 0, OPT_COUNT},
{OPT_N_TESTALL, no_argument, 0, OPT_TESTALL},
{OPT_N_READ, no_argument, 0, OPT_READ},
{OPT_N_WRITE, no_argument, 0, OPT_WRITE},
{OPT_N_TYPE, required_argument, 0, OPT_TYPE},
{OPT_N_DATA, required_argument, 0, OPT_DATA},
{0, 0, 0, 0},
};
const char *short_options = "wrvVhC:lD:d:t:A:";
int main(int argc,char * argv[])
{
int opt=0,option_index=0,addr;
char *w_data=NULL,*type=NULL;
int g_verbose_flag,dump_flag,test_all,list_flag,write_flag,read_flag,dev_id,count;
while((opt = getopt_long (argc, argv, short_options,long_options, &option_index)) != -1)
{
switch ( opt )
{
case OPT_VERBOSE:
g_verbose_flag = 1;
printf("OPT_VERBOSE:%c\n",OPT_VERBOSE);
break;
case OPT_DUMP:
printf("OPT_DUMP:%c\n",OPT_DUMP);
dump_flag = true;
break;
case OPT_TESTALL:
printf("OPT_TESTALL:%c\n",OPT_TESTALL);
test_all = true;
break;
case OPT_LIST:
printf("OPT_LIST:%c\n",OPT_LIST);
list_flag = true;
break;
case OPT_WRITE:
printf("OPT_WRITE:%c\n",OPT_WRITE);
write_flag = true;
break;
case OPT_READ:
printf("OPT_READ:%c\n",OPT_READ);
read_flag = true;
break;
case OPT_TYPE:
printf("OPT_TYPE:%c\n",OPT_TYPE);
type = optarg;
break;
case OPT_DEV:
printf("OPT_DEV:%c\n",OPT_DEV);
dev_id = atoi ( optarg );
printf("dev_id:%d",dev_id);
break;
case OPT_ADDR:
printf("optarg:0x%x\n",*optarg);
sscanf(optarg, "%x", &addr);
break;
case OPT_COUNT:
count = atoi ( optarg );
printf("count:%d\n",count);
break;
case OPT_DATA:
w_data = optarg;
break;
case OPT_VERSION:
printf("Vesion:1\n");
return 0;
case '?':
case OPT_HELP:
printf("help\n");
break;
default:
printf("ERROR: %s: Bad command line argument(s).\n", argv[0]);
exit(0);
}
}
if(test_all == true)
printf("Testing all the devices...\n");
else if(write_flag == true)
{
printf("Writing now...\n");
printf("data:0x%x\n",*w_data);
printf("last:0x%x\n",addr);
}
else if(read_flag == true)
printf("Reading Now...\n");
else if(list_flag == true)
printf("Listing Now...\n");
else
printf("Oh ye!!!\n");
return 0;
}