Are there any standard Command line conventions fo

2019-06-22 14:32发布

问题:

This question already has an answer here:

  • Are there standards for Linux command line switches and arguments? 4 answers

What are the command-line conventions regarding when to use 2 dashes, 1 dash, or simply no options at all and simply read inputs in order?

I realize there are many variants, but do any conventions stand out as the industry standard (say in Java, or C, or Python)?

回答1:

Read up on the background section of Python's optparse module, it answers some of your questions and exemplifies with some common argument formatting standards seen in the wild. The optparse module author recommends a style that roughly corresponds to the POSIX conventions for command line arguments, with the addition of --double-dashed-long-arguments which comes from the GNU coding standard.



回答2:

It depends on your taste.

Unix convention is that commands have 2 forms: long and short (one character). To indicate long form we use 2 dashes --. For example --install. Short form is marked with one dash, e.g. -i.

But there is no rules without exceptions. For example command line option of java itself do not follow this convention: -cp and -classpath mean the same and both are marked with one dash only. -version does not have short alias etc.

Slashes are used in windows applications.

I as a java developer prefer to use platform independent conventions (dashes). Moreover various libraries (like cli from jakarta project) supports dashes, so it is easier to implement.



回答3:

I agree with @Nishant, the single dash is a shorthand notation of a more verbose option.
See the ls Example given by Apache Commons CLI http://commons.apache.org/cli/usage.html#ls_Example