From 1b1db90358d11c97d02fa7462bfcb1ef6b6b786c Mon Sep 17 00:00:00 2001 From: YurelyCamacho <49034451+YurelyCamacho@users.noreply.github.com> Date: Wed, 13 Mar 2024 22:21:24 -0400 Subject: [PATCH] fix(cli): Add an initial code for the CLI option defined by the user (#225) --- .github/workflows/main.yaml | 1 + .makim.yaml | 1 + src/scicookie/__main__.py | 1 + src/scicookie/cli.py | 1 + src/scicookie/hooks/post_gen_project.py | 2 + src/scicookie/logs.py | 1 + src/scicookie/profile.py | 1 + src/scicookie/ui.py | 1 + .../.pre-commit-config.yaml | 1 + .../build-system/poetry-pyproject.toml | 5 + .../{{cookiecutter.package_slug}}/cli.py | 91 +++++++++++++++++++ tests/smoke/cli.sh | 7 ++ tests/test_bake_project.py | 1 + 13 files changed, 114 insertions(+) create mode 100644 src/scicookie/{{cookiecutter.project_slug}}/{{cookiecutter.package_slug}}/cli.py create mode 100644 tests/smoke/cli.sh diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 0262c097..1b3defeb 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -69,6 +69,7 @@ jobs: - linters.sh - tests.sh - virtual-envs.sh + - cli.sh defaults: run: diff --git a/.makim.yaml b/.makim.yaml index a1b59b1b..1873b209 100644 --- a/.makim.yaml +++ b/.makim.yaml @@ -68,6 +68,7 @@ groups: run: | ./tests/smoke/auto-format-tools.sh ./tests/smoke/build-systems.sh + ./tests/smoke/cli.sh ./tests/smoke/containers.sh ./tests/smoke/docs.sh ./tests/smoke/files.sh diff --git a/src/scicookie/__main__.py b/src/scicookie/__main__.py index 165b5178..2c003643 100644 --- a/src/scicookie/__main__.py +++ b/src/scicookie/__main__.py @@ -1,4 +1,5 @@ """Interface for the `python -m scicookie`.""" + from scicookie.cli import app if __name__ == "__main__": diff --git a/src/scicookie/cli.py b/src/scicookie/cli.py index 5effd20a..c7e1886e 100644 --- a/src/scicookie/cli.py +++ b/src/scicookie/cli.py @@ -1,4 +1,5 @@ """Module with CLI functions.""" + import argparse import json import os diff --git a/src/scicookie/hooks/post_gen_project.py b/src/scicookie/hooks/post_gen_project.py index f5323ebc..028c7d28 100644 --- a/src/scicookie/hooks/post_gen_project.py +++ b/src/scicookie/hooks/post_gen_project.py @@ -179,6 +179,8 @@ def clean_up_containers(): def clean_up_cli(): if not USE_CLI: remove_package_file("__main__.py") + remove_package_file("cli.py") + def clean_up_prettier(): if not USE_PRETTIER: diff --git a/src/scicookie/logs.py b/src/scicookie/logs.py index c6facd47..680844df 100644 --- a/src/scicookie/logs.py +++ b/src/scicookie/logs.py @@ -1,4 +1,5 @@ """Module for functions and classes for systen logs.""" + import os from enum import Enum diff --git a/src/scicookie/profile.py b/src/scicookie/profile.py index c8397fd3..1ec217f7 100644 --- a/src/scicookie/profile.py +++ b/src/scicookie/profile.py @@ -1,4 +1,5 @@ """Profile handles "profiles" defined in the .yaml files.""" + from __future__ import annotations from pathlib import Path diff --git a/src/scicookie/ui.py b/src/scicookie/ui.py index c3ef4fb3..c68f929a 100644 --- a/src/scicookie/ui.py +++ b/src/scicookie/ui.py @@ -1,4 +1,5 @@ """Define functions for the interface with the user.""" + from __future__ import annotations import os diff --git a/src/scicookie/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/src/scicookie/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index e8cb7175..b1b8ac5e 100644 --- a/src/scicookie/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/src/scicookie/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -51,6 +51,7 @@ repos: entry: ruff --fix language: system pass_filenames: true + require_serial: yes files: "./" types: - python diff --git a/src/scicookie/{{cookiecutter.project_slug}}/build-system/poetry-pyproject.toml b/src/scicookie/{{cookiecutter.project_slug}}/build-system/poetry-pyproject.toml index 61d00b4f..b037807c 100644 --- a/src/scicookie/{{cookiecutter.project_slug}}/build-system/poetry-pyproject.toml +++ b/src/scicookie/{{cookiecutter.project_slug}}/build-system/poetry-pyproject.toml @@ -28,6 +28,11 @@ exclude = [ include = ["{{ package_path }}/py.typed"] {%- endif %} +{% if cookiecutter.command_line_interface != "None" -%} +[tool.poetry.scripts] +"{{ cookiecutter.project_slug }}" = "{{ cookiecutter.package_slug }}.__main__:app" +{%- endif %} + [tool.poetry.dependencies] python = ">=3.8.1,<4" diff --git a/src/scicookie/{{cookiecutter.project_slug}}/{{cookiecutter.package_slug}}/cli.py b/src/scicookie/{{cookiecutter.project_slug}}/{{cookiecutter.package_slug}}/cli.py new file mode 100644 index 00000000..ddf191b6 --- /dev/null +++ b/src/scicookie/{{cookiecutter.project_slug}}/{{cookiecutter.package_slug}}/cli.py @@ -0,0 +1,91 @@ +"""Module with CLI functions.""" + +{%- if cookiecutter.command_line_interface == "Argparse" %} + +import argparse + +from {{ cookiecutter.package_slug }} import __version__ + + +class CustomHelpFormatter(argparse.RawTextHelpFormatter): + """Formatter for generating usage messages and argument help strings. + + Only the name of this class is considered a public API. All the methods + provided by the class are considered an implementation detail. + """ + + def __init__( + self, + prog, + indent_increment=2, + max_help_position=4, + width=None, + **kwargs, + ): + super().__init__( + prog, + indent_increment=indent_increment, + max_help_position=max_help_position, + width=width, + **kwargs, + ) + + +def get_args(): + """Return the arguments for the CLI.""" + parser = argparse.ArgumentParser( + prog="{{ cookiecutter.project_slug }}", + description=("{{ cookiecutter.project_name }}"), + epilog=( + "If you have any problem, open an issue at: " + "{{ cookiecutter.project_url }}" + ), + add_help=True, + formatter_class=CustomHelpFormatter, + ) + parser.add_argument( + "--version", + action="store_true", + help="Show the version of the installed {{ cookiecutter.project_name }} tool.", + ) + + return parser + + +def show_version(): + """Show the version for the application.""" + print(__version__) + + +def app(): + """Run the application.""" + args_parser = get_args() + args = args_parser.parse_args() + + if args.version: + return show_version() + +{%- elif cookiecutter.command_line_interface == "Click" %} + +import click + +from {{ cookiecutter.package_slug }} import __version__ + + +@click.command() +@click.option( + "--version", + is_flag=True, + help="Show the version of the installed {{ cookiecutter.project_name }} tool.", +) +def app(version): + """Run the application.""" + if version: + return click.echo(__version__) + click.echo("You can add more Click commands here.") + +{%- endif %} + + +if __name__ == "__main__": + app() diff --git a/tests/smoke/cli.sh b/tests/smoke/cli.sh new file mode 100644 index 00000000..68e860aa --- /dev/null +++ b/tests/smoke/cli.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -e + +SMOKE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +. ${SMOKE_DIR}/base.sh "command_line_interface=Argparse" +. ${SMOKE_DIR}/base.sh "command_line_interface=Click" diff --git a/tests/test_bake_project.py b/tests/test_bake_project.py index 0d2bb374..5db0aabf 100644 --- a/tests/test_bake_project.py +++ b/tests/test_bake_project.py @@ -1,4 +1,5 @@ """Cookiecutter bake test.""" + from pathlib import Path import pytest