diff globalneighbors/cli.py @ 0:5dba84370182

initial commit; half-working prototype
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 12:03:39 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globalneighbors/cli.py	Sat Jun 24 12:03:39 2017 -0700
@@ -0,0 +1,41 @@
+"""
+command line interfaces
+"""
+
+import argparse
+from .read import read_cities
+from .schema import fields
+
+
+class ValidationParser(argparse.ArgumentParser):
+    """CLI option parser"""
+
+    def __init__(self, **kwargs):
+        kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
+        argparse.ArgumentParser.__init__(self, **kwargs)
+        self.add_arguments()
+        self.options = None
+
+    def add_arguments(self):
+        """Abstract Base Class:  add your arguments here"""
+
+    def parse_args(self, *args, **kw):
+        options = argparse.ArgumentParser.parse_args(self, *args, **kw)
+        self.validate(options)
+        self.options = options
+        return options
+
+    def validate(self, options):
+        """validate options"""
+
+
+class CitiesParser(ValidationParser):
+
+    def add_arguments(self):
+        self.add_argument('cities',
+                          type=argparse.FileType('r'),
+                          help="path to TSV cities file, as documented in http://download.geonames.org/export/dump/")
+
+    def read_cities(self):
+        assert self.options
+        return read_cities(self.options.cities, fields=fields)