Reputation:
How can I make the -c switch not require input? and then if the -c switch is specified when running the test.py script it should run this if statement and create the database. I'm not an expert so I would appreciate a code example.
parser = argparse.ArgumentParser(prog='test.py', description='Testing Script')
parser.add_argument('-d', '--device', nargs= '*', type=str, help="Device IP")
parser.add_argument("-c", '--createDatabase', nargs= '?', help="Create Database")
parser = menu.parse_args()
if args.c:
print 'It Worked!, now creating database'
con1 = MySQLdb.connect("localhost","root","testing")
query = con1.cursor()
query.execute("SET sql_notes = 0; ")
query.execute("CREATE DATABASE IF NOT EXISTS test CHARACTER SET utf8 COLLATE utf8_general_ci;")
con1.commit()
con1.close()
Upvotes: 5
Views: 4776
Reputation: 526623
Use a store_true
action:
parser.add_argument("-c", '--createDatabase', action="store_true", help="Create Database")
This will make args.c
default to False
, and change to True
if the -c
option is present.
Upvotes: 7