如果程序被指定像这样运行,那么在c++中解析命令行参数的最佳方式是什么:
prog [-abc] [input [output]]
是否有某种方法可以在标准库中实现这一点,或者我需要自己编写代码?
相关:
在unicode c++应用程序中解析命令行参数
如果程序被指定像这样运行,那么在c++中解析命令行参数的最佳方式是什么:
prog [-abc] [input [output]]
是否有某种方法可以在标准库中实现这一点,或者我需要自己编写代码?
相关:
在unicode c++应用程序中解析命令行参数
当前回答
它太大了,不可能包含在Stack Overflow回答中,但我创建了一个用于声明式定义命令行的库。它利用了c++ 14通过给每个成员变量赋初始值来构建类构造函数的能力。
这个库基本上是一个基类。要定义命令语法,需要声明一个派生自该语法的结构。下面是一个例子:
struct MyCommandLine : public core::CommandLine {
Argument<std::string> m_verb{this, "program", "program.exe",
"this is what my program does"};
Option<bool> m_help{this, "help", false,
"displays information about the command line"};
Alias<bool> alias_help{this, '?', &m_help};
Option<bool> m_demo{this, "demo", false,
"runs my program in demonstration mode"};
Option<bool> m_maximize{this, "maximize", false,
"opens the main window maximized"};
Option<int> m_loops{this, "loops", 1,
"specifies the number of times to repeat"};
EnumOption<int> m_size{this, "size", 3,
{ {"s", 1},
{"small", 1},
{"m", 3},
{"med", 3},
{"medium", 3},
{"l", 5},
{"large", 5} } };
BeginOptionalArguments here{this};
Argument<std::string> m_file{this, "file-name", "",
"name of an existing file to open"};
} cl;
参数、选项和别名类模板是在CommandLine基类的范围内声明的,您可以为自己的类型专门化它们。每个选项都包含this指针、选项名称、默认值和用于打印命令概要/用法的描述。
我仍然在寻找消除所有这些指针的需要,但我还没有找到一种不引入宏的方法。这些指针允许每个成员向驱动解析的基类中的表注册自己。
一旦有了实例,就会有多个方法重载来解析来自字符串或主样式参数向量的输入。解析器同时处理windows风格和unix风格的选项语法。
if (!cl.Parse(argc, argv)) {
std::string message;
for (const auto &error : cl.GetErrors()) {
message += error + "\n";
}
std::cerr << message;
exit(EXIT_FAILURE);
}
一旦它被解析,你可以使用operator()访问任何选项的值:
if (cl.m_help()) { std::cout << cl.GetUsage(); }
for (int i = 0; i < cl.m_loops(); ++i) { ... }
整个库只有大约300行(不包括测试)。实例有点臃肿,因为解析表是实例(而不是类)的一部分。但是每个程序通常只需要一个实例,而且这种纯声明性方法的便利性非常强大,可以通过解析新输入简单地重置实例。
其他回答
提振。Program_options
这是我最喜欢的执行命令行的方式,特别是,但绝对不是只有在效率是一个问题。这可能看起来有点过分,但我认为这种过分有一些缺点。
使用gperf进行高效的C/ c++命令行处理
缺点:
您必须首先运行一个单独的工具来生成C/ c++哈希表的代码 不支持特定的命令行接口。例如,posix简写系统“-xyz”用一个破折号声明多个选项是很难实现的。
优点:
Your command line options are stored separately from your C++ code (in a separate configuration file, which doesn't need to be read at runtime, only at compile time). All you have in your code is exactly one switch (switching on enum values) to figure out which option you have Efficiency is O(n) where n is the number of options on the command line and the number of possible options is irrelevant. The slowest part is possibly the implementation of the switch (sometimes compilers tend to implement them as if else blocks, reducing their efficiency, albeit this is unlikely if you choose contiguous values, see: this article on switch efficiency ) The memory allocated to store the keywords is precisely large enough for the keyword set and no larger. Also works in C
使用像eclipse这样的IDE,您可能可以自动化运行gperf的过程,因此您惟一需要做的就是在配置文件和switch语句中添加一个选项,然后按build…
我使用了一个批处理文件来运行gperf,并做了一些清理,并使用sed添加了包含保护(在gperf生成的.hpp文件上)…
所以,在你的软件中有非常简洁干净的代码和一个自动生成的哈希表文件,你真的不需要手动更改。我怀疑boost::program_options即使没有效率作为优先级,实际上也能打败它。
GNU C库中有这些工具,其中包括GetOpt。
如果你正在使用Qt并且喜欢GetOpt接口,那么froglogic已经在这里发布了一个不错的接口。
你可以试试我的小选项头(166 loc很容易被黑客)options.hpp。它是一个单头实现,应该按您的要求执行。它还会自动打印帮助页面。
还有一个谷歌库可用。
实际上,命令行解析已经“解决”了。随便选一个。