首页 > 其他 > 详细

getopt与getopt_long

时间:2019-01-03 22:27:31      阅读:201      评论:0      收藏:0      [点我收藏+]

如何通过命令行,为程序传入参数,可以使用函数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实现不了,还是自己手写程序来解析吧

 

getopt与getopt_long

原文:https://www.cnblogs.com/djw316/p/10217170.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!