Python argparse: difference between -o and -option

I am trying to understand the package argparse

and I really cannot find an answer to this very simple question:

  • What is the difference between -a

    and --argument

    when adding an argument?

Sometimes I find both in one add_argument()

, like here:

parser.add_argument(
        "-f", "--file",
        help="file path"
        )

      

+3


source to share


3 answers


If both are specified, then argparse will store the value in long form ('argument') rather than short form ('a'). It will print the destination, so "-foo-bar" will be stored in foo_bar

.

You can combine short forms and use a single hyphen ( -af

), but unlike some * NIX commands, order is important: if your argument requires a value ( -f filename

), then the argument should appear as the final argument of a group of short arguments:

cmd -af my_file   # works
cmd -fa my_file   # does not work

      

Compare this to the monster tar(1)

that allows tar -cfC myfile.tar ./directory

and will correctly sort it as logically equivalent tar -c -f myfile.tar -C ./directory

. Also, some long forms allow '=' to be used when specifying an option tar -cf myfile.tar

rather than tar -c --file=myfile.tar

. You cannot use '=' in short forms.



Luckily python argparse doesn't care about '='. Use it or not, with short form or long form.

# All equivalent:
  cmd -af=my_file
  cmd -a -f my_file
  cmd -a -fmy_file
  cmd -afmy_file
  cmd --file my_file -a
  cmd --file=my_file -a

      

Does it matter? As the other answer suggests, the long form can help with readability. A short form can make your energy users happier.

+1


source


Sometimes it is easier to use long option names, especially with undefined names or when short options are hard to find, and sometimes vice versa. If you think this might be the situation, you can use both forms of options and let the user pick their favorite.

Do you prefer script.py -r1000

or script.py --repeat 1000

? I would say all about the taste, and if you are dealing with a lot of people using it, you might want to consider them.



What is the difference between -a

and --argument

when adding an argument?

As long as you add them to the function add_argument

, the answer is with -a

, you can stick the values ​​(if any) right after -a

, no space needed like in the example in the second paragraph. Aside, no.

0


source


An attempt to capture the spirit "-" against "-".

Just to be brief, both types of parameters are equally capable. I can't think of anything that you can do in one type but not in another (other than wrinkling single-hyphenated single characters, which is not usually a Python programmer).

  • -

    • Known as a short form parameter.
    • eg. -f filename

    • Use this for parameters that will either always be used or are easily understood by the user of your program.
    • This is usually always present.
  • --

    • Known as the name of the long-form parameter.
    • eg. --directory /root/foobar

    • This format is typically only used when there are multiple competing single characters for a parameter. For example. if your code accepts d

      and d

      , then it's better to mention long form options for both.
    • This type is not always present (unless you have a soft OCD to provide a short and long shape).
0


source







All Articles