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

独自空忆成欢 提交于 2020-12-04 20:01:23

问题


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?


回答1:


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/




回答2:


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.




回答3:


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....




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/15683347/when-implementing-command-line-flags-should-i-prefix-with-a-fowardslash-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!