Xjames56X Xjames56X - 1 month ago 15
Git Question

how works options of git?

I am working on my own lib to parse argc/argv data. I was thinking about git. How git works ? I mean, for example in this example:
$>git commit -m "message"
commit is not an option... it didn't have any "-" in front of "commit"... Is there any place where this type of command line options is described correctly ?

Ohw, and I am quite sure that my lib will be worth than TCLAP or CLAP, but I don't care about it because I am not able to use an extern lib... :/

Thanks per avance

edit: link interesting --> http://softwareengineering.stackexchange.com/questions/70357/command-line-options-style-posix-or-what

Answer

Git appears to have different commands, so you can do git status, git add, etc. So from a user's point of view, git commit seems to be an invocation of the command git where commit is the first argument - when you're doing argc/argv, there's absolutely no rule saying that your arguments must begin with dashes or have some other format.

The interesting thing about Git is that it's also extensible. If you create, somewhere in your path, a script called git-hello (note single word, with a dash), then you can invoke it by git hello on the command line, making it indistinguishable from "real" Git commands.

You might guess from the above that there are actually separate programs called git-commit, git-status and so on, and that would be a correct guess. It's notable when browsing the man pages since you can do something like man git-commit. Git does in fact have an execution path where such scripts are located, and you can find it out by running git --exec-path. On my system, that's /usr/lib/git-core and there's a bunch of separate scripts and symlinks back to Git.

So Git is actually quite unlike most programs in terms of argument handling. You can see how other standard utilities handle their command line arguments in their source. For instance, 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 docs and is part of glibc.