如何通过命令行,为程序传入参数,可以使用函数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 on 2019-01-03 21:52 刘建章 阅读() 评论() 编辑 收藏

版权声明:本文为djw316原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/djw316/p/10217170.html