命令行参数,标准解析方法?

发布于 2024-12-11 18:00:44 字数 486 浏览 0 评论 0原文

我一直在阅读一些有关程序如何处理命令行参数的内容。但这些信息似乎“不完整”,我读过:

  • 选项前面可能有一个“-”或“/”符号。
  • 选项可以有附加参数(不带 - 号),
  • 选项参数直接跟在选项后面,带或不带空格。
  • 选项可以是单个字母或完整单词。
  • 选项可以合并到单个“选项”中: -abc 等于 -a -b -c

(来源)

现在我真的很想知道:什么样的选项你给了“-”号,哪些不给。 另外,将选项合并为 1 似乎与全字选项不兼容? “-file”可以是一个完整的单词,但也可能表示“-f”、“-i”、“-l”、“-e”、4 个不同的开关。或者甚至:“-f”和“ile”作为选项参数。

我理解有问题吗?

I've been reading a bit about how programs handle command line parameters. But the information seems to be "incomplete", things I've read:

  • Options may have a preceding '-' or '/' sign if front of them.
  • Options can have additional arguments (which go without a - sign)
  • the option arguments follow the option directly, with or without a space.
  • Options can be a single letter or a full word.
  • optionscan be merged inside a single "option": -abc equals -a -b -c

(Source)

Now I'm really wondering: What kind of options do you give a "-" sign and which not.
Also the merging of options into 1 seems to be incompatible with full-word options? "-file" can be a full word, but it might also mean "-f", "-i", "-l", "-e", 4 different switches. Or even: "-f" with "ile" as option_argument.

Am I understanding something wrong?

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

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

发布评论

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

评论(2

高跟鞋的旋律 2024-12-18 18:00:44

在像 Linux 这样的系统上,约定完整单词的选项使用两个破折号(例如 --file),而单字母选项使用单个破折号(例如 -f.)

使用斜杠引入选项来自旧的 DOS,并保留在 Windows 中。

另外,如果一个选项使用整个单词,则不能将其拆分为多个选项。这是关于 -file 的示例:-file 可以是一个选项,也可以是四个不同选项(-f-i-l-e)。

总而言之,选项的外观或处理方式在程序之间差异很大,并且确实没有任何特定的标准。

我建议你找到一些你喜欢的方法,然后使用它。

On systems like Linux, there is the convention that options that are full words use two dashes (e.g. --file), while single-letter options use a single dash (e.g. -f.)

Using slash to introduce options is from old DOS, and is being kept in Windows.

Also, if an option is using a whole word, it can not be split into several options. This is in regards to your example with -file: -file can either be one option, or four different options (-f, -i, -l and -e).

All in all, how options looks, or is handled, differ very much between programs, and there really isn't any specific standard.

I would suggest you find some way you like, and then use that.

南薇 2024-12-18 18:00:44

选项/参数/参数等没有 Windows 标准。

选项是根据开发人员的理解开发的,以便他们轻松解释传入的信息。

/- 前缀选项的真正原因通常是为了解析规则,因为用更少的代码解析唯一的选项前缀比用大量代码解析复杂的选项要容易得多。通用开发标准(例如标准使用选项)可以最大程度地减少用户的困惑;)

There are no Windows standards for options/arguments/parameters etc.

Options are developed as per the developers understanding to make it easy for them to interpret the passed in information.

The real reason for / or - prefixed options is generally for parsing rules since it's much easier to parse unique option prefixes with less code rather than complex options with large code. Common development standards (such as standard use options) minimises confusion for users ;)

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