diff sqlex/main.py @ 5:3a7f515571dc

can list column names
author Jeff Hammel <k0scist@gmail.com>
date Sat, 01 Apr 2017 11:11:28 -0700
parents b440206930ac
children 22fbe50d92e8
line wrap: on
line diff
--- a/sqlex/main.py	Sat Apr 01 09:28:49 2017 -0700
+++ b/sqlex/main.py	Sat Apr 01 11:11:28 2017 -0700
@@ -7,6 +7,7 @@
 
 # imports
 import argparse
+import csv
 import os
 import sys
 from .model import SQLEx
@@ -36,6 +37,9 @@
         self.add_argument('--tables', '--list-tables', dest='list_tables',
                           action='store_true', default=False,
                           help="list tables and exit")
+        self.add_argument('--columns', '--list-columns', dest='list_columns',
+                          action='store_true', default=False,
+                          help="list columns in `table` and exit")
         self.options = None
 
     def parse_args(self, *args, **kw):
@@ -52,6 +56,8 @@
         except Exception as e:
             self.error("Could not open '{}': {}".format(options.db, e))
 
+        if options.list_columns and not options.table:
+            self.error("`--list-columns` requires `table`")
 
 def main(args=sys.argv[1:]):
     """CLI"""
@@ -74,5 +80,15 @@
             retval = int(options.table not in tables)
         return retval
 
+    if options.table:
+        # ensure selected table exists
+        if options.table not in db.tables():
+            parser.error("No table '{}' in {} tables:\n{}".format(options.table, options.db, ', '.join(db.tables())))
+
+    if options.list_columns:
+        # list columns and return
+        print ('\n'.join(db.columns(options.table).keys()))
+        return
+
 if __name__ == '__main__':
     main()