Skip to content

Commit

Permalink
Use Typer instead of argparse in main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nikobockerman committed Dec 14, 2023
1 parent 3ba488e commit 2970925
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
29 changes: 15 additions & 14 deletions adventofcode/main.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import argparse
import importlib
import logging
import pathlib
import sys
from typing import Any
from typing import Any, Optional

import typer
from typing_extensions import Annotated

from adventofcode.answers import ANSWERS

app = typer.Typer()

def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("day", type=int, nargs="?")
parser.add_argument("problem", type=int, nargs="?")
parser.add_argument("--debug", "-d", action="store_true")
args = parser.parse_args()

@app.command()
def main(
day: Annotated[Optional[int], typer.Argument()] = None,
problem: Annotated[Optional[int], typer.Argument()] = None,
debug: Annotated[bool, typer.Option("--debug", "-d")] = False,
) -> None:
level = logging.INFO
if args.debug:
if debug:
level = logging.DEBUG
logging.basicConfig(level=level)

exit_code: int = 0
if args.day is not None and args.problem is not None:
day: int = args.day
problem: int = args.problem
if day is not None and problem is not None:
exit_code = specific_problem(day, problem)
else:
days = list(ANSWERS.keys()) if args.day is None else [args.day]
days = list(ANSWERS.keys()) if day is None else [day]
all_passed = None
for day in days:
problems = ANSWERS.get(day, {})
Expand Down Expand Up @@ -121,4 +122,4 @@ def output(msg: str) -> None:


if __name__ == "__main__":
main()
typer.run(main)
23 changes: 22 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test = ["pytest"]

[tool.poetry.dependencies]
python = "^3.12"
typer = "^0.9.0"

[tool.poetry.group.dev.dependencies]
black = "^23.11.0"
Expand All @@ -31,7 +32,7 @@ isort = "^5.12.0"
poethepoet = "^0.24.4"

[tool.poetry.scripts]
adventofcode = "adventofcode.main:main"
adventofcode = "adventofcode.main:app"

[tool.mypy]
python_version = "3.12"
Expand Down

0 comments on commit 2970925

Please sign in to comment.