Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Some programs will take options like this:

$ someprogram -orange apple

And other programs will use something like this:

$ otherprogram --orange apple

Is there a "rule" or convention for this in Linux/Unix/OSX?

share|improve this question

2 Answers 2

up vote 19 down vote accepted

The most common is GNU getopt style, with one dash for short options and two dashes for long options.


  • Initially, Unix programs took single-letter options preceded by a single dash and optionally bundled:

    ls -laF
    
    ls -l -a -F
    

    The two commands above are equal.

    When an option takes a value, it overrides bundling: In gpg -aofoo.gpg, -a and -o are options and foo.gpg is the value given to -o.

  • Most of them did, anyway.

    tar cvzf is a common sight (although current versions accept tar -cvzf too).

    BSD ps always uses -; SysV ps never does. The Linux version accepts both versions and changes its behavior depending on whether an option was prefixed with a dash.

  • X11 programs used long options preceded by a single dash or sometimes a plus sign:

    xterm -class FooTerm +vb -u8
    

    This sets options class and u8, and unsets the vb option.

    X11 style is incompatible with option bundling.

  • Later, long options were added to GNU getopt() in a way that's compatible with one-letter options.

    gpg -se --no-armor --output=signed.gpg
    

    This sets -s, -e, and --no-armor (which is opposite to --armor).

    Usually, --output=signed.gpg and --output signed.gpg are equivalent.

    (If I recall correctly, long options used + as the prefix before it was changed to --.)

  • The POSIX specification has a section Utility Argument Syntax, which describes the one-character options.


Most Windows programs use their own parsers, driving users mad.

  • Some require /a /b /c, others allow VMS-style /a/b/c, yet others prefer Unix-style /abc.
  • Most use / as prefix, some also accept -, others accept only -.
  • Values can be given as /foo bar, /foo=bar, /foo:bar.
  • Usually spaces can be /quoted "like this", but some programs take the " as a literal character. (This is a downside of letting the program do its own word-splitting; in Unix this is handled by the shell.)
  • Cross-platform programs may use an implementation of getopt.
share|improve this answer
1  
You forgot find - probably the most familiar use of single-dash long options. –  Dennis Williamson Mar 20 '11 at 21:56
    
@Dennis: Feel free to edit =) –  grawity Mar 20 '11 at 22:22
    
Epic answer. Thank you! –  Closure Cowboy Mar 20 '11 at 23:30
1  
You got System V and BSD ps backwards; consider that Linux refers to the --less options as BSD compatible, and OSX used a stock BSD ps until Tiger, when it added SVID style ps and then made it the default in Leopard with BSD style selectable by environment variable. And until I got used to it (having originated in the System III/V side of things) FreeBSD kept tripping me up with ps. –  geekosaur Mar 20 '11 at 23:37
    
Regarding quotes, what do you mean by "in Unix this is handled by the shell"? The program receives '"like' and 'this"' as arguments, only using e.g. getopts will fix this. Did you mean like\ this? –  Daniel Beck Mar 21 '11 at 5:45

Generally the first is found in older programs which are too well entrenched to change. Those kind of long options are incompatible with the standard getopt() function.

The second style was introduced by GNU getopt_long(), and is compliant with existing standards which expect the first style to be bundled short options (that is, -orange is expected to mean -o -r -a -n -g -e). This style of long option is strongly preferred.

share|improve this answer
2  
The convention as far as I have seen is to have all options available as --word and the most commonly used options as -x -y -z etc as well as --word –  Majenko Mar 20 '11 at 21:16
    
Right. That's supporting POSIX/SVID-style options as well as long options; the first style (used mainly by programs from the X11 distribution) isn't compatible. –  geekosaur Mar 20 '11 at 21:19
1  
Those pesky X11 options - they should be taken outside and shown the toe of my boot - repeatedly. –  Majenko Mar 20 '11 at 21:21
    
Thank you for the answer. +1 –  Closure Cowboy Mar 20 '11 at 23:31

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.