20

Are their any conventions (either written or just generally understood) for when to use a forward slash (/) or a hyphen (-) when reading arguments/flags from a command line?

C:\> myprogram.exe -a
C:\> myprogram.exe /a

The two seem to be interchangeable in my experience, but I haven't used enough command line tools to say I've spotted any rules or patterns.

Is there a good reason that either of them are used at all? Could I theoretically use an asterisk (*) if I wanted to?

Connell
  • 13,925
  • 11
  • 59
  • 92
  • both should work fine. you can inject your custom processing to parse your arguments. – David Mar 28 '13 at 13:39
  • 2
    On Windows, you can use /args. On Unix, Linux, and Windows, you should use -x for single-letter args and --xyz-abc for multi-letter args. – Nayuki Mar 28 '13 at 13:46
  • 4
    There's no hard-and-fast rule in windows BUT `-` is perfectly legal in a filename whereas `/` is not. Hence -option may be a filename, but /option isn't. Asterix is a comic-book character. `*` is asterisk. – Magoo Mar 31 '13 at 21:31
  • 1
    @PeterWright hahaha, I don't think anyone on here is stupid enough to get the two confused in that context.. But thanks for pointing out the filename :) – Connell Apr 02 '13 at 08:45

7 Answers7

15

You can (theoretically) use whatever you want, as the parameters are just strings passed to your command-line program.

Windows convention seems to prefer the use of the forward slash ipconfig /all, though there are programs that take a hyphen gacutil -i or even a sort-of environment variable syntax setup SKUUPGRADE=1.

*Nix convention seems to prefer the hyphen -v for single-letter parameters, and double hyphen --verbose for multi-letter parameters.

I tend to prefer hyphens, as they are more OS-agnostic (forward slashes are path delimiters in some OSes) and used in more modern Windows apps (nuget, for example).

Edit:

This would be a good place to recommend a library that does .NET command-line argument parsing: http://commandline.codeplex.com/

qxn
  • 17,162
  • 3
  • 49
  • 72
12

See also Command line options style - POSIX or what?.

The tradition in DOS and Windows is to use a forward slash, as in /a or /extend. The tradition of using -a comes from Unix (and possibly elsewhere).

There's a GNU standard in which a single dash is used for one-letter flags, like -e -d, and they can be merged into -ed (so -ed is equivalent to -e -d). Then many-letter switches need two dashes, as in --extend --display. Sometimes it's only necessary to write as much of the word as is sufficient to deduce what switch is meant, so for example --disp might be a short-hadn for --display if no other switch begins with the letters disp....

Community
  • 1
  • 1
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • +1 for mentioning the reason behind the double dash. I didn't realise multiple letters with a single dash (`-ed`) should be treated as multiple single letter switches. – Connell Mar 28 '13 at 13:54
  • Mercurial's hg, while strictly different in that it doesn't need slashes or slashes, is similar in that you only need type in enough characters to distinctly identify what you want (e.g. "hg st") – JerKimball Mar 28 '13 at 14:03
5

Usually it's / on Windows and -/-- on Unix systems for short/long options. But there's no rule for that, so it is actually up to you.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
5

Old DOS commands used the prefix / for optional parameters. Microsoft is now moving towards supporting the Posix use of - to denote parameters as documented in their PowerShell Command-Line Standard documentation. This is because their operating system now sees both \ and / as directory separator characters.

https://technet.microsoft.com/en-us/library/ee156811.aspx

Chris C.
  • 959
  • 1
  • 8
  • 17
  • Even the earliest versions of MS-DOS (that had a hierarchical filesystem, so ≥ 2.0) treated both / and \ as path separators in the relevant system calls (see the PATHCHRCMP routine in the [Microsoft source code release of MS-DOS 2.0](https://github.com/microsoft/MS-DOS/blob/04a3d20ff411409ab98474892b2bb1713bde0f7f/v2.0/source/FCB.ASM#L478), for example), it’s just that the command-line parsers in the shell and utilities did not (there is some sort of token separation business there, so xcopy/s/q is equivalent to xcopy /s /q not xcopy\s\q, but I’ve never seen it described precisely). – Alex Shpilkin Apr 13 '22 at 03:55
1

A leading forward-slash (/) is common among Windows apps. Single hyphens (-) are common for short options (those consisting of a single letter) in applications that are POSIX-compliant. Double hyphens (--) are common for long options in such applications.

See this link for POSIX info. Or, see this SO post.

Community
  • 1
  • 1
Jordan
  • 33
  • 3
  • Thanks! I was looking for the technical term for standalone `--` ('end of options') and these references pointed me toward what I needed – Allison May 20 '22 at 20:33
1

The convention is:

  • - for single-letter flags, and they can be chained (ex: rm -Rf is the same as rm -R -f)

  • -- for flags with more than one letter (ex: rm --recursive --force)

Using / is an old standard that Windows inherited DOS, and DOS from CP/M, and it should not be used for new software. It creates ambiguities on non-Windows systems and for the reader. Modern Microsoft tools such as .NET and PowerShell use - for flags, so even Microsoft doesn't want to use / anymore.

Aaron Franke
  • 3,268
  • 4
  • 31
  • 51
0

You can use anything you want, or no leading character at all. It is more about adhering to standards than anything. When other users use your application they will think to include a - or / when adding arguments because that is what they are used to.

JeremyK
  • 1,075
  • 1
  • 22
  • 45