c++ argparse
需求
- c++程序传参数,像python中argparse一样方便。
方法1
用gflags 参考https://heroacool.blog.csdn.net/?type=blog
git clone https://github.com/gflags/gflags
cd gflags # 进入项目文件夹
cmake . # 使用 cmake 编译生成 Makefile 文件
make -j 24 # make 编译
sudo make install # 安装库
编译的时候加上 gflags 动态链接库
样例代码
#include <iostream>
#include <gflags/gflags.h>DEFINE_string(name, "xiaoming", "your_name");int main(int argc, char** argv) {gflags::ParseCommandLineFlags(&argc, &argv, true);std::cout << "your name is : " << FLAGS_name << std::endl;return 0;
}
执行程序
./test_node -your_name “zhou jielun”
输出为
your name is : zhou jielun
方法2
p-ranav/argparse https://github.com/p-ranav/argparse
使用方法见github链接即可