C / UNIX的参数解析助手 [英] Argument-parsing helpers for C/UNIX
问题描述
我知道以下内容:
- the venerable
getopt(3)
- the extended
getopt_long
- glibc's
argp
parser for unix-style argument vectors - popt from the GNOME project (or its spiritual successor in Glib)
我相信还有更多我没有使用,甚至听说过;快速的Google搜索会显示 Gopt , argtable 和 Optlist 。
I'm sure there's more that I haven't used or even heard of; a quick Google search reveals Gopt, argtable, and Optlist.
我个人喜欢 argp
最好,每个程序都使用 getopt
/ getopt_long
(超过某个复杂性的基线)已转换为使用 argp
。它比 popt
更广泛地可用,比 getopt_long
更强大,具有良好的文档,符合所有GNU风格的约定,非常灵活。在缺点方面,它远离最简单的使用(感谢这么灵活),并且支持它的代码是非常冗长的(就像C中的很多东西一样)。
Personally, I like argp
best, and every program I wrote using getopt
/getopt_long
(beyond a certain baseline of complexity) has been converted to use argp
. It's more widely available than popt
, more powerful than getopt_long
, well-documented, consistent with all the GNU-style conventions, and very flexible. On the downside, it's far from the easiest to use (thanks to being so flexible), and the code to support it is quite verbose (as are many things in C).
您使用什么,为什么?
是的,我的意思是C而不是C ++。有大量的C ++解析器,但我不使用C ++。
Yes, I mean C rather than C++. There's a ton of C++ parsers, but I don't use C++.
John Millikin指出 popt
长期维护。我列出它,因为许多程序仍然使用它 - 包括AbiWord,rpm,rsync和桑巴 - 尽管Gnome的努力迁移。但是我现在也添加了一个指向Glib参数解析器的链接。
John Millikin notes that popt
is no longer maintained. I list it because many programs still use it -- including AbiWord, rpm, rsync, and samba -- despite Gnome's efforts to migrate away. But I've added a link to Glib's argument parser now, too.
对于C ++参数解析, a href =http://stackoverflow.com/questions/253556/what-parameter-parser-libraries-are-there-for-c> C ++有什么参数解析器库?
For C++ argument parsing, see the question What parameter parser libraries are there for C++?
推荐答案
GNU有 gengetopt 它生成一个选项数据结构的代码和 getopt_long
代码来解析命令行并填充结构。这是相当容易学习和工作。
GNU has gengetopt which generates code for an options data structure and the getopt_long
code to parse the command line and fill the structure.. It's fairly easy to learn and works well.
作为奖励,您可以在代码周围传递选项结构,并在需要时避免全局存储。
As a bonus you can pass the options structure around your code and avoid global storage if desired.
提供GNU风格语义(显然),并且足够小,只要包含在项目中进行分发,如果你不确定你的受众的构建环境。
It provides GNU style semantics (obviously), and is small enough to simply include with the project for distribution if you're not sure of your audience's build environment.
这篇关于C / UNIX的参数解析助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!