comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:5dba84370182
1 """
2 command line interfaces
3 """
4
5 import argparse
6 from .read import read_cities
7 from .schema import fields
8
9
10 class ValidationParser(argparse.ArgumentParser):
11 """CLI option parser"""
12
13 def __init__(self, **kwargs):
14 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
15 argparse.ArgumentParser.__init__(self, **kwargs)
16 self.add_arguments()
17 self.options = None
18
19 def add_arguments(self):
20 """Abstract Base Class: add your arguments here"""
21
22 def parse_args(self, *args, **kw):
23 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
24 self.validate(options)
25 self.options = options
26 return options
27
28 def validate(self, options):
29 """validate options"""
30
31
32 class CitiesParser(ValidationParser):
33
34 def add_arguments(self):
35 self.add_argument('cities',
36 type=argparse.FileType('r'),
37 help="path to TSV cities file, as documented in http://download.geonames.org/export/dump/")
38
39 def read_cities(self):
40 assert self.options
41 return read_cities(self.options.cities, fields=fields)