#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <stdint.h>
#include <stdbool.h>
enum {
TEST_OPT = 3,
BUFF,
}TEST;
//数字后面带:表示需要输入输数 如-a 1,不带:表示不用带参入如 -h
static const char short_option[] = "ha:s:";
static const struct option long_option[] = {
{"help", no_argument, 0, 'h' },
{"test", required_argument, 0, TEST_OPT },
{"phy_addr", required_argument, 0, 'a'},
{"str", required_argument, 0, 's'},
{NULL, 0, NULL, 0},
};
static void usage(const char *argv0)
{
fprintf(stderr, "Usage: %s [options] ...\n", argv0);
fprintf(stderr, "Available options are\n");
fprintf(stderr, " -a <addr> Set physical address start \n");
fprintf(stderr, " -s <size> Set str \n");
fprintf(stderr, " --test test str \n");
}
int phy_addr = 0;
char str[20] = {0};
char test[20] = {0};
static int cmd_parser(int argc, char *argv[], const char *so, const struct option *lo) {
int c = 'h';
int long_index = 0;
while ((c = getopt_long(argc, argv, so, lo, NULL)) != -1) {
long_index++;
switch (c) {
case 'h' :
usage(argv[0]);
return 0;
case 'a' :
phy_addr = (strtoul(optarg, NULL, 0));
printf("1optind:%d \n", optind);
break;
case 's' :
memcpy(str, optarg, strlen(optarg));
printf("2optind:%d \n", optind);
break;
case TEST_OPT:
memcpy(test, optarg, strlen(optarg));
printf("3optind:%d argc:%d,str:%s , %s \n", optind, argc,argv[--optind],argv[optind]);
break;
case '?' :
default :
printf("input parameter error\n");
usage(argv[0]);
return 0;
}
}
if (long_index == 0) usage(argv[0]);
printf("input addr:%d, str:%s, test:%s \n",phy_addr, str, test );
return long_index;
}
int main(int argc, char *argv[])
{
int ret = -1;
if (cmd_parser(argc, argv, short_option, long_option) == 0) return 0;
}
?
|