平时用C++写一些命令行工具,须要解析命令行的输入參数,这是一项繁琐而且easy出错的工作,我们不应该将主要精力放在这上面。能够考虑使用开源的库。以下的cmdline就是当中很好用的一款。

cmdline介绍

cmdline 是一个非常easy好用的C++命令行解析库,其基于模板。所以使用非常easy,写出的代码也非常优雅。

因为其仅仅包括一个头文件。所以非常easy集成到自己的项目中。

cmdline项目托管地址Github:​​https://github.com/tanakh/cmdline​

cmdline使用

以下是cmdline项目首页给出的演示样例代码,我将当中的凝视翻译成了中文,同一时候加入了一些自己使用经验和理解。

1. 

2. #include "cmdline.h"
3.


4. int main(int argc, char *argv[])
5. {
6. // 创建一个命令行解析器
7. cmdline::parser a;
8.


9. // 加入指定类型的输入參数
10. // 第一个參数:长名称
11. // 第二个參数:短名称('\0'表示没有短名称)
12. // 第三个參数:參数描写叙述
13. // 第四个參数:bool值,表示该參数是否必须存在(可选。默认值是false)
14. // 第五个參数:參数的默认值(可选,当第四个參数为false时该參数有效)
15. a.add<string>("host", 'h', "host name", true, "");
16.


17. // 第六个參数用来对參数加入额外的限制
18. // 这里端口号被限制为必须是1到65535区间的值,通过cmdline::range(1, 65535)进行限制
19. a.add<int>("port", 'p', "port number", false, 80, cmdline::range(1, 65535));
20.


21. // cmdline::oneof() 能够用来限制參数的可选值
22. a.add<string>("type", 't', "protocol type", false, "http", cmdline::oneof<string>("http", "https", "ssh", "ftp"));
23.


24. // 也能够定义bool值
25. // 通过调用不带类型的add方法
26. a.add("gzip", '\0', "gzip when transfer");
27.


28. // 执行解析器
29. // 仅仅有当全部的參数都有效时他才会返回
30. // 假设有无效參数,解析器会输出错误消息。然后退出程序
31.
// 假设有'--help'或-?'这种帮助标识被指定,解析器输出帮助信息。然后退出程序


32. a.parse_check(argc, argv);
33.


34. // 获取输入的參数值
35. cout << a.get<string>("type") << "://"
36. << a.get<string>("host") << ":"
37. << a.get<int>("port") << endl;
38.


39. // bool值能够通过调用exsit()方法来推断
40. if (a.exist("gzip")) cout << "gzip" << endl;
41. }
42.



使用測试

仅仅输入程序名,默认会输出帮助信息。选项后面的括号里显示了其类型和默认值(中括号)

1. 

2. $ ./test
3. usage: ./test --host=string [options] ...
4. options:
5. -h, --host host name (string)
6. -p, --port port number (int [=80])
7. -t, --type protocol type (string [=http])
8. --gzip gzip when transfer
9.
-?, --help print this message


10.


通过以下方式显示的输出帮助信息

1. 

2. $ ./test -?
3. usage: ./test --host=string [options] ...
4. options:
5. -h, --host host name (string)
6. -p, --port port number (int [=80])
7. -t, --type protocol type (string [=http])
8. --gzip gzip when transfer
9. -?, --help print this message
10.


输入必选字段--主机名,其它參数是可选的。当缺省时使用默认值,这里使用了默认协议​​http​​和默认port号​​80​

1. 

2. $ ./test --host=github.com
3. http://github.com:80
4.


输入​​-t​​參数。使用​​ftp​​覆盖默认值​​http​

1. 

2. $ ./test --host=github.com -t ftp
3. ftp://github.com:80
4.


因为​​-t​​參数限制为仅仅能取​​cmdline::oneof<string>("http", "https", "ssh", "ftp")​​中的某一个。

这里输入的​​-t ttp​​不在当中,因此被视为无效參数。程序输入帮助信息然后退出。

1. 

2. $ ./test --host=github.com -t ttp
3. option value is invalid: --type=ttp
4. usage: ./test --host=string [options] ...
5. options:
6. -h, --host host name (string)
7. -p, --port port number (int [=80])
8. -t, --type protocol type (string [=http])
9. --gzip gzip when transfer
10. -?, --help print this message
11. ```
12. 使用bool型參数
13. ```
14. $ ./test --host=github.com --gzip
15. http://github.com:80
16. gzip
17.


其它选项

× footer 脚注

footer() 方法用来在帮助信息后面加入自己定义文本,比如:

1. a.footer("user define information...");


执行结果:

1. 

2. $ ./test
3.
usage: ./test --host=string [options] ... user define information...

4. options:
5. -h, --host host name (string)
6. -p, --port port number (int [=80])
7. -t, --type protocol type (string [=http])
8. --gzip gzip when transfer
9. -?, --help print this message
10.


× pragram name 应用程序名

通过​​argv[0].set_program_name()​​能够设置帮助信息中的应用程序显示的名称