Argument with the name async

Question:

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?

Asked By: VojtaK

||

Answers:

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")
Answered By: tdelaney
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.