Skip to content

Commit

Permalink
Use argparse for command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Dec 12, 2023
1 parent 1ac17b6 commit e45cd30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
19 changes: 9 additions & 10 deletions aoc_lube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess
import time
import webbrowser
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from pathlib import Path
from typing import Callable, Literal

Expand Down Expand Up @@ -62,24 +62,23 @@
)


def setup_dir(year: int | None = None, template: str | Path | None = None) -> None:
def setup_dir(year: int | None = None, template: Path | None = None) -> None:
r"""Run once to setup directory with templates for daily solutions.
A file or text can be provided as a template. The template must be a format
string with `year` and `day` keyword arguments, e.g.::
"import aoc_lube\ntodays_input = aoc_lube.fetch({year}, {day})"
If no template is provided, the default one is used instead.
A file can be provided as a template. The file's text should be able to be formatted
with `year` and `day` keyword arguments. See `code_template.txt` (the default
template) as an example.
"""
if year is None:
year = date.today().year

if template is None:
template = TEMPLATE_FILE.read_text()
elif isinstance(template, Path):
else:
template = template.read_text()

for day in range(1, 26):
file = Path(f"day_{day:02}.py")

if not file.exists():
file.write_text(template.format(year=year, day=day))

Expand Down
27 changes: 13 additions & 14 deletions aoc_lube/__main__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
"""Setup directory from command line with `python -m aoc_lube [YYYY]`."""
import datetime
import sys
"""Setup directory from command line with
`python -m aoc_lube [-y YEAR] [-t TEMPLATE]`.
"""
import argparse
from pathlib import Path

from . import setup_dir

try:
_, year, *rest = sys.argv
except ValueError:
setup_dir(datetime.date.today().year)
else:
if not year.isdigit():
print(f"What is year {year}?")
elif rest:
print(f"Extra argument(s) not recognized: {rest}")
else:
setup_dir(int(year))
parser = argparse.ArgumentParser(
prog="python -m aoc_lube", description="Setup a directory for Advent of Code."
)
parser.add_argument("-y", "--year", help="Advent of Code year.", default=None)
parser.add_argument("-t", "--template", help="A code template file.", default=None)
args = parser.parse_args()

setup_dir(args.year, args.template if args.template is None else Path(args.template))

0 comments on commit e45cd30

Please # to comment.