参数的解析佣工的C / UNIX [英] Argument-parsing helpers for C/UNIX

查看:95
本文介绍了参数的解析佣工的C / UNIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道了以下内容:


  • 古老的的getopt(3)

  • 扩展 getopt_long

  • 的glibc的 argp 解析器UNIX风格的参数向量

  • POPT 从GNOME项目(或的Glib

  • 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)

我敢肯定有更多的,我没有使用过或听说过的;快速谷歌搜索发现 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风格的约定和非常灵活。缺点是,它远远最容易使用的(感谢这么灵活)和code来支持它相当冗长(因为很多东西在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++.

约翰·米利金指出, POPT 不再保留。我列出,是因为许多程序仍然使用它 - 包括AbiWord的,转,rsync的,和桑巴 - 尽管侏儒的努力迁移走 。但我添加了一个链接,油嘴的说法解析器现在也。

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 ++参数解析,看看问题<一href=\"http://stackoverflow.com/questions/253556/what-parameter-parser-libraries-are-there-for-c\">What参数解析器库在那里为C ++?

For C++ argument parsing, see the question What parameter parser libraries are there for C++?

推荐答案

GNU有 gengetopt 产生$ C $下一个选项的数据结构和 getopt_long code解析命令行和填充结构。它是相当简单易学,效果很好。

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.

作为奖励,你可以通过选项结构在你的code,如果需要避免全球存储。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆