title: How to add command line arguments with flags in Python3?
body: <p>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 <code>sys.argv</code> like below:</p>
  <pre><code>##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 (&quot;Error %d: %s&quot; % (sql.Error.args[0],sql.Error.args[1]))
          sys.exit(1)
  
      return con   
  </code></pre>
  <p>So, it could be run as:</p>
  <pre><code>$test.py DATABASE USERNAME PASWORD
  </code></pre>
  <p>But the problem is that I have to use 'flags'. So, the script could be run like this:</p>
  <pre><code>$test.py -db DATABSE -u USERNAME -p PASSWORD -size 20
  </code></pre>
  <p>How can I use flags to take arguments from the command line?</p>