0

In Python 3.* argparse what is the diffenrence between arguments starting with "-" and "--"?

mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • short and long options – Jean-François Fabre Apr 14 '19 at 19:30
  • What is short and long options? – mrgloom Apr 14 '19 at 19:30
  • `-v` vs `--version`, for example – cs95 Apr 14 '19 at 19:31
  • That's not just an argparse thing -- it's GNU convention, extending POSIX-specified convention. See the "Utility Syntax Guidelines" at the end of http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html for the POSIX-standardized parts, and https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html for the GNU-extended parts. – Charles Duffy Apr 14 '19 at 19:43
  • 1
    `argparse` generally follows, but does not strictly enforce the GNU conventions. The short can be strung together as shown in the answer. Long can't. Long, if provided, is used for the argument name (`dest`). Common `argparse` practice is to provide both for an argument - a short one and a more descriptive one (but it also recognizes abbreviations). – hpaulj Apr 14 '19 at 20:24
  • Questions where `argparse` is more lenient than expected: https://stackoverflow.com/questions/31127366/single-dash-for-argparse-long-options, https://stackoverflow.com/questions/52520309/short-form-of-argparse-option-permisive-of-many-characters – hpaulj Apr 14 '19 at 20:34
  • https://docs.python.org/3/library/argparse.html#option-value-syntax describes the essentials of `argparse` use. But the code's actually a bit more lenient than the docs. A short works in the '-a=foobar' syntax. – hpaulj Apr 15 '19 at 05:54

1 Answers1

1

A double hyphen (--) is just standard for a longer argument.

E.g. ls --help gives the help for the ls.

Similarly, a single hyphen (-) is standard for shorter (usually one-letter) arguments.

E.g. ls -t lists directory contents sorted by date last modified.


It's up to the developer to decide which format their arguments will take, but it's down to the user to remember what options are available!

Note that there are no standards (that I am aware of), but in general more commonly used options are given single letter priority.

This also means that options can be strung together, e.g. ls -1tr lists the contents of a directory in only one column with most recent stuff at the bottom. Here the -1tr argument is parsed equivalently to three separate -1, -t and -r arguments. This would not [normally *] be implemented with double hyphened arguments.

* But note that it could be because its up to the developer to do what they like with their program.

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
  • There *are* relevant standards (well, one formal standard and one widely-adopted convention from an influential source); see links in my comment on the question. – Charles Duffy Apr 14 '19 at 19:44