default_value :当命令行参数 ,没有 填写 选项时的 默认值;
implicit_value : 当命令行参数 设置了 选项,但是没有填写值 的情况下的 默认值;
implicit_value : 源码 注释
/** Specifies an implicit value, which will be used
if the option is given, but without an adjacent value.
Using this implies that an explicit value is optional, but if
given, must be strictly adjacent to the option, i.e.: '-ovalue'
or '--option=value'. Giving '-o' or '--option' will cause the
implicit value to be applied.
*/
关于 notify 和 一选项 多参数 在 实例2 里显示。
以下 转载:
http://blog.csdn.net/morning_color/article/details/50241987
boost::program_options 用法详解
简介
program options是一系列
pair<name,value>
组成的选项列表,它允许程序通过命令行或配置文件来读取这些参数选项.
主要组件
program_options的使用主要通过下面三个组件完成:
组件名 | 作用 |
---|---|
options_description(选项描述器) | 描述当前的程序定义了哪些选项 |
parse_command_line(选项分析器) | 解析由命令行输入的参数 |
variables_map(选项存储器) | 容器,用于存储解析后的选项 |
代码流程
示例一
下面的代码是boost::program_options的一个简单的用法示例.
该示例中指定了两个选项,分别是–help和–filename.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
在编译后(假设在linux系统下其编译后的可执行文件为a.out)
输入
./a.out –help
则其输出为:
all options:
–filename arg the file name which want to be found
–help this is a program to find a specified file
输入
./a.out –filename=program_test
则其输出为
find program_test
若不指定选项,即输入
./a.out
则输出为
no options found
示例二
下面的这个示例主要用来说明外部变量,参数默认值以及一个选项对应多个值的情况
这段代码主要有四个选项:
–apple : 苹果的数量
–orange:橘子的数量
–address:水果的生产地,可指定多个生产地
–help: 打印帮助信息
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
输入
./a.out –help
输出
all options:
-a [ –apple ] arg (=10) 苹果的数量
-o [ –orange ] arg (=20) 橘子的数量
–address arg 生产地
–help 计算苹果和橘子的总数量
指定苹果和橘子的数量:
./a.out –apple=8 –orange=20
其输出为:
苹果的数量:8
橘子的数量:20
总数量数量:28
仅指定橘子的数量,不指定苹果的数量:
./a.out –orange=20
其输出为:
苹果的数量:10
橘子的数量:20
总数量数量:30
可以看到,由于没有输入苹果的数量,所以输出的苹果的数量为我们指定的默认值
指定一个生产地:
./a.out –apple=8 –orange=20 –address=山东
输出:
生产地为:山东
苹果的数量:8
橘子的数量:20
总数量数量:28
指定多个生产地:
./a.out –apple=8 –orange=20 –address=山东 陕西
输出为
生产地为:山东 陕西
苹果的数量:8
橘子的数量:20
总数量数量:28
简写形式的输入:
./a.out -a 8 -o 20 –address=山东
输出:
生产地为:山东
苹果的数量:8
橘子的数量:20
总数量数量:28