git 个参数是如何解析的?

How are git arguments parsed?

我正在使用自己的库来解析 argc/argv 数据。我在想 git。 git 个参数是如何工作的?

示例:

git commit -m "message"

在这里,commit 不是一个选项,因为它没有连字符。有没有解释这种命令行选项行为的地方?

我知道我的库会比 TCLAP 或 CLAP 差,但我不允许使用外部库。

编辑:有趣 link -> https://softwareengineering.stackexchange.com/questions/70357/command-line-options-style-posix-or-what

Git 似乎有不同的命令,因此您可以执行 git statusgit add 等。所以从用户的角度来看,git commit 似乎是命令 git 的调用,其中 commit 是第一个参数 - 当您执行 argc/argv 时,绝对没有规则规定您的参数必须以破折号开头或具有其他格式。

Git 的有趣之处在于它也是可扩展的。如果您在路径中的某处创建了一个名为 git-hello 的脚本(注意单个单词,带有破折号),那么您可以在命令行上通过 git hello 调用它,使其与 [=38 无法区分=] Git 命令。

您可能会从上面猜到实际上有单独的程序称为 git-commitgit-status 等等,这将是一个正确的猜测。浏览 man 页面时值得注意,因为您可以执行类似 man git-commit 的操作。 Git其实是有这样的脚本所在的执行路径的,可以通过运行git --exec-path查到。在我的系统上,那是 /usr/lib/git-core 并且有一堆单独的脚本和符号链接回到 Git.

所以 Git 实际上在参数处理方面与大多数程序完全不同。您可以看到其他标准实用程序如何在其源代码中处理命令行参数。例如,this is the source for cat in its GNU implementation. If you go their main, you'll notice that the argument processing is via a getopt_long function, which is, along with getopt described in GNU docsglibc.

的一部分