在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子。下面介绍使用getopt_long解析命令行选项。
短选项
长选项
是否需要参数
备注
-v
--version
否
查询版本号
-n
--name
是(用户名)
指定使用者
-d
--debug
否
是否已测试
1、函数出处
程序中主要使用:
1、函数出处
- #include <getopt.h> //getopt_long()头文件位置
- int getopt_long (int ___argc, char *const *___argv,
- const char *__shortopts,
- const struct option *__longopts, int *__longind);
- int getopt_long_only (int ___argc, char *const *___argv,
- const char *__shortopts,
- const struct option *__longopts, int *__longind);
- struct option
- {
- const char *name;//长选项名
- int has_arg;//是否需要参数
- int *flag;
- int val;
- };
- # define no_argument 0 //不需要参数
- # define required_argument 1 //必须指定参数
- # define optional_argument 2 //参数可选