posix多值option_argument语法

发布于 2025-01-20 16:40:23 字数 578 浏览 1 评论 0原文

我已阅读 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_arguments?
Is --key value1 value2 the same?

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

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

发布评论

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

评论(1

菩提树下叶撕阳。 2025-01-27 16:40:23

-k value1 value2 通常等同于 -k value1 -- value2,其中 value1option_argument code>-k,并且 value2 是一个操作数(程序参数,如 echo value2ls 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, where value1 is the option_argument to -k, and value2 is an operand (a program argument like in echo value2 or ls value2). Were you to implement this by manipulating optind/OPTIND, you would have the following problems:

  • In -k value1 value2 ... valueN input-file, how would you know that input-file is an input file and not intended as -k input-file?
  • Does -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 replace value1 with value2, 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 changing optind/OPTIND in the case of --key value1 to set the key value to value1 since key 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 on getopt_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).

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