C getopt -<整数>整数>
如何从命令行参数“tail -10”中获取选项-10。 getopt 函数查找 '1' 字符。但是我如何访问字符串“10”?
如果这可以通过 getopt_long 完成,那么一个例子会有所帮助。谢谢。
How do I get the option -10 from command line arguments- "tail -10". getopt function finds '1' character. But how do I access the string "10"?
If this can be done by getopt_long, an example would help. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非您打算将
-1
作为以0
作为参数的选项,否则答案是您不这样做。getopt
仅用于处理符合标准 POSIX 实用程序选项语法的选项。可以使用 GNU getopt_long 来实现此目的,或者您可以编写自己的 argv 解析器(这很简单)。编辑:实际上我想我误读了你想要的内容。如果您希望将
-
后跟任何数字解释为具有该数值的选项,我认为没有任何版本的getopt
可以工作。您无法将每个数字特殊情况下作为选项,如果您只是告诉getopt
所有数字都是带有参数的选项字符,-123
将被解释为参数为23
的-1
选项(这很好,您可以从那里解释它),但是一个单独的-1
将导致下一个argv
元素被eaten
作为-1
的参数,这是很难或不可能恢复的。Unless you intend for
-1
to be an option with0
as its argument, the answer is you don't.getopt
is only made for processing options that fit the standard POSIX utilities' option syntax. It may be possible to use GNUgetopt_long
for this purpose, or you could just write your ownargv
parser (it's easy).Edit: Actually I think I misread what you want. If you want
-
followed by any number to be interpreted as an option with that numeric value, I don't think there's any version ofgetopt
that will work. There's no way you can special-case every single number as an option, and if you simply tellgetopt
that all of the digits are option characters that take arguments,-123
will be interpreted as a-1
option with an argument of23
(which is fine, you can interpret it from there), but a lone-1
will cause the nextargv
element to geteaten
as an argument to-1
, which is difficult or impossible to recover from.这里发生的事情是,c 语言绝对没有关于处理命令行选项的正确方法的标准。
此外,unix 世界在相当长的一段时间内没有这样的标准(相信 中有一个关于它的完整部分) Unix 仇恨者手册(PDF 链接))。
因此,人们编写了临时处理机制。这些最终发展成为一个通用标准,
getopt
诞生了(在贝尔实验室)。后来我们得到了 GNUgetopt
和getopt_long
。然而,这些并不是必需的,有些程序仍然以自己的方式管理事物。但在事情稳定下来之前(有时是之后),人们会在处理选项的“通常”方式中添加新功能,只要它们看起来是个好主意。现在,很有可能
tail
会希望比任何其他功能更频繁地调整行数,因此使其简单 很少的击键来调整在当时看来是个好主意......What is going on here is that c the language comes with absolutely no standard about the proper way to handle command line options.
Moreover, the unix world had no such standard for quite some time ( believe there is a whole section about it in the Unix Haters Handbook (PDF link)).
So, people wrote ad hoc handling mechanisms. These eventually evolved toward a common(ish) standard and
getopt
was born (at Bell labs). Later we got the GNUgetopt
andgetopt_long
. These are not required, however, and some programs still manage things their own way.But before things settled down (and occasionally after), people added new features to the "usual" way of handling options whenever they seemed like a good idea. Now, it's a pretty good bet that
tail
will want to have the number of lines adjusted more often than any other feature, so making it easy and few keystrokes to adjust must have seemed like a good idea at the time...我从来没有那么喜欢 getopt,所以我做了平常的事情并重新发明了轮子。我将我的解决方案称为 argopt。您可以在 https://github.com/colding/argopt 获取源代码和手册页。我认为它比 getopt 更容易使用。
I never did like getopt that much, so I did the usual thing and reinvented the wheel. I call my solution argopt. You can get the source and manual page at https://github.com/colding/argopt. It is, I think, much easier to use than getopt.
在可能的情况下,我建议更改命令参数语法以避免数字参数,方法是将数字作为选项参数传递(例如
-n -5
),或者仅在-- 之后允许它
选项分隔符。对于无法更改参数语法的情况,BUGS BSD getopt(3) 手册页 的部分记录了一种实现此目的的方法。我调整了他们的解决方案以避免
optreset
,即 POSIX 未指定 且 glibc 不支持 :您可以将
getopt
替换为getopt_long
,并根据需要添加其他短或长选项。Where possible, I would recommend changing the command argument syntax to avoid numeric arguments, either by passing the number as an option argument (e.g.
-n -5
) or allowing it only after the--
option delimiter.For cases where changing the argument syntax is not possible, the BUGS section of the BSD getopt(3) man page documents a way to do this. I've adjusted their solution to avoid
optreset
, which is not specified by POSIX and not supported by glibc:You can replace
getopt
withgetopt_long
and add additional short or long options as necessary.