Mercurial > hg > sqlex
comparison sqlex/main.py @ 1:1cfdb859f9d2
skeleton
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 31 Mar 2017 20:31:31 -0700 |
parents | a05c70cc24be |
children | 63a75d318b06 |
comparison
equal
deleted
inserted
replaced
0:a05c70cc24be | 1:1cfdb859f9d2 |
---|---|
6 """ | 6 """ |
7 | 7 |
8 # imports | 8 # imports |
9 import argparse | 9 import argparse |
10 import os | 10 import os |
11 import sqlite3 | |
11 import sys | 12 import sys |
12 | 13 |
13 def ensure_dir(directory): | 14 def ensure_dir(directory): |
14 """ensure a directory exists""" | 15 """ensure a directory exists""" |
15 if os.path.exists(directory): | 16 if os.path.exists(directory): |
25 | 26 |
26 def __init__(self, **kwargs): | 27 def __init__(self, **kwargs): |
27 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) | 28 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) |
28 kwargs.setdefault('description', __doc__) | 29 kwargs.setdefault('description', __doc__) |
29 argparse.ArgumentParser.__init__(self, **kwargs) | 30 argparse.ArgumentParser.__init__(self, **kwargs) |
30 self.add_argument('db', help="sqlite `.db` file") | 31 self.add_argument('db', |
32 help="sqlite `.db` file") | |
33 self.add_argument( | |
31 self.options = None | 34 self.options = None |
32 | 35 |
33 def parse_args(self, *args, **kw): | 36 def parse_args(self, *args, **kw): |
34 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | 37 options = argparse.ArgumentParser.parse_args(self, *args, **kw) |
35 self.validate(options) | 38 self.validate(options) |
36 self.options = options | 39 self.options = options |
37 return options | 40 return options |
38 | 41 |
39 def validate(self, options): | 42 def validate(self, options): |
40 """validate options""" | 43 """validate options""" |
44 | |
45 try: | |
46 open(options.db) | |
47 except Exception as e: | |
48 self.error("Could not open '{}': {}".format(options.db, e)) | |
41 | 49 |
42 def main(args=sys.argv[1:]): | 50 def main(args=sys.argv[1:]): |
43 """CLI""" | 51 """CLI""" |
44 | 52 |
45 # parse command line options | 53 # parse command line options |