feat(main.py):添加CLI工具支持问题管理功能

📝 docs(README.md):更新项目说明并添加main.py使用指南
⬆️ chore(pyproject.toml):添加typer依赖以支持CLI功能
 test(solutions):添加0036号问题的解决方案文件
This commit is contained in:
2026-01-06 14:04:31 +08:00
parent 0ea015f3af
commit 6fb29a863c
5 changed files with 253 additions and 4 deletions

68
main.py
View File

@@ -1,6 +1,68 @@
def main():
print("Hello from projecteuler!")
import runpy
from pathlib import Path
from profile import run
from tarfile import SOLARIS_XHDTYPE
import typer
app = typer.Typer()
@app.command("add", help="Add a new problem to the projecteuler repository.")
def add_newproblem(num: int, name: str | None = None) -> None:
"""Add a new problem to the projecteuler repository."""
typer.echo(f"Adding problem {num} to the projecteuler repository.")
if name:
title = f"{num:04d}.{name}"
else:
title = f"{num:04d}"
Path(f"solutions/{title}").mkdir(parents=True, exist_ok=True)
Path(f"solutions/{title}/euler_{num}.py").touch()
@app.command("list", help="List all problems in the projecteuler repository.")
def list_problems(every: bool = False):
"""List all problems in the projecteuler repository."""
if every:
typer.echo("Listing all problems in the projecteuler repository.")
else:
typer.echo("Listing near problems in the projecteuler repository.")
for path in Path("solutions").iterdir():
if path.is_dir():
if every:
tmp = list(path.iterdir())
tmp = all(ff.is_dir() for ff in tmp)
if tmp:
for ff in path.iterdir():
if ff.is_dir():
typer.echo(f"{ff.name}")
else:
typer.echo(f"{path.name}")
else:
if "_" in path.name:
typer.echo(f"{path.name} (completed)")
else:
typer.echo(f"{path.name}")
@app.command("solution", help="Run a solution for a given problem.")
def run_solution(num: int) -> None:
folders = list(Path("solutions").iterdir())
folders = [
folder
for folder in folders
if folder.is_dir() and folder.name.startswith(f"{num:04d}")
]
runpy.run_path(
(folders[0] / f"euler_{num}.py").resolve().as_posix(), run_name="__main__"
)
@app.command("version", help="Display the version of the projecteuler CLI.")
def version():
"""Display the version of the projecteuler CLI."""
typer.echo("projecteuler solution version 0.1.0")
if __name__ == "__main__":
main()
app()