//解析命令行
options_description opts("gw options");
opts.add_options()("version,v", "print version string")
("help,h", "help message\n a bit of long text")
("config,c", value<string>()->default_value("config.json"), "load config file");
variables_map vm;
store(parse_command_line(argc, argv, opts), vm);
notify(vm);
print_vm(opts, vm);
/**
* print_vm 打印命令行参数
* @param[in] opts 描述信息
* @param[in] vm
* @return
*/
void print_vm(options_description &opts, variables_map &vm) {
cout << vm.count("version") << endl;
cout << vm.count("help") << endl;
if (vm.count("version")) {
cout << "v1.1" << endl;
return;
}
if (vm.count("config")) {
cout << "config.json" << endl;
return;
}
if (vm.empty() || vm.count("help")) {
cout << opts << endl;
return;
}
}
打印出的选项:
gw options:
-v [ --version ] print version string
-h [ --help ] help message
a bit of long text
-c [ --config ] arg (=config.json) load config file
运行代码时,加-v / -h /-c会有不同的打印