11

The following works:

git -C ~/dotfiles status

But this fails:

git status -C ~/dotfiles

Why is this?

1 Answer 1

31

This is because -C is a global option, and doesn't "belong" to the status action. This is a common pattern, resulting in synopses like the one below:

command [global options] action [action-specific options]

git --help lists Git's global options, and man git goes into more detail.

7
  • 18
    It might be worth mentioning that every program defines its own option syntax, so this shouldn't be taken as a universal rule about options. Some of them can be moved around freely, some can only appear in certain places in the command line, and some can only show up in a specific order or a specific position. Commented Aug 14, 2020 at 8:09
  • 3
    And these decisions are often made by the library a program uses to parse its arguments. For example, GNU ls (using GNU getopts) treats - in ls foo -l as the long output option. BSD ls (using a different library), on the other hand, treats it as a filename argument. Commented Aug 14, 2020 at 17:24
  • 1
    @DavidZ well, kind of, at least for the standard UNIX tools it is standardized that options go before arguments in POSIX (with some exceptions). So, in general, it's likely that programs support at least that, although many GNU tools also allow options after arguments (as well as long opts, etc.) Commented Aug 14, 2020 at 23:53
  • @ljrk Yeah, but there are also things like git status -u which can't be flipped around to git -u status. And I figure a person who would ask this question might not be too familiar with command-line argument conventions in general, and so if they see something like du -s dir/ -h (GNU du of course) they might try to extract more meaning from that argument ordering than there really is. I just think it's easy to see one pattern or a few examples from one program and think all others work the same way, which is why I'd favor making the point that it really is up to the program. Commented Aug 15, 2020 at 3:46
  • 1
    @DavidZ Yeah it is, this wasn't meant as a critique, just an addendum :) Commented Aug 15, 2020 at 8:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.