From 2d817dccc043ae24f05771d2829657c05b3e9978 Mon Sep 17 00:00:00 2001 From: "Kai A. Hiller" Date: Tue, 26 Nov 2024 15:16:07 +0100 Subject: [PATCH] Print usage on too many arguments --- scripts-dev/gen_config_documentation.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/scripts-dev/gen_config_documentation.py b/scripts-dev/gen_config_documentation.py index eb150b80ef..eb4ce75d77 100755 --- a/scripts-dev/gen_config_documentation.py +++ b/scripts-dev/gen_config_documentation.py @@ -461,19 +461,22 @@ def section(prop: str, values: dict) -> str: def main() -> None: - try: - script_name = "???.py" - script_name = sys.argv[0] - schemafile = sys.argv[1] - except IndexError: - print("No schema file provided.", file=sys.stderr) + def usage(err_msg: str) -> int: + script_name = (sys.argv[:1] or ["__main__.py"])[0] + print(err_msg, file=sys.stderr) print(f"Usage: {script_name} ", file=sys.stderr) print(f"\n{__doc__}", file=sys.stderr) exit(1) - with open(schemafile) as f: - schema = json.load(f) + def read_json_file_arg() -> Any: + if len(sys.argv) > 2: + exit(usage("Too many arguments.")) + if not (filepath := (sys.argv[1:] or [""])[0]): + exit(usage("No schema file provided.")) + with open(filepath) as f: + return json.load(f) + schema = read_json_file_arg() schema = resolve_local_refs(schema) sections = (section(k, v) for k, v in schema["properties"].items())