pgsql/set-permissions: use argparse instead of optparse. v0.7.x
authorPascal Volk <user@localhost.localdomain.org>
Sat, 09 Feb 2013 17:24:12 +0000
branchv0.7.x
changeset 693 2e19ab98118a
parent 692 4f00a6de934c
child 694 b1bfd4d1d9c0
pgsql/set-permissions: use argparse instead of optparse.
pgsql/set-permissions.py
--- a/pgsql/set-permissions.py	Sun Jan 27 14:46:43 2013 +0000
+++ b/pgsql/set-permissions.py	Sat Feb 09 17:24:12 2013 +0000
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 # coding: utf-8
-# Copyright 2012, Pascal Volk
+# Copyright 2012 - 2013, Pascal Volk
 # See COPYING for distribution information.
 
 """
@@ -13,7 +13,7 @@
 import getpass
 import sys
 
-from optparse import OptionParser
+from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
 
 has_psycopg2 = False
 try:
@@ -32,13 +32,13 @@
     DBErr = PgSQL.libpq.DatabaseError
 
 
-def check_opts(opts, err_hdlr):
-    if not opts.postfix:
+def check_args(args, err_hdlr):
+    if not args.postfix:
         err_hdlr('missing Postfix database user name')
-    if not opts.dovecot:
+    if not args.dovecot:
         err_hdlr('missing Dovecot database user name')
-    if opts.askp:
-        opts.dbpass = getpass.getpass()
+    if args.askp:
+        args.dbpass = getpass.getpass()
 
 
 def get_dbh(database, user, password, host, port):
@@ -49,39 +49,34 @@
                          database=database, port=port)
 
 
-def get_optparser():
+def get_argparser():
     descr = 'Set permissions for Dovecot and Postfix in the vmm database.'
-    usage = 'usage: %prog OPTIONS'
-    parser = OptionParser(description=descr, usage=usage)
-    parser.add_option('-a', '--askpass', dest='askp', default=False,
+    parser = ArgumentParser(description=descr, usage='%(prog)s OPTIONS',
+                            formatter_class=ArgumentDefaultsHelpFormatter)
+    parser.add_argument('-a', '--askpass', dest='askp',
             action='store_true', help='Prompt for the database password.')
-    parser.add_option('-H', '--host', dest='host', metavar='HOST',
-            default=None,
+    parser.add_argument('-H', '--host', dest='host', metavar='HOST',
             help='Hostname or IP address of the database server. Leave ' +
                  'blank in order to use the default Unix-domain socket.')
-    parser.add_option('-n', '--name', dest='name', metavar='NAME',
+    parser.add_argument('-n', '--name', dest='name', metavar='NAME',
             default='mailsys',
-            help='Specifies the name of the database to connect to. ' +
-                 'Default: %default')
-    parser.add_option('-p', '--pass', dest="dbpass", metavar='PASS',
-            default=None, help='Password for the database connection.')
-    parser.add_option('-P', '--port', dest='port', metavar='PORT', type='int',
-            default=5432,
+            help='Specifies the name of the database to connect to.')
+    parser.add_argument('-p', '--pass', dest="dbpass", metavar='PASS',
+            help='Password for the database connection.')
+    parser.add_argument('-P', '--port', dest='port', metavar='PORT',
+            type=int, default=5432,
             help='Specifies the TCP port or the local Unix-domain socket ' +
                  'file extension on which the server is listening for ' +
-                 'connections. Default: %default')
-    parser.add_option('-U', '--user', dest='user', metavar='USER',
+                 'connections.')
+    parser.add_argument('-U', '--user', dest='user', metavar='USER',
             default=getpass.getuser(),
-            help='Connect to the database as the user USER instead of the ' +
-                 'default: %default')
-    parser.add_option('-D', '--dovecot', dest='dovecot', metavar='USER',
+            help='Connect to the database as the user USER instead of the ')
+    parser.add_argument('-D', '--dovecot', dest='dovecot', metavar='USER',
             default='dovecot',
-            help='Database user name of the Dovecot database user. Default: ' +
-                 '%default')
-    parser.add_option('-M', '--postfix', dest='postfix', metavar='USER',
+            help='Database user name of the Dovecot database user.')
+    parser.add_argument('-M', '--postfix', dest='postfix', metavar='USER',
             default='postfix',
-            help='Database user name of the Postfix (MTA)  database user. ' +
-                 'Default: %default')
+            help='Database user name of the Postfix (MTA)  database user.')
     return parser
 
 
@@ -155,15 +150,15 @@
 
 
 if __name__ == '__main__':
-    optparser = get_optparser()
-    opts, args = optparser.parse_args()
-    check_opts(opts, optparser.error)
-    dbh = get_dbh(opts.name, opts.user, opts.dbpass, opts.host, opts.port)
+    argparser = get_argparser()
+    args = argparser.parse_args()
+    check_args(args, argparser.error)
+    dbh = get_dbh(args.name, args.user, args.dbpass, args.host, args.port)
     versions = {}
     set_versions(dbh, versions)
     if versions['pgsql'] < 80400:
-        set_permissions(dbh, versions['dovecot'], opts.dovecot, opts.postfix)
+        set_permissions(dbh, versions['dovecot'], args.dovecot, args.postfix)
     else:
-        set_permissions84(dbh, versions['dovecot'], opts.dovecot, opts.postfix)
+        set_permissions84(dbh, versions['dovecot'], args.dovecot, args.postfix)
     dbh.commit()
     dbh.close()