Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(file): add example code for the cli option defined by the user #225

Merged
merged 21 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
- linters.sh
- tests.sh
- virtual-envs.sh
- cli.sh

defaults:
run:
Expand Down
1 change: 1 addition & 0 deletions .makim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/scicookie/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Interface for the `python -m scicookie`."""

from scicookie.cli import app

if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions src/scicookie/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module with CLI functions."""

import argparse
import json
import os
Expand Down
2 changes: 2 additions & 0 deletions src/scicookie/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/scicookie/logs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module for functions and classes for systen logs."""

import os

from enum import Enum
Expand Down
1 change: 1 addition & 0 deletions src/scicookie/profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Profile handles "profiles" defined in the .yaml files."""

from __future__ import annotations

from pathlib import Path
Expand Down
1 change: 1 addition & 0 deletions src/scicookie/ui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Define functions for the interface with the user."""

from __future__ import annotations

import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ repos:
entry: ruff --fix
language: system
pass_filenames: true
require_serial: yes
files: "./"
types:
- python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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__":
xmnlab marked this conversation as resolved.
Show resolved Hide resolved
app()
7 changes: 7 additions & 0 deletions tests/smoke/cli.sh
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions tests/test_bake_project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Cookiecutter bake test."""

from pathlib import Path

import pytest
Expand Down