getopt_long 与 getopt_long_only

发布于 2024-08-20 06:49:17 字数 132 浏览 14 评论 0原文

要制作一个合适的 Linux/unix 风格的应用程序,最好的选择是什么(例如,ls 使用 getopt_long,但例如 ffmpeg getopt_long_only)。 您推荐哪一款?

干杯,

to do a proper Linux/unix styled application, what is the best choice (eg. afaik ls uses getopt_long but for example ffmpeg getopt_long_only).
Which one do you recommend?

Cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长途伴 2024-08-27 06:49:17

在我看来,以下几点通常是正确的:

  • 用户喜欢长的、自然的语言选项,因为它们很容易记住。
  • 编写脚本来包装具有数百个选项的程序的用户喜欢简短的选项。

如果一个程序变得足够大,它最终会用完与规范选项相比有意义的短选项组合。例如,-Z 可能与以完全不同的字母开头的长选项相同。到了这个时候,尤其是对于单个维护者来说,选项解析代码就变得维护起来很头疼。

发生这种情况时,您有几种选择:

  • 使用 gengetopt 之类的东西来编写该代码从模板中为您
  • 提供 只使用长选项(通常是一个坏主意)
  • 尝试将您的程序减少到 52 个选项(az AZ)(通常是一个坏主意)
  • 实现短选项只是成为不带参数的开关的选项,使用对于那些这样做的人来说,有很长的选择
  • 一系列对你来说非常有意义但对用户来说没什么意义的其他方法

混合在不同的环境中,你真的开始感受到痛苦。

当我坐下来编写一个需要很多选项的工具时,我通常做的第一件事就是编写代码来解析参数,这有助于规划程序的流程并成为一个大纲。之后你只需让每个选项都起作用即可。

换句话说,如果你的选择变成了一种长期的痛苦,这通常表明一个项目很快就超出了它的计划。

不管怎样,为了结束我冗长的回答,通常最好尽可能保持兼容的 getopt() 行为。从用户那里获取指令的代码只是开展业务的成本,因此您应该完全关注如何在可能的情况下带来更好的用户体验。

In my opinion, the following things are usually true:

  • Users love long, natural language options because they are easy to remember.
  • Users who write scripts to wrap programs that have hundreds of options love short options.

If a program gets big enough, it will eventually run out of short option combinations that make any sort of sense when compared to the canonical option. For instance, -Z might be the same as a long option that begins with a totally different letter. At that point, especially for a single maintainer, the option parsing code becomes a headache to maintain.

You have a few choices when that happens:

  • Use something like gengetopt to write that code for you from a template
  • Use only long options (usually a bad idea)
  • Try and keep your program down to 52 options (a-z A-Z) (usually a bad idea)
  • Implement options where short options just become switches that don't take arguments, use long options for those that do
  • A flurry of other methods that make perfect sense to you and little sense to users

Mix in different locales, and you really begin grasping the pain.

When I sit down to write a tool that takes lots of options, the first thing I usually do is write the code to parse the arguments, this helps to plan the flow of the program and becomes an outline. You just make each option work after that.

In other words, if you get to the point where options become such a chronic pain, its usually indicative of a program that quickly evolved beyond its planning.

Anyway, to bring my long winded answer to a close, its usually better to keep the compat getopt() behavior whenever possible. Code that gets instructions from the user is just the cost of doing business, so your concern should be fully on what makes for a better user experience, when possible.

岁月无声 2024-08-27 06:49:17

我想说,对于“正确的 gnu/linux 风格”,您应该使用 getopt_long() 并为大多数选项提供长选项(有时仅提供长选项)。大多数命令行界面都遵循这一点。

I'd say for "proper gnu/linux style" you should use getopt_long() and provide long options for most options (and sometimes long option only). Most command line interfaces follow this.

橘寄 2024-08-27 06:49:17

两者都不。用户 argp_parse 或 libpopt。

Neither. User either argp_parse or libpopt.

多孤肩上扛 2024-08-27 06:49:17

我从未使用过 getopt_long_only,但似乎它必须进行更多查找,因为如果未知标志以单个 - 开头,它必须同时查看长选项和短选项:

如果选项以“-”开头(不是
'--') 与长选项不匹配,但是
确实匹配一个短选项,它是
改为解析为短选项。

-- man getopt_long_only

I've never used getopt_long_only, but it seems like it would have to do more lookups, as it has to look at both the long and short options if an unknown flag starts with a single -:

If an option that starts with '-' (not
'--') doesn't match a long option, but
does match a short option, it is
parsed as a short option instead.

-- man getopt_long_only

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文