Switch from optparse to argparse
authorBen Steffen <bds@mit.edu>
Mon, 25 Nov 2019 05:28:15 +0000 (00:28 -0500)
committerBen Steffen <bds@mit.edu>
Mon, 25 Nov 2019 06:40:52 +0000 (01:40 -0500)
optparse has been deprecated so use argparse instead

debian/changelog
invirt-images

index 8cc6288..ee86058 100644 (file)
@@ -1,3 +1,10 @@
+invirt-images (0.0.8) precise; urgency=medium
+
+  * Use argparse instead of optparse
+  * Change cli to resemble git
+
+ -- Ben Steffen <bds@mit.edu>  Mon, 25 Nov 2019 01:39:57 -0500
+
 invirt-images (0.0.7) precise; urgency=medium
 
   * Port to Python 3
index eeda870..1e77767 100755 (executable)
@@ -7,7 +7,7 @@ import string
 import tempfile
 import urllib.request
 import math
-import optparse
+import argparse
 
 from invirt import database
 
@@ -134,81 +134,71 @@ def reap_images():
             subprocess.run(['lvchange', '-a', 'ey', f'/dev/xenvg/{lv}'])
             subprocess.run(['lvremove', '--force', f'/dev/xenvg/{lv}'])
 
+def action_add_cdrom(args):
+    attrs = {key: vars(args)[key] for key in ('cdrom_id', 'description', 'mirror_id', 'uri_suffix')}
+
+    cdrom = database.CDROM(**attrs)
+    database.session.add(cdrom)
+    database.session.commit()
+
+    load_image(cdrom)
+
+def action_add_mirror(args):
+    attrs = {key: vars(args)[key] for key in ('mirror_id', 'uri_prefix')}
+
+    mirror = database.Mirror(**attrs)
+    database.session.add(mirror)
+    database.session.commit()
+
+def action_update(args):
+    if not args.names:
+        images = database.CDROM.query().all()
+    else:
+        images = [database.CDROM.query().get(arg) for arg in args.names]
+
+    for cdrom in images:
+        if cdrom is not None:
+            load_image(cdrom)
+
+def action_reap(args):
+    reap_images()
+
 def main():
     database.connect()
     database.session.begin()
 
-    usage = """%prog [options] --add [--cdrom] cdrom_id description mirror_id uri_suffix
-       %prog [options] --add --mirror mirror_id uri_prefix
-
-       %prog [options] --update [short_name1 [short_name2 ...]]
-       %prog [options] --reap"""
-
-    parser = optparse.OptionParser(usage=usage)
-    parser.set_defaults(verbosity=0,
-                        item='cdrom')
-
-    parser.add_option('-a', '--add', action='store_const',
-                      dest='action', const='add',
-                      help='Add a new item to the database')
-
-    parser.add_option('-u', '--update', action='store_const',
-                      dest='action', const='update',
-                      help='Update all cdrom images in the database with the latest version')
-    parser.add_option('-r', '--reap', action='store_const',
-                      dest='action', const='reap',
-                      help='Reap stale cdrom images that are no longer in use')
-
-    a_group = optparse.OptionGroup(parser, 'Adding new items')
-    a_group.add_option('-c', '--cdrom', action='store_const',
-                       dest='item', const='cdrom',
-                       help='Add a new cdrom to the database')
-    a_group.add_option('-m', '--mirror', action='store_const',
-                       dest='item', const='mirror',
-                       help='Add a new mirror to the database')
-    parser.add_option_group(a_group)
-
-    v_group = optparse.OptionGroup(parser, "Verbosity levels")
-    v_group.add_option("-q", "--quiet", action='store_const',
-                       dest='verbosity', const=0,
-                       help='Show no output from commands this script runs (default)')
-    v_group.add_option("-v", "--verbose", action='store_const',
-                       dest='verbosity', const=1,
-                       help='Show only errors from commands this script runs')
-    v_group.add_option("--noisy", action='store_const',
-                       dest='verbosity', const=2,
-                       help='Show all output from commands this script runs')
-    parser.add_option_group(v_group)
-
-    (options, args) = parser.parse_args()
-    if options.action is None:
-        print(parser.format_help())
-    elif options.action == 'add':
-        if options.item == 'cdrom':
-            attrs = dict(list(zip(('cdrom_id', 'description', 'mirror_id', 'uri_suffix'),
-                                  args)))
-            cdrom = database.CDROM(**attrs)
-            database.session.add(cdrom)
-            database.session.commit()
+    parser = argparse.ArgumentParser(description='Perform actions on the CD-ROM images in the database')
+    subparsers = parser.add_subparsers(help='Action to perform')
 
-            load_image(cdrom)
+    add_parser = subparsers.add_parser('add', help='Add new image to database')
+    add_parser.set_defaults(func=lambda args: add_parser.print_help())
+    add_subparsers = add_parser.add_subparsers()
+
+    add_cdrom_parser = add_subparsers.add_parser('cdrom')
+    add_cdrom_parser.add_argument('cdrom_id')
+    add_cdrom_parser.add_argument('description')
+    add_cdrom_parser.add_argument('mirror_id')
+    add_cdrom_parser.add_argument('uri_suffix')
+    add_cdrom_parser.set_defaults(func=action_add_cdrom)
+
+    add_mirror_parser = add_subparsers.add_parser('mirror')
+    add_mirror_parser.add_argument('mirror_id')
+    add_mirror_parser.add_argument('uri_prefix')
+    add_mirror_parser.set_defaults(func=action_add_mirror)
+
+    update_parser = subparsers.add_parser('update', help='Update images in database')
+    update_parser.add_argument('names', nargs='*', metavar='name', help='Shortnames of images to update')
+    update_parser.set_defaults(func=action_update)
+
+    reap_parser = subparsers.add_parser('reap', help='Reap old images in database')
+    reap_parser.set_defaults(func=action_reap)
+
+    args = parser.parse_args()
+    if 'func' in args:
+        args.func(args)
+    else:
+        parser.print_help()
 
-        elif options.item == 'mirror':
-            attrs = dict(list(zip(('mirror_id', 'uri_prefix'),
-                                  args)))
-            mirror = database.Mirror(**attrs)
-            database.session.add(mirror)
-            database.session.commit()
-    elif options.action == 'update':
-        if len(args) > 0:
-            images = [database.CDROM.query().get(arg) for arg in args]
-        else:
-            images = database.CDROM.query().all()
-        for cdrom in images:
-            if cdrom is not None:
-                load_image(cdrom)
-    elif options.action == 'reap':
-        reap_images()
 
 if __name__ == '__main__':
     main()