Search code examples
pythonpython-3.xargparse

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?


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")