From 15a8deca6574674b7a3e10d88bc815dc7b7e3a28 Mon Sep 17 00:00:00 2001 From: alterNERDtive Date: Mon, 9 Mar 2020 17:51:36 +0100 Subject: [PATCH] spansh.py: added `--parsable` format to `nearestsystem` --- README.md | 4 +++- spansh.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 40f2382..cf10c30 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,8 @@ subcommands: ``` ``` -usage: spansh.py nearestsystem [-h] [--short] coordinate coordinate coordinate +usage: spansh.py nearestsystem [-h] [--short | --parsable] + coordinate coordinate coordinate positional arguments: coordinate the coordinates to search for (order: x, y, z) @@ -163,6 +164,7 @@ positional arguments: optional arguments: -h, --help show this help message and exit --short short output format (system name only) + --parsable parsable output format (|,,|) ``` ``` diff --git a/spansh.py b/spansh.py index d7381c8..e89d4e8 100755 --- a/spansh.py +++ b/spansh.py @@ -17,9 +17,13 @@ def getNearestSystem(coords): } json = requests.post(APIURLS["nearest"], params).json() - ret = "" + ret = None + system = json["system"] if args.short: - ret = json["system"]["name"] + ret = system["name"] + elif args.parsable: + ret = "{}|{},{},{}|{}".format(system["name"], system["x"], + system["y"], system["z"], round(system["distance"], 2)) else: system = json["system"] ret = "{} ({}, {}, {}), {} ly".format(system["name"], system["x"], @@ -77,8 +81,11 @@ parser_nearestsystem = subparsers.add_parser("nearestsystem", help="Searches for the nearest system in the database to given coordinates.") parser_nearestsystem.add_argument("coordinate", nargs=3, type=int, help="the coordinates to search for (order: x, y, z)") -parser_nearestsystem.add_argument("--short", action='store_true', +group = parser_nearestsystem.add_mutually_exclusive_group(required=False) +group.add_argument("--short", action='store_true', help="short output format (system name only)") +group.add_argument("--parsable", action='store_true', + help="parsable output format (|,,|)") parser_oldstations = subparsers.add_parser("oldstations", help="Searches for stations with old data (>1 year without an update.")