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

161
arguments.md Normal file
View File

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

View File

@ -5,6 +5,7 @@ from pathlib import Path
from typing import Any from typing import Any
from .config import Config from .config import Config
from .utils import Utils
from .argspec import ArgSpec from .argspec import ArgSpec
@ -24,6 +25,7 @@ class Args:
fast_minutes: float = 0.0 fast_minutes: float = 0.0
normal_minutes: float = 0.0 normal_minutes: float = 0.0
slow_minutes: float = 0.0 slow_minutes: float = 0.0
argdoc: bool = False
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
@ -53,6 +55,7 @@ class Args:
"fast_minutes", "fast_minutes",
"normal_minutes", "normal_minutes",
"slow_minutes", "slow_minutes",
"argdoc",
] ]
for n_item in normals: for n_item in normals:
@ -66,6 +69,91 @@ class Args:
for p_item in paths: for p_item in paths:
ArgParser.get_value(p_item, path=True) 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: class ArgParser:
parser: argparse.ArgumentParser parser: argparse.ArgumentParser

View File

@ -160,3 +160,9 @@ class ArgSpec:
type=float, type=float,
info="The number of minutes between slow updates", 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_on: str = ""
icon_off: str = "" icon_off: str = ""
ant: str = "🐜" ant: str = "🐜"
arguments_path: Path
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
@ -98,3 +99,4 @@ class Config:
Config.emoji_font_path = Config.here / "fonts" / "NotoEmoji-Regular.ttf" Config.emoji_font_path = Config.here / "fonts" / "NotoEmoji-Regular.ttf"
Config.song_path = Config.here / "audio" / "March of the Cyber Ants.mp3" Config.song_path = Config.here / "audio" / "March of the Cyber Ants.mp3"
Config.logo_path = Config.here / "img" / "logo_3.jpg" 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() Config.prepare()
Args.prepare() Args.prepare()
if Args.argdoc:
Args.make_argdoc()
sys.exit(0)
program = Config.program program = Config.program
title = Config.title title = Config.title

View File

@ -83,3 +83,8 @@ class Storage:
def get_manifest() -> Any: def get_manifest() -> Any:
with Config.manifest_path.open() as file: with Config.manifest_path.open() as file:
return json.load(file) return json.load(file)
@staticmethod
def save_arguments(text: str) -> None:
with Config.arguments_path.open("w") as file:
file.write(text)