This commit is contained in:
Auric Vente
2024-07-26 21:56:41 -06:00
parent 03c3fbeba6
commit 381f9721e2
6 changed files with 266 additions and 0 deletions

View File

@@ -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

View File

@@ -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",
)

View File

@@ -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"

View File

@@ -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

View File

@@ -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)