Skip to content

Commit

Permalink
Refactor some of the logic in the CLI (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
seapagan authored Sep 2, 2024
1 parent 127c3e1 commit 35f3af4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
37 changes: 16 additions & 21 deletions lice2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
from pathlib import Path
from types import SimpleNamespace
from typing import Annotated, Optional
from typing import Annotated, Any, Callable, Optional

import typer
from rich import print as rprint
Expand Down Expand Up @@ -44,7 +44,7 @@
),
context_settings={"help_option_names": ["-h", "--help"]},
)
def main( # noqa: C901, PLR0912, PLR0913
def main( # noqa: PLR0913
license_name: Annotated[
str,
typer.Argument(
Expand Down Expand Up @@ -202,25 +202,20 @@ def main( # noqa: C901, PLR0912, PLR0913
# get the language if set
lang = get_lang(args)

# output metadata as JSON if requested
if metadata:
get_metadata(args)

# list available licenses and their template variables
if args.list_licenses:
list_licenses()

# list available source formatting languages
if args.list_languages:
list_languages()

# generate header if requested
if header:
generate_header(args, lang)

# list template vars if requested
if args.list_vars:
list_vars(args, license_name)
actions: list[tuple[bool, Callable[..., None], list[Any]]] = [
(metadata, get_metadata, [args]),
(args.list_licenses, list_licenses, []),
(args.list_languages, list_languages, []),
(header, generate_header, [args, lang]),
(args.list_vars, list_vars, [args, license_name]),
]

# Iterate through the list and call the utility functions based on the
# conditions. All the utility functions exit the program after execution.
# This saves us from having to write a lot of if-else statements.
for condition, func, func_args in actions:
if condition:
func(*func_args)

# create context
if args.template_path:
Expand Down
2 changes: 1 addition & 1 deletion lice2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def copy_to_clipboard(out: StringIO) -> None:
raise typer.Exit(2) from None


def get_metadata(args: SimpleNamespace) -> str:
def get_metadata(args: SimpleNamespace) -> None:
"""Return metadata for the package as a JSON string."""
licenses = LICENSES
languages = list(LANGS.keys())
Expand Down

0 comments on commit 35f3af4

Please sign in to comment.