Read and compare argument with argv - Python
I want to pass an argument and from it make a condition, follow the example below:
import argparse
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("--database", required=True, help="path to the
database images")
args = vars(ap.parse_args())
if (args == "mnist"):
print('Hello!')
And then in the terminal I enter with:
python argv.py --database mnist
But my code simply does nothing. What am I doing wrong? How could I do this correctly?
Solution 1:
Calling vars
on the Argparse NameSpace gives you a dict. You should compare the database
argument now parsed as Key 'database'
in the dict returned by vars
:
if args['database'] == "mnist":
print('Hello!')