osl-incubator / scicookie

Cookiecutter template for a Python package.
https://osl-incubator.github.io/scicookie
Other
14 stars 18 forks source link

The file cli.py is not created with a small example for the cli option defined by the user #122

Closed xmnlab closed 6 months ago

xmnlab commented 1 year ago

Summary

The file cli.py is not created with a small example for the cli option defined by the user

Additional Information

No response

Code of Conduct

xmnlab commented 1 year ago

A very simple example for argparse could be inspired by this snippet:

import argparse
import os
import sys

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='{{ coockiecutter.project_slug }}',
        description=('{{ coockiecutter.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()

of course, this one uses single quote, but the real implementation should check if it is blue or black and set a variable QUOTE (in the template), and use it instead of hard coded single quote

xmnlab commented 1 year ago

Additionally, we need to have something like this inside the pyproject.toml:

[tool.poetry.scripts]
"{{ cookiecutter.project_slug }}" = "{{ cookiecutter.package_slug }}.__main__:app"

just if the CLI tool was selected by the user.

this new section could be defined after the section [tool.poetry]

YurelyCamacho commented 8 months ago

@xmnlab Any code suggestions for Click. I have seen this, but I don't really know which one could be useful, I don't know much about it. https://realpython.com/python-click/ https://click.palletsprojects.com/en/8.1.x/ https://medium.com/@rahulmadan_18191/why-not-click-for-cli-using-python-e70c41c4539a

xmnlab commented 8 months ago

I would just use the chatgpt for converting the current one to click let me know if you need access to an api key