Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why do console applications get the arguments started with either:

a) one dash (myapp -arg1 123; ls -al)
b) two dashes (myapp --arg1 123; git push origin master --force)
c) without dashes at all (myapp 123; man ls)
d) without dashes but with the equal sign (myapp arg1=123; dd if=/dev/zero)

Isn't there a standard convension? Even among the standard Linux applications all three cases a), b) and d) do exist at the same time. And it's tough to remember when I should use, for example, -help and when --help.

share|improve this question
2  
Related question: unix.stackexchange.com/questions/21852/… –  Barmar May 10 at 12:29

2 Answers 2

Dashes are used to denote options, which modify the behavior of the command. Arguments without dashes denote the main parameters of the command, often these are filenames.

Single hyphens usually introduce options consisting of just one letter. Multiple such options can be grouped together, so ls -a -l can be abbreviated as ls -al. This was the standard convention for most early Unix commands.

Double hyphens introduce options that are whole words. This convention is necessary to distinguish them from the grouping described above. This option style was popularized by the GNU versions of utilities, because they often had so many features that they ran out of mnemonic single letters.

Sometimes an option requires a parameter of its own. The styles of this vary: some commands use -o parameter, some use -oparameter, some use --option=parameter, and some allow multiple forms.

There are also a handful of commands that invented their own, ideosyncratic argument styles. These are typically very old commands, from before there was a concensus on argument conventions. Examples of this are tar and dd. find is also unusual in being an old command that used full word options before the -- convention was created; its arguments are practically a language of its own, because its needs don't fit into the typical command -options parameters paradigm.

Other reasons for variation among commands is that Unix did not originally have an argument parsing library function. It wasn't until well into its lifetime that the getopt and getopts functions were created. Using these libraries essentially forces you to follow common practices. But older programs did their own, ad hoc argument parsing, and different programmers made different decisions.

share|improve this answer

@Barmar is correct, but is missing a few points of information. Why this actually happens is solely because of how the program's code was written, and, more specifically, what was used to parse the arguments.

Before I talk more about that, I want to clear up some terminology. First of all, what you call "options" are actually also arguments. In fact, everything you type on the command-line is an argument (including the program's name). These arguments are then typically stored in an array (called argv in C). Then, the program chooses how to (or if to) parse these arguments and behave accordingly. Now, arguments typically take one of three shapes:

  1. flags (do not take an argument; simply turn a behavior on or off)
  2. switches (take an argument; modify behavior based on the argument)
  3. parameters (plain data not meant to modify behavior)

1 and 2 are often referred to as OPTIONS and are meant to change program behavior, but both appear in different styles (as also mentioned by Barmar). C's getopt library actually allows for a great deal of flexibility in this area. Though the convention is to have options be specified either as a single-letter preceded by a single hyphen or a full-word preceded by two options, programs written with getopt actually allow for any of the following to be equivalent (assuming help is given h as the index):

-h, --h, --help

However, -help is actually not allowed by getopt (so, if a tool uses -help for it's usage flag, then you can be pretty sure it wasn't written with the getopt library). This is because getopt interprets a single hypen to signal a list of combined options, so it interprets -help as -h -e -l -p. Though the getopt library is a widely-used standard now, many tools that predate it (such as tar) still use their own parsing setup, hence why tar -xjf is equivalent to tar xjf.


TL;DR: getopt hasn't always been around, so programmers had to parse arguments their own way. But, newer tools typically use it so their behavior is sane and predictable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.