Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Argument with the name async

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading