Switch from optparse to argparse
[invirt/packages/invirt-base.git] / scripts / invirt-getconf
index 4721212..5b3681b 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 """
 invirt-getconf loads an invirt configuration file (either the original YAML
@@ -13,41 +13,34 @@ the YAML configuration; e.g., they cannot contain dots.)
 Examples:
 
   invirt-getconf db.uri
-  invirt-getconf authn.0.type
+  invirt-getconf hosts.0.ip
 """
 
-from invirt.config import load
-from sys import argv, exit, stderr, stdout
-from optparse import OptionParser
-
-class invirt_exception(Exception): pass
-
-def main(argv):
-    try:
-        parser = OptionParser(usage = '%prog [options] key',
-                description = __doc__.strip().split('\n\n')[0])
-        parser.add_option('-s', '--src',
-                default = '/etc/invirt/master.yaml',
-                help = 'the source YAML configuration file to read from')
-        parser.add_option('-c', '--cache',
-                default = '/var/lib/invirt/invirt.json',
-                help = 'path to the JSON cache')
-        parser.add_option('-r', '--refresh',
-                action = 'store_true',
-                help = 'force the cache to be regenerated')
-        parser.add_option('-l', '--ls',
-                action = 'store_true',
-                help = 'list node\'s children')
-        opts, args = parser.parse_args()
-
-        if len(args) > 1:
-            raise invirt_exception(__doc__.strip())
-        elif args and args[0]:
-            components = args[0].split('.')
+import argparse
+import sys
+import yaml
+
+import invirt
+
+
+class InvirtException(Exception):
+    pass
+
+
+def main():
+    parser = argparse.ArgumentParser(description='Get values from invirt configuration file')
+    parser.add_argument('-r', '--refresh', action='store_true', help='Force regenerate the cache')
+    parser.add_argument('-l', '--ls', action='store_true', help='List children of node')
+    parser.add_argument('path', nargs='?', default='', help='Path of value to get')
+
+    args = parser.parse_args()
+
+    components = args.path.split('.')
+
         else:
             components = []
 
-        conf = load(opts.src, opts.cache, opts.refresh)
+        conf = config.load(opts.refresh)
         for i, component in enumerate(components):
             progress = '.'.join(components[:i])
             if type(conf) not in (dict, list):
@@ -88,5 +81,3 @@ def main(argv):
 
 if __name__ == '__main__':
     exit(main(argv))
-
-# vim:et:sw=4:ts=4