随笔 - 1282  文章 - 0  评论 - 8  阅读 - 50万

【C++】cmdline —— 轻量级的C++命令行解析库

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

cmdline介绍

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

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

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

cmdline使用

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

#include "cmdline.h"

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

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

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

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

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

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

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

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


使用測试

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

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

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

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

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

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

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

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

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

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

$ ./test --host=github.com -t ttp
option value is invalid: --type=ttp
usage: ./test --host=string [options] ...
options:
  -h, --host    host name (string)
  -p, --port    port number (int [=80])
  -t, --type    protocol type (string [=http])
      --gzip    gzip when transfer
  -?, --help    print this message
  ```
使用bool型參数
```
$ ./test --host=github.com --gzip
http://github.com:80
gzip

其它选项

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

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

执行结果:

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

options:
  -h, --host    host name (string)
  -p, --port    port number (int [=80])
  -t, --type    protocol type (string [=http])
      --gzip    gzip when transfer
  -?, --help    print this message

× pragram name 应用程序名

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

posted on   ljbguanli  阅读(8228)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示