I have to input the parameters from the command line i.e username, password, and database name. I know how to do that without using flags, by using sys.argv like below:
##Test.py
hostname = str(sys.argv[1])
username = str(sys.argv[2])
password = str(sys.argv[3])
def ConnecttoDB():
try:
con=sql.connect(host=hostname, user= username, passwd= password)
print ('\nConnected to Database\n')
# If we cannot connect to the database, send an error to the user and exit the program.
except sql.Error:
print ("Error %d: %s" % (sql.Error.args[0],sql.Error.args[1]))
sys.exit(1)
return con
So, it could be run as:
$test.py DATABASE USERNAME PASWORD
But the problem is that I have to use 'flags'. So, the script could be run like this:
$test.py -db DATABSE -u USERNAME -p PASSWORD -size 20
How can I use flags to take arguments from the command line?