Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all thanks to Mark for 'argparse' code. I have figured out the way to take arguments from command line specified by flags. I am pasting the code below which uses a modified 'Mark' code and a small module which shows how to use the arguments collected via commandline:</p> <pre><code>import argparse parser = argparse.ArgumentParser() #-db DATABASE -u USERNAME -p PASSWORD -size 20000 parser.add_argument("-host", "--hostname", dest = "hostname", default = "xyz.edu", help="Server name") parser.add_argument("-db", "--database", dest = "db", default = "ding_dong", help="Database name") parser.add_argument("-u", "--username",dest ="username", help="User name") parser.add_argument("-p", "--password",dest = "password", help="Password") parser.add_argument("-size", "--binsize",dest = "binsize", help="Size", type=int) args = parser.parse_args() print( "Hostname {} db {} User {} Password {} size {} ".format( args.hostname, args.db, args.username, args.password, args.binsize )) def ConnectToDB(): print ('Trying to connect to mySQL server') # Try to connect to the database try: con=sql.connect(host=args.hostname, user= args.username, passwd= args.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 </code></pre> <p>One of my concern was how to use variables from arguments to the code. For that two things need to be done </p> <ol> <li>Add 'dest' to the add_argument line e.x. dest ="username" which means the value collected from comamnd line will be assigned to variable 'username'</li> <li>Whenever an you use such command line assigned variables from 'dest' just add 'args' before variable e.x con=sql.connect(host=args.hostname, user= args.username, passwd= args.password)</li> </ol> <p>Done</p> <p>Thanks</p> <p>AK</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload