getopt_long 与 getopt_long_only
要制作一个合适的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,以下几点通常是正确的:
如果一个程序变得足够大,它最终会用完与规范选项相比有意义的短选项组合。例如,-Z 可能与以完全不同的字母开头的长选项相同。到了这个时候,尤其是对于单个维护者来说,选项解析代码就变得维护起来很头疼。
发生这种情况时,您有几种选择:
混合在不同的环境中,你真的开始感受到痛苦。
当我坐下来编写一个需要很多选项的工具时,我通常做的第一件事就是编写代码来解析参数,这有助于规划程序的流程并成为一个大纲。之后你只需让每个选项都起作用即可。
换句话说,如果你的选择变成了一种长期的痛苦,这通常表明一个项目很快就超出了它的计划。
不管怎样,为了结束我冗长的回答,通常最好尽可能保持兼容的 getopt() 行为。从用户那里获取指令的代码只是开展业务的成本,因此您应该完全关注如何在可能的情况下带来更好的用户体验。
In my opinion, the following things are usually true:
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:
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.
我想说,对于“正确的 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.
两者都不。用户 argp_parse 或 libpopt。
Neither. User either argp_parse or libpopt.
我从未使用过 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
-
:--
man getopt_long_only