-2

What are the general rules for knowing when command options are prefixed by -- as opposed to -?

Edit: An answer referencing a canonical source, such as steeldriver's is preferred. One suggested duplicate refers to Wikipedia which is okay but probably not as good as a GNU documentation link.

Second edit: I think the title is not my original so if it misleads, I'm sorry, it was not my edit. That probably explains why some comments direct me to the man page.

  • 3
    I don't understand the question, there's no rule, that's a developer's / developers' choice, it's totally up to the specific tool. – kos Dec 20 '15 at 20:22
  • 1
    You may find this a useful staring point: Program Argument Syntax Conventions – steeldriver Dec 20 '15 at 20:44
  • 2
    There can be no canonical source for this, since there are many software developers and all of them do not agree to any common convention. – muru Dec 20 '15 at 21:34
  • 1
    You should really rephrase the question, because as you can see every answer is addressing a different aspect of the topic. What do you want to know exactly? The literal answer to your question is "look at the man page", but you seem to be wanting to learn about the possible conventions and if there are differences in the usage. Can you clarify what you want to know exactly? – kos Dec 20 '15 at 21:51
  • 2
    @H2ONaCl I have said why in my comment. Canonical in terms of conventions would be a standard that defines such a convention, not just "conforming to orthodox or well-established" whatever. english.stackexchange.com/a/34926/93130 – muru Dec 20 '15 at 22:56
4

In general, the single dash - introduces single letter options, where the letter is a mnemonic for the name of the option.

The double dash -- introduces long option names, where the name of the option is a whole word.

AFAIK this convention was introduced in the 1980's by the FSF GNU utilities. Note that the POSIX standard recommends single dash options, except for a double dash to indicate the end of the option list.

See also

  • @muru Thanks for the edit, I wrote on my phone and it needed some cleanup! – andy256 Dec 20 '15 at 21:22
2

- is usually only followed by a single character. This is a heritage from Unix. -- is usually followed by multiple characters (generally words or sentences), this comes from the GNU project. Usually commands (mainly GNU utilities) come with two parameter types, - and --. For example, in the ls command, the -a and the --all parameters do the exact same thing, but one is an entire word and another is a single letter.

Having an entire word as a parameter may be helpful for scripts, for example, because developers can read them and interpret the commands' parameters more easily.

In the other hand, having a single letter parameter may become handy when you are in a terminal and want to run commands faster.

If you're in doubt about the parameters a command can take, you can try a few commands like:

command --help
command -?
man command

The man command will give a more detailed explanation, but some commands do not have man (manual) pages.

  • After reading steeldriver's link it seems that ls -lat is three options so it is equivalent to ls -l -a -t but ls --lat would be a single option, probably undefined as of today. – H2ONaCl Dec 20 '15 at 20:57
  • Hey, -lat and --lat are not the same. You can only pass ONE argument at a time using --. It is usually an entire word. The equivalent to -lat in the long form would be '-l --all -t'. There are no long versions for '-l' and '-t' in ls. – Eduardo Cola Dec 20 '15 at 21:14
  • Yes, I know, they're not the same. ls --lat is undefined (today) and when it becomes defined it will not be the same as ls -lat (probably) . – H2ONaCl Dec 20 '15 at 21:23
2

Generally a command line has three parts:

command -option argument 

So, - this one is known as option and -- this one is called long option. It basically almost do the same jobs, but you have to defy your options clearly with the later one.

Here are some example of - and -- you can use with the command ls

enter image description here

2

About arguments

Like @kos mentions, the use of either -, -- or no hyphen at all is up to the choice of the developer of the program.

The command to run the program itself is the first argument (index 0), the second argument (and further) is defined by the application.

An example in python:

if sys.argv[1] == "peanutbutter":
    print("monkey")

will print "monkey" if the application is called with:

<application> peanutbutter

If the code is:

if sys.argv[1] == "--peanutbutter":
    print("monkey")

The same will happen when the application is run with:

<application> --peanutbutter

As simple as that. Often, either the use of - or -- is conventional.

  • It's all up to the developer but the developer should know the convention before deciding to violate it. – H2ONaCl Dec 20 '15 at 21:31
  • 2
    @H2ONaCl Obviously, there are various opinions on "THE" convention, since all variants exist. It is a matter of style, the same as conventions on using capitals in classes and functions in coding. There is no law on this, so please don't suggest there is one size for all her. – Jacob Vlijm Dec 20 '15 at 21:32
  • I did not suggest one size fits all here. – H2ONaCl Dec 20 '15 at 21:50
  • 1
    @H2ONaCl the explanation is simple, you are looking for something there isn't. – Jacob Vlijm Dec 20 '15 at 21:51
  • 1
    @H2ONaCl then I suggest you re- read the section on "how to answer a question". And next time, please don't change the question after answers have been posted. – Jacob Vlijm Dec 20 '15 at 22:00

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