What's the difference between one-dash and two-dashes for command prompt parameters?

37,879

Solution 1

Just do ls --help and look at the options and then it will be clearer.

It has nothing to do with parameters at all. Many options have a short form and a long form, and many have one and not the other.

And also, regarding parameters, it's simply that in the long form when they take a parameter it looks like it's always with an equals. But obviously short ones can take parameters just as much; just they don't use an equals.

Here is an extract from ls --help (man ls gives equivalent information). Notice how some have a long form without a short form (--author, --block-size), some have a short form without a long form (-c, -f, -g), and some have both a long form and a short form (-A/--almost-all, -b/--escape).

 -a, --all                  do not ignore entries starting with .
 -A, --almost-all           do not list implied . and ..
     --author               with -l, print the author of each file
 -b, --escape               print octal escapes for nongraphic characters
     --block-size=SIZE      use SIZE-byte blocks
 -B, --ignore-backups       do not list implied entries ending with ~
 -c                         with -lt: sort by, and show, ctime (time of last
                              modification of file status information)
                              with -l: show ctime and sort by name
                              otherwise: sort by ctime
 -C                         list entries by columns
     --color[=WHEN]         control whether color is used to distinguish file
                              types.  WHEN may be `never', `always', or `auto'

Solution 2

There is no widespread standard. There's some consistency e.g. in GNU programs, but you need to check each program's documentation.

Quoting Wikipedia, emphasis mine:

In Unix-like systems, the ASCII hyphen–minus is commonly used to specify options. The character is usually followed by one or more letters. An argument that is a single hyphen–minus by itself without any letters usually specifies that a program should handle data coming from the standard input or send data to the standard output. Two hyphen–minus characters ( -- ) are used on some programs to specify "long options" where more descriptive option names are used. This is a common feature of GNU software.

Usually, hyphens indicate a predefined argument. I think it's used to differentiate them from e.g. file names or other labels you might use as arguments. That's not always the case, though (see below).


You'll often find the same argument available both as short and long option, as e.g. in ls.

Some programs use a single hyphen for one-character options, and two hyphens for multi-character options, but not all (GNU find comes to mind). Some programs have optional hyphens or skip them altogether (tar or BSD ps come to mind).

Sometimes long options (--foo) require arguments, while short options (-f) don't (or at least imply a specific default argument).

Short options (e.g. cut -d ' ') can have arguments , while long options (e.g. ls --all) don't necessarily have them.

To set a particular behavior of a program, you sometimes need to use a short option, for others you need to use a long option, and for some you have a choice.

On a related note, some programs can handle no whitespace between an option and its argument, while others can't.

As I wrote at the beginning, there just is no common behavior or standard. Often you can trace similar behavior to the same library used for argument parsing, but you probably don't want to read the sources to find this out.

You really cannot infer one program's argument syntax from another's.


If you also consider Windows, it gets even worse: While the Windows command line calls traditionally use /f (at least most of the time, single characters) for options, with : as the separator between options and their value (see e.g. here); cross-platform utilities are widespread (such as those you mention) and bring along the more common hyphen syntax for arguments, with all the inconsistencies mentioned above.

Solution 3

This is a convention coming from *nix. Double hyphen precedes options when they are written in full, , while single hyphen precedes options when they are written in short form. For example ls --all --l, can be shortened to ls -al. As it is seen, not all options have their single letter equivalents, although more used ones usually do.

Whether the option takes an argument doesn't really make a difference - it can either take them, or not take them, regardless of the way you enter the option.

When writing them for one time usage, it doesn't really matter, but when writing commands for example in .alias files, it is customary to use the full-form. Purely for the ease of reading for the next person.

Solution 4

single dash is implemented by the getopt and is posix standard function, double dash in getopt_long and is a gnu standard.

Traditionally, a single dash supplies a single character option like this:

-A or -V etc but need not be limited to that. e.g. -Wall for gcc turns on all compiler warnings for the gcc compiler command.

double dash arguments tend to be more verbose and often take a supplied parameter like --max-count=NUM. However, --version takes no equals.

Generally speaking, there are no rules or defined standards around how program arguments should be specified, just a bunch of traditions. However, if the getopt and getopt_long command line parsing functions are used then the parameters should generally follow the standard as the library functions enforce a certain way of doing it.

Share:
37,879

Related videos on Youtube

Pacerier
Author by

Pacerier

# 9

Updated on September 18, 2022

Comments

  • Pacerier
    Pacerier over 1 year

    I was wondering why is it that some programs requires their command prompt parameters to have two dashes in front whereas some (most) only require one dash in front?

    For example most programs look like this: relaxer -dtd toc.xml toc_gr.xml toc_jp.xml

    Whereas some programs look like this: xmllint --valid toc.xml --noout

    What's the reason that some requires two dashes instead of one? Doesn't it make sense for everyone to stick to one standard (i.e. a single dash will do).

    • Admin
      Admin over 12 years
      > Doesn't it make sense for everyone to stick to one standard – yes. Do all programmers stick to standards and keep consistency? No. Many programmers can't even maintain consistency within their programs :) That being said, the consensus would be to use one dash only for one-letter options, and two dashes for all that are actually words, e.g. -i vs. --input or -n --dry-run.
    • Admin
      Admin over 12 years
      @slhck Heys thanks for the help =) By that convention then, does it mean that the -dtd should really have been --dtd ? Effectively what I was wondering about is what does the dash (and double-dash) trying to signify ?
    • Admin
      Admin over 12 years
      For bonus points: programs conforming to the Gnu standards for --long-options will accept any unique abbreviation as well. So, for a program with options --file-in and --file-out, you could use --file-o=foo or --file-i=foo, which can save some typing for --very-long-optional-parameters.
    • Admin
      Admin over 5 years
      GNU came along with their convention of using two dashes for "long" options, which I happen to prefer, but many older utilities, such as those bundled with the X Window System, as well as ImageMagick (e.g., convert, mogrify) have "long" options using only a single dash. For example: xterm -fn 6x10 -geometry 80x24+30+200. Abbreviations are supported, provided they're distinct (e.g., -g or -geom for -geometry). See X(7) for other examples.
  • Admin
    Admin over 12 years
    Also, in many cases with single letter options following a single dash, you can group the letters together. For example, "ls -al" is the same as "ls -a -l". Double-dash options cannot be combined in this way. Single-letter options are an older standard, but these days many commands will take both types. E.g. "ls --all" is the same as "ls -a".
  • Admin
    Admin over 12 years
    Actually, another correction. Single-dash single-letter options can take a parameter, but it's usually not linked with an equals sign. For example, "tail -n 50" shows the last 50 lines of a file, equivalent to "tail --lines=50".
  • Admin
    Admin over 12 years
    By this convention, does it mean that --noout (in the question above) should really have been written as -noout since it doesn't have arguments ?
  • Rook
    Rook over 12 years
    @Pacerier - Unix is a trade name. By typing *nix I actually refer to all unix and similar systems, which for all practical purposes, are the same. But mostly, it's a force of habit ...
  • BRPocock
    BRPocock over 12 years
    It should be noted that the GNU standard libraries provide the functionality of mapping the one-minus-one-letter, two-minus-multi-letter options, so all new GNU programs and most new free software in general uses this notation (e.g. -f, -f foo, --file, --file foo, --file=foo, --fil=foo, --fi=foo); throwbacks such as tar, ps, find and such had their command-line syntax set by POSIX before this standard had completely gelled. On a Gnu/Linux system, it's a reasonably safe bet that at least --help will (almost) always be supported, as well as man <command> or info <command>
  • Admin
    Admin over 12 years
    what is said in this answer about parameters is totally wrong. The first sentence is obvious. The last sentence is obvious. And the paragraph in the middle that should answer the question, is totally wrong.
  • Admin
    Admin over 12 years
    @RandyOrrison when a single letter i've never seen an equals sign.. and to elaborate on your point, they often take parameters.. wget -w 10 head -n 3 cut -b 2 And look at Ping -? !! Loads take parameters. This answer is terrible
  • Pacerier
    Pacerier over 12 years
    How do we do a ls --help on windows?
  • HikeMike
    HikeMike over 12 years
    @Pacerier There is no ls on Windows. The equivalent command would be dir /?. You are using cross-platform software that violates the usual Windows conventions, see my answer.
  • barlop
    barlop over 12 years
    @Pacerier It's not built in, but download it 3rd party, Gnuwin32 you download it(google gnuwin32). there are a bunch of packages within gnuwin32 each with commands, download the coreutils package, that has many common commands.
  • Admin
    Admin over 11 years
    The -- variation is not conventional in Unix. Support for it was first provided in the GNU version of clib, and implemented in many GNU command-line tools, all of which became standard on Linux (or "GNU/Linux" if you insist on that term). Some Unix implementations (such as Solaris) have implemented this convention; others (BSD, OS X) have not.
  • Dan
    Dan over 10 years
    There are long options without short options in your ls --help excerpt. See --author and --block-size.
  • barlop
    barlop over 10 years
    does the GNU standard say that you shouldn't have a short form without a long form and shouldn't have a long without a short form?
  • Ryan_S
    Ryan_S over 8 years
    +1 for mentioning shorthand command combining. that's the real reason why there's two styles.
  • user3155703
    user3155703 about 8 years
    To answer the question in @Pacerier's original comment: The most common way modern Windows systems get the ls command is via GIT. The GIT bash shell or CMD window with GIT bin in the PATH will have ls.
  • barlop
    barlop about 8 years
    @yzorg does git use ming?
  • user3155703
    user3155703 about 8 years
    @barlop I'm no expert, but looks to be so, msys-1.0.dll and subset of mingw.
  • TheDudeAbides
    TheDudeAbides over 5 years
    +1 for being the most complete answer, including mention of GNU find which does not follow the "GNU convention" for long options (probably for reasons of POSIX compliance, as @BRPocock pointed out). I would +1 again if I could, for mentioning DOS/Windows command line "switches," since the OP's question somehow got tagged "windows," and that convention should be mentioned for completeness.
  • Larryc
    Larryc over 5 years
    ls is present in powershell, you don't need to install anything.
  • barlop
    barlop over 4 years
    @Larryc that's a good point, I don't yet use powershell much but indeed there is one in powershell, though with that one you can't do man ls, or ls --help, whereas with cygwin's one you can. Apparently ubuntu is in some way available on windows 10 'store' tutorials.ubuntu.com/tutorial/tutorial-ubuntu-on-windows#0
  • luckyguy73
    luckyguy73 almost 3 years
    "it should be obvious to you." thats a dick remark, unnecessary
  • barlop
    barlop almost 3 years
    @luckyguy73 you have taken those words out of context to give a wrong impression . I wrote "Just do ls --help and look at the options; it should be obvious to you." but I will adjust what I wrote so eomebody like you can't do what you just did Certainly after doing a particular thing, some things can become obvious, that's reality. Applies to you or me or anybody. That's different from simply saying something should be obvious. Nevertheless I will adjust it so somebody like you can't distort what I wrote or the meaning.