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

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

Closed
1 task done
xmnlab opened this issue Jun 11, 2023 · 4 comments
Closed
1 task done
Assignees
Labels
bug Something isn't working grant-psf-2024-01 size:~2h <=2h

Comments

@xmnlab
Copy link
Member

xmnlab commented Jun 11, 2023

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

  • I agree to follow the Code of Conduct
@xmnlab xmnlab added the bug Something isn't working label Jun 11, 2023
@xmnlab
Copy link
Member Author

xmnlab commented Jun 11, 2023

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
Copy link
Member Author

xmnlab commented Jun 11, 2023

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
Copy link
Collaborator

@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
Copy link
Member Author

xmnlab commented Feb 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working grant-psf-2024-01 size:~2h <=2h
Projects
None yet
Development

No branches or pull requests

3 participants