Skip to content

Commit

Permalink
Making CLI better
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Feb 28, 2015
1 parent 0f5217e commit 5145120
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/features/Command Line Interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The output is, by default, sent to stdout, so it can be conveniently parsed
by JSON parsing tools like `jq`.

```bash
$ geocode `textfile.txt` | jq [.lat,.lng,.country] -c
$ geocode "textfile.txt" | jq [.lat,.lng,.country] -c
[45.389198303222656,-75.68800354003906,"Canada"]
[42.35866165161133,-71.0567398071289,"United States"]
```
Expand Down
75 changes: 50 additions & 25 deletions geocoder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,76 @@
import sys
from .api import get


def peek(iterable):
iterator = iter(iterable)
item = next(iterator)
new_iterator = itertools.chain([item], iterator)
return item, new_iterator


def cli():
parser = argparse.ArgumentParser(description="Geocode an arbitrary number of strings from Command Line.")
parser.add_argument('input', type=str, nargs="*",
parser = argparse.ArgumentParser(description="Geocode an arbitrary number"
" of strings from Command Line.")
parser.add_argument('input',
type=str,
nargs="*",
help="Filename(s) or strings to be geocoded")
parser.add_argument('-p', '--provider',
help="provider (choose from: bing,"
"geonames, google, mapquest, nokia, osm, tomtom, "
"geolytica, arcgis, yahoo)",
"geonames, google, mapquest, nokia, osm, tomtom, "
"geolytica, arcgis, yahoo, ottawa)",
default='bing')
parser.add_argument('-o', '--outfile', help="Output file (default stdout)",
default=sys.stdout)
parser.add_argument('--geojson', help="GeoJSON output format (default json)",
action="store_true")
parser.add_argument('--json', help="JSON output format (default json)",
action="store_true")
parser.add_argument('--osm', help="OSM output format (default json)",
action="store_true")
parser.add_argument('--pretty', help="Prettify JSON output",
parser.add_argument('-m', '--method',
type=str,
help="Output type (choose from: geocode, reverse)",
default='geocode')
parser.add_argument('-o', '--outfile',
help="Output file (default stdout)",
default=sys.stdout)
parser.add_argument('-t', '--type',
type=str,
help="Output type (choose from: json, osm, geojson)",
default='json')
parser.add_argument('--pretty',
help="Prettify JSON output",
action="store_true")
args = parser.parse_args()

try:
sys.argv = [sys.argv[1]] + args.input
input = fileinput.input()
_, input = peek(input)
except IOError:
input = args.input
# User input data
if args.input:
try:
sys.argv = [sys.argv[1]] + args.input
input = fileinput.input()
_, input = peek(input)
except IOError:
input = args.input
else:
print('[ERROR] Please include a location or a <File Path>.\n'
'$ geocode "Ottawa ON\n"'
'$ geocode "textfile.txt"')
sys.exit()

for item in input:
item = item.strip()
g = get(item, provider=args.provider)
g = get(item, provider=args.provider, method=args.method)

if args.geojson:
output = g.geojson
elif args.osm:
output = g.osm
# User input output
args.type = args.type.lower()
type_lookup = {
'json': g.json,
'geojson': g.geojson,
'osm': g.osm,
}
if args.type in type_lookup:
output = type_lookup.get(args.type.lower(), '')
else:
output = g.json
print('[ERROR] Please provide a valid type.\n'
'Ex: geojson, osm, json\n'
'$ geocode "Ottawa ON" --type geojson')
sys.exit()

# User define Pretty output
if args.pretty:
params = {'indent': 4}
else:
Expand Down

0 comments on commit 5145120

Please sign in to comment.