posix多值option_argument语法
我已阅读 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs /V1_chap12.html。
据我所知,简短的 option_argument
可能看起来像 -kvalue
或 -k value
。
如果我们想为 option_argument
指定多个值,我们可以编写 -kvalue1 -kvalue2
、-k value1 -k value2
或混合编写两种语法的。
我的问题:
-k value1 value2
有效且与上述相同吗?
此外,对于长 option_argument
又如何呢? --key value1 value2
相同吗?
I have read https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html.
I understand that a short option_argument
can look like -kvalue
or -k value
.
If we want to specify multiple values for the option_argument
, we can write -kvalue1 -kvalue2
, -k value1 -k value2
, or a mix of both syntaxes.
My question:
Is -k value1 value2
valid and equivalent to the above?
Further, what about for long option_argument
s?
Is --key value1 value2
the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-k value1 value2
通常等同于-k value1 -- value2
,其中value1
是 option_argument code>-k,并且value2
是一个操作数(程序参数,如echo value2
或ls value2< /代码>)。如果您通过操作
optind/OPTIND
来实现这一点,您将遇到以下问题:-k value1 value2 ... valueN input-file
中,您如何知道input-file
是一个输入文件,而不是用作-k input-file
?-k -z
是否意味着-k
有 0 个参数,后跟-z
选项,还是-z
> 被视为option_argument?那-k value1 value2 -z value4
呢?此外,
-k value1 -k value2
通常会将value1
替换为value2
,因此最好记录value1
的附加行为code>-k 如果已实现。您应该避免-(
和-)
选项。在 Solaris 上,根据其 CLIP 准则(向下滚动到命令语法标准:指南部分),(
和)
字符分隔长选项名称,例如?(help)V(version)
会将--help
识别为-?
并将--version
识别为 <代码>-V。至于长选项参数,POSIX 不支持它们,无论是
-old-style arg
、--gnu-style=arg
还是--gnu -样式arg
。根据 POSIX,optstring"-:k:"
不可移植,因为-
不是字母数字;即使支持,您还需要在--key value1
的情况下处理更改optind/OPTIND
以将键值设置为value1< /code> 因为
key
是-
选项的 option_argument。如果您只是编写一个实用程序,而不是在自己的选项解析器中实现
getopt
规则,则另一种选择是依赖getopt_long
,保留 实现差异 牢记;即使 POSIX 没有指定,它也可以广泛使用。您还可以使用单独的选项解析库(或 shell 函数或您正在处理的语言中使用的任何函数)。-k value1 value2
is usually equivalent to-k value1 -- value2
, wherevalue1
is the option_argument to-k
, andvalue2
is an operand (a program argument like inecho value2
orls value2
). Were you to implement this by manipulatingoptind/OPTIND
, you would have the following problems:-k value1 value2 ... valueN input-file
, how would you know thatinput-file
is an input file and not intended as-k input-file
?-k -z
mean-k
had 0 arguments, followed by the-z
option, or is-z
treated as an option_argument? What about-k value1 value2 -z value4
?Also,
-k value1 -k value2
would typically replacevalue1
withvalue2
, so it would be a good idea to document the additive behavior of-k
if it were implemented. You should avoid-(
and-)
options. On Solaris, per its CLIP guidelines (scroll down to the Command Syntax Standard: Guidelines section), the(
and)
characters delimit long option names, like?(help)V(version)
would recognize--help
as-?
and--version
as-V
.As for long option arguments, POSIX does not support them, whether that is
-old-style arg
,--gnu-style=arg
, or--gnu-style arg
. An optstring"-:k:"
is not portable according to POSIX either because-
is not alphanumeric; even if it is supported, you would also need to deal with changingoptind/OPTIND
in the case of--key value1
to set the key value tovalue1
sincekey
is the option_argument for the-
option.If you are merely writing a utility rather than implementing
getopt
rules in your own option parser, an alternative would be to rely ongetopt_long
, keeping the implementation differences in mind; it is widely available, even if it is not specified by POSIX. You could also use a separate option parsing library (or shell function or whatever you would use in the language you're dealing with).