#> cat sample.cc
#include <iostream>
#include <string>
#include "getopt.hpp"
int main() {
// - No initialization required: (argc, argv) pair automatically retrieved.
// - First argument is default option value, then all option indentifiers follow.
bool help = getarg( false, "-h", "--help", "-?" );
int version = getarg( 0, "-v", "--version", "--show-version" );
int depth = getarg( 1, "-d", "--depth", "--max-depth");
std::string file = getarg( "", "-f", "--file" );
// [...]
std::cout << help << ',' << file << ',' << version << std::endl;
}
#> g++ sample.cc && ./a.out --file=/a/b/c --depth=123
0, /a/b/c, 123
getopt: 解析命令行输入参数的库
最新推荐文章于 2024-08-14 16:01:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)