Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When implementing command line flags, should I prefix with a fowardslash (/) or hyphen (-)?

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?

like image 532
Connell Avatar asked Mar 28 '13 13:03

Connell


People also ask

What does forward slash mean in CMD?

The forward slash (or simply slash) character (/) is the divide symbol in programming and on calculator keyboards. For example, 10 / 7 means 10 divided by 7. The slash is also often used in command line syntax to indicate a switch. For example, in the DOS/Windows Xcopy statement xcopy *.

What is the hyphen for in a command prompt?

The default is standard input. If no file is specified or if the - (hyphen) is specified as the last file name, the hyphen command reads standard input. The hyphen command can be used as a filter. Note: The hyphen command cannot read hyphenated words that are italic or underlined.

What are flags in command line?

Flags modify the operation of a command and are sometimes called options. A flag is set off by spaces or tabs and usually starts with a dash (-). Exceptions are ps, tar, and ar, which do not require a dash in front of some of the flags. For example, in the following command: ls -a -F.

How do I escape from command prompt?

Use a double backslash as the escape character for backslash.


1 Answers

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/

like image 165
ken Avatar answered Nov 15 '22 14:11

ken