getopt与getopt_long

如何通过命令行,为程序传入参数,可以使用函数getopt与getopt_long。

函数的声明如下:

#include <unistd.h>

       int getopt(int argc, char * const argv[],
                  const char *optstring);

       extern char *optarg;
       extern int optind, opterr, optopt;

       #include <getopt.h>

       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能够输入,类似“-r”, “-h”之类的短参数,如果需要输入长参数,如--help”就用函数getopt_long。

更详细的用法,建议man getopt。

但是我手上有个需求是,传入多个参数,如下面命令来连续读出16个寄存器的取值。

./mem_rdwr.out --rd 0x70006d00 16

 

看上去用getopt实现不了,还是自己手写程序来解析吧

 

posted @ 2019-01-03 21:52  刘建章  阅读(146)  评论(0编辑  收藏  举报