Print usage on too many arguments

This commit is contained in:
Kai A. Hiller 2024-11-26 15:16:07 +01:00
parent 9fd0a48ccb
commit 2d817dccc0

View file

@ -461,19 +461,22 @@ def section(prop: str, values: dict) -> str:
def main() -> None: def main() -> None:
try: def usage(err_msg: str) -> int:
script_name = "???.py" script_name = (sys.argv[:1] or ["__main__.py"])[0]
script_name = sys.argv[0] print(err_msg, file=sys.stderr)
schemafile = sys.argv[1]
except IndexError:
print("No schema file provided.", file=sys.stderr)
print(f"Usage: {script_name} <JSON Schema file>", file=sys.stderr) print(f"Usage: {script_name} <JSON Schema file>", file=sys.stderr)
print(f"\n{__doc__}", file=sys.stderr) print(f"\n{__doc__}", file=sys.stderr)
exit(1) exit(1)
with open(schemafile) as f: def read_json_file_arg() -> Any:
schema = json.load(f) 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) schema = resolve_local_refs(schema)
sections = (section(k, v) for k, v in schema["properties"].items()) sections = (section(k, v) for k, v in schema["properties"].items())