From 381f9721e2b7f4b551e9ac119ff53db95ce74585 Mon Sep 17 00:00:00 2001 From: Auric Vente Date: Fri, 26 Jul 2024 21:56:41 -0600 Subject: [PATCH] Mods --- arguments.md | 161 +++++++++++++++++++++++++++++++++++++++++++ cromulant/args.py | 88 +++++++++++++++++++++++ cromulant/argspec.py | 6 ++ cromulant/config.py | 2 + cromulant/main.py | 4 ++ cromulant/storage.py | 5 ++ 6 files changed, 266 insertions(+) create mode 100644 arguments.md diff --git a/arguments.md b/arguments.md new file mode 100644 index 0000000..405588f --- /dev/null +++ b/arguments.md @@ -0,0 +1,161 @@ +# Arguments + +Here are all the available command line arguments: + +--- + +### version + +Check the version of the program + +Action: version + +--- + +### names + +Path to a JSON file with a list of names. Use these instead of the default ones + +Type: str + +--- + +### ants + +Path to a JSON file with ants data. Use this instead of the default one + +Type: str + +--- + +### no-images + +Don't show the images on the left + +Action: store_false + +--- + +### no-header + +Don't show the header controls + +Action: store_false + +--- + +### no-footer + +Don't show the footer controls + +Action: store_false + +--- + +### no-intro + +Don't show the intro message + +Action: store_false + +--- + +### title + +Custom title for the window + +Default: [Empty string] + +Type: str + +--- + +### width + +The width of the window in pixels + +Default: 0 + +Type: int + +--- + +### height + +The height of the window in pixels + +Default: 0 + +Type: int + +--- + +### program + +The internal name of the program + +Default: [Empty string] + +Type: str + +--- + +### speed + +Use this update speed + +Default: [Empty string] + +Choices: "fast", "normal", "slow" + +Type: str + +--- + +### clean + +Start with clean ants data + +Default: False + +Action: store_true + +--- + +### fast-minutes + +The number of minutes between fast updates + +Default: 0.0 + +Type: float + +--- + +### normal-minutes + +The number of minutes between normal updates + +Default: 0.0 + +Type: float + +--- + +### slow-minutes + +The number of minutes between slow updates + +Default: 0.0 + +Type: float + +--- + +### argdoc + +Make the arguments document and exit + +Default: False + +Action: store_true diff --git a/cromulant/args.py b/cromulant/args.py index 57dfe41..f49af1c 100644 --- a/cromulant/args.py +++ b/cromulant/args.py @@ -5,6 +5,7 @@ from pathlib import Path from typing import Any from .config import Config +from .utils import Utils from .argspec import ArgSpec @@ -24,6 +25,7 @@ class Args: fast_minutes: float = 0.0 normal_minutes: float = 0.0 slow_minutes: float = 0.0 + argdoc: bool = False @staticmethod def prepare() -> None: @@ -53,6 +55,7 @@ class Args: "fast_minutes", "normal_minutes", "slow_minutes", + "argdoc", ] for n_item in normals: @@ -66,6 +69,91 @@ class Args: for p_item in paths: ArgParser.get_value(p_item, path=True) + @staticmethod + def make_argdoc() -> None: + from .utils import Utils + from .storage import Storage + + text = Args.argtext() + Storage.save_arguments(text) + Utils.print("Saved arguments document") + + @staticmethod + def argtext(filter_text: str | None = None) -> str: + sep = "\n\n---\n\n" + text = "" + filter_lower = "" + + if not filter_text: + text = "# Arguments\n\n" + text += "Here are all the available command line arguments:" + else: + filter_lower = filter_text.lower() + + for key in ArgSpec.arguments: + if key == "string_arg": + continue + + arg = ArgSpec.arguments[key] + info = arg.get("help", "") + + if filter_text: + if filter_lower not in key.lower(): + if filter_lower not in info.lower(): + continue + + text += sep + name = key.replace("_", "-") + text += f"### {name}" + + if info: + text += "\n\n" + text += info + + defvalue = ArgSpec.defaults.get(key) + + if defvalue is not None: + if isinstance(defvalue, str): + if defvalue == "": + defvalue = "[Empty string]" + elif defvalue.strip() == "": + spaces = defvalue.count(" ") + ds = Utils.singular_or_plural(spaces, "space", "spaces") + defvalue = f"[{spaces} {ds}]" + else: + defvalue = f'"{defvalue}"' + + text += "\n\n" + text += f"Default: {defvalue}" + + choices = arg.get("choices", []) + + if choices: + text += "\n\n" + text += "Choices: " + + choicestr = [ + f'"{choice}"' if isinstance(choice, str) else choice + for choice in choices + ] + + text += ", ".join(choicestr) + + action = arg.get("action", "") + + if action: + text += "\n\n" + text += f"Action: {action}" + + argtype = arg.get("type", "") + + if argtype: + text += "\n\n" + text += f"Type: {argtype.__name__}" + + text += "\n" + return text.lstrip() + class ArgParser: parser: argparse.ArgumentParser diff --git a/cromulant/argspec.py b/cromulant/argspec.py index 710f246..d23dded 100644 --- a/cromulant/argspec.py +++ b/cromulant/argspec.py @@ -160,3 +160,9 @@ class ArgSpec: type=float, info="The number of minutes between slow updates", ) + + ArgSpec.add_argument( + "argdoc", + action="store_true", + info="Make the arguments document and exit", + ) diff --git a/cromulant/config.py b/cromulant/config.py index 14177bb..ce3fa21 100644 --- a/cromulant/config.py +++ b/cromulant/config.py @@ -61,6 +61,7 @@ class Config: icon_on: str = "✅" icon_off: str = "❌" ant: str = "🐜" + arguments_path: Path @staticmethod def prepare() -> None: @@ -98,3 +99,4 @@ class Config: Config.emoji_font_path = Config.here / "fonts" / "NotoEmoji-Regular.ttf" Config.song_path = Config.here / "audio" / "March of the Cyber Ants.mp3" Config.logo_path = Config.here / "img" / "logo_3.jpg" + Config.arguments_path = Config.here / ".." / "arguments.md" diff --git a/cromulant/main.py b/cromulant/main.py index 03e79f8..0fd3391 100644 --- a/cromulant/main.py +++ b/cromulant/main.py @@ -20,6 +20,10 @@ def main() -> None: Config.prepare() Args.prepare() + if Args.argdoc: + Args.make_argdoc() + sys.exit(0) + program = Config.program title = Config.title diff --git a/cromulant/storage.py b/cromulant/storage.py index 4266892..0d26623 100644 --- a/cromulant/storage.py +++ b/cromulant/storage.py @@ -83,3 +83,8 @@ class Storage: def get_manifest() -> Any: with Config.manifest_path.open() as file: return json.load(file) + + @staticmethod + def save_arguments(text: str) -> None: + with Config.arguments_path.open("w") as file: + file.write(text)