From bdc3b1ed0eb3c7f9acf1f3d7aac8596f1e2b686c Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 18 Apr 2024 19:40:44 +0000 Subject: [PATCH] sane-ip-check: port argument parsing to argparse --- .../additional/sane-scripts/src/sane-ip-check | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/pkgs/additional/sane-scripts/src/sane-ip-check b/pkgs/additional/sane-scripts/src/sane-ip-check index f20beb21e..cc8c804c8 100755 --- a/pkgs/additional/sane-scripts/src/sane-ip-check +++ b/pkgs/additional/sane-scripts/src/sane-ip-check @@ -1,9 +1,13 @@ #!/usr/bin/env nix-shell #!nix-shell -i python3 -p "python3.withPackages (ps: [ ps.requests ps.sane-lib.ssdp ])" -p miniupnpc +""" +sane-ip-check: query the IP address of this machine as seen by an external Internet host. +""" # best to run this with an external timeout. e.g. # - `timeout 60 sane-ip-check` +import argparse import json import logging import requests @@ -27,20 +31,25 @@ def get_wan_fallback(): if __name__ == '__main__': logging.basicConfig() + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("-v", action="store_true", help="be verbose") + parser.add_argument("-vv", action="store_true", help="be more verbose") + parser.add_argument("--json", action="store_true", help="output results in json format") + parser.add_argument("--no-upnp", action="store_true", help="instead of asking the gateway for its IP address, discover my IP by querying a third-party public service") + format = "plaintext" try_upnp = True + args = parser.parse_args() + + if args.v: + logging.getLogger().setLevel(logging.INFO) + if args.vv: + logging.getLogger().setLevel(logging.DEBUG) + if args.json: + format = "json" + if args.no_upnp: + try_upnp = False - for arg in sys.argv[1:]: - if arg == "-v": - logging.getLogger().setLevel(logging.INFO) - elif arg == "-vv": - logging.getLogger().setLevel(logging.DEBUG) - elif arg == "--json": - format = "json" - elif arg == "--no-upnp": - try_upnp = False - else: - raise RuntimeError(f"invalid CLI argument {arg!r}") upnp_details = get_any_wan() if try_upnp else None if upnp_details: