1

So I am using argparse and have an argument called async. The following code works in Python 2 but not in Python 3.8 and newer

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--async", action='store_true')
ARG = parser.parse_args()
if ARG.async:
   print("Async")

Unfortunatelly async seems to be a new keyword and there is SyntaxError on the line with if statement. How to solve when I don't want to rename it?

VojtaK
  • 483
  • 4
  • 13
  • Even though this issue is solved from the info in the other post I linked, I'll share the relevant info here. You simply can do `vars(ARG)['async']` instead of `ARG.async` now, and the result is identical from what I can see. – Random Davis Sep 28 '22 at 17:41

1 Answers1

3

You can specify an alternate internal name using the "dest" parameter. You'll have to change the python code to use that new name, but the external "--async" signature does not change.

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--async", action='store_true', dest="use_async")
ARG = parser.parse_args()
if ARG.use_async:
   print("Async")
tdelaney
  • 73,364
  • 6
  • 83
  • 116