]> Devi Nivas Git - chevron.git/commitdiff
Added nicer cli using argparse
authornoah morrison <noah@morrison.ph>
Wed, 19 Nov 2014 04:43:36 +0000 (23:43 -0500)
committernoah morrison <noah@morrison.ph>
Wed, 19 Nov 2014 04:43:36 +0000 (23:43 -0500)
chevron/chevron.py

index 23efd000edf78e4b5ffd4183c7e34005cb3bb151..28508461ddb19ddcfb2e61250c64115d296d67bb 100755 (executable)
@@ -398,10 +398,47 @@ def main(template, data=None, **kwargs):
 
 
 def cli_main():
-    try:
-        print(main(argv[1], argv[2]))
-    except IndexError:
-        print('usage: chevron [Json file] [Mustache file]')
+    """Render mustache templates using json files"""
+    import argparse
+    import os
+
+    def is_file(arg):
+        if not os.path.isfile(arg):
+            parser.error('The file {0} does not exist!'.format(arg))
+        else:
+            return arg
+
+    def is_dir(arg):
+        if not os.path.isdir(arg):
+            parser.error('The directory {0} does not exist!'.format(arg))
+        else:
+            return arg
+
+    parser = argparse.ArgumentParser(description=__doc__)
+
+    parser.add_argument('template', help='The mustache file',
+                        type=is_file)
+
+    parser.add_argument('-d', '--data', dest='json_file',
+                        help='The json data file',
+                        type=is_file)
+
+    parser.add_argument('-p', '--path', dest='partials_path',
+                        help='The directory where your partials reside',
+                        type=is_dir)
+
+    parser.add_argument('-e', '--ext', dest='partials_ext',
+                        help='The extension for your mustache\
+                              partials, \'mustache\' by default')
+
+    args = parser.parse_args()
+
+    kwargs = {
+        'partials_path': args.partials_path or '.',
+        'partials_ext': args.partials_ext or 'mustache'
+    }
+
+    print(main(args.template, args.json_file, **kwargs))
 
 if __name__ == '__main__':
     cli_main()