From: W. Trevor King Date: Thu, 19 May 2011 14:30:17 +0000 (-0400) Subject: Use argparser in maproute.py. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b659164a0f36ede2daaba83090a431c1fb8d96db;p=blog.git Use argparser in maproute.py. --- diff --git a/posts/geoscript/maproute.py b/posts/geoscript/maproute.py index 59422be..67c5c2c 100644 --- a/posts/geoscript/maproute.py +++ b/posts/geoscript/maproute.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" +"""Print route distances and plot routes on a 2D map. >>> from StringIO import StringIO >>> stream = StringIO('\\n'.join([ @@ -43,6 +43,10 @@ import numpy as _numpy import pylab as _pylab import pyproj as _pyproj + +__version__ = '0.1' + + def read_latlngs(stream): "Read in data from space-separated lat/lng lines with '#' comments." return _numpy.loadtxt(stream) @@ -75,11 +79,20 @@ def plot_route(latlngs, proj=None): lines = _pylab.plot(xs, ys, '.-') if __name__ == '__main__': - import sys + from argparse import ArgumentParser + + p = ArgumentParser( + description=__doc__.splitlines()[0],) + p.add_argument( + '--version', action='version', version='%(prog)s '+__version__) + p.add_argument('coordfiles', metavar='COORDFILE', type=str, nargs='+', + help='Space-separated lat/lng line file') + + args = p.parse_args() _pylab.hold(True) - for filename in sys.argv[1:]: + for filename in args.coordfiles: latlngs = read_latlngs(filename) dist = route_distance(latlngs) print('%s distance: %g m (%g mi)' % (filename, dist, dist/1.6e3))