Skip to content

Commit

Permalink
Merge pull request #731 from audreyfeldroy/add-typer-for-cli
Browse files Browse the repository at this point in the history
Add typer for cli
  • Loading branch information
pydanny authored Apr 6, 2024
2 parents 4bbb716 + a5cf681 commit d032573
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 61 deletions.
2 changes: 1 addition & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"use_pytest": "n",
"use_pypi_deployment_with_travis": "y",
"add_pyup_badge": "n",
"command_line_interface": ["Click", "Argparse", "No command-line interface"],
"command_line_interface": ["Typer", "Argparse", "No command-line interface"],
"create_author_file": "y",
"open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"],
"__gh_slug": "{{ cookiecutter.github_username }}/{{ cookiecutter.project_slug }}"
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ classifiers = [
]
license = {text = "{{cookiecutter.open_source_license}}"}
dependencies = [
{% if cookiecutter.command_line_interface.lower() == "yes" -%}"rich",
{% if cookiecutter.command_line_interface.lower() == "typer" -%}
"typer"
{%- endif %}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
"""Console script for {{cookiecutter.project_slug}}."""
import {{cookiecutter.project_slug}}

{%- if cookiecutter.command_line_interface|lower == 'argparse' %}
import argparse
{%- endif %}
import sys
{%- if cookiecutter.command_line_interface|lower == 'click' %}
import click
{%- endif %}
import typer
from rich.console import Console

{% if cookiecutter.command_line_interface|lower == 'click' %}
@click.command()
def main(args=None):
"""Console script for {{cookiecutter.project_slug}}."""
click.echo("Replace this message by putting your code into "
"{{cookiecutter.project_slug}}.cli.main")
click.echo("See click documentation at https://click.palletsprojects.com/")
return 0
{%- endif %}
{%- if cookiecutter.command_line_interface|lower == 'argparse' %}
app = typer.Typer()
console = Console()


@app.command()
def main():
"""Console script for {{cookiecutter.project_slug}}."""
parser = argparse.ArgumentParser()
parser.add_argument('_', nargs='*')
args = parser.parse_args()

print("Arguments: " + str(args._))
print("Replace this message by putting your code into "
"{{cookiecutter.project_slug}}.cli.main")
return 0
{%- endif %}
console.print("Replace this message by putting your code into "
"{{cookiecutter.project_slug}}.cli.main")
console.print("See Typer documentation at https://typer.tiangolo.com/")



if __name__ == "__main__":
sys.exit(main()) # pragma: no cover
app()
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@
{% else %}
import unittest
{%- endif %}
{%- if cookiecutter.command_line_interface|lower == 'click' %}
from click.testing import CliRunner
{%- endif %}

from {{ cookiecutter.project_slug }} import {{ cookiecutter.project_slug }}
{%- if cookiecutter.command_line_interface|lower == 'click' %}
from {{ cookiecutter.project_slug }} import cli
{%- endif %}

{%- if cookiecutter.use_pytest == 'y' %}


Expand All @@ -33,19 +26,6 @@ def test_content(response):
"""Sample pytest test function with the pytest fixture as an argument."""
# from bs4 import BeautifulSoup
# assert 'GitHub' in BeautifulSoup(response.content).title.string
{%- if cookiecutter.command_line_interface|lower == 'click' %}


def test_command_line_interface():
"""Test the CLI."""
runner = CliRunner()
result = runner.invoke(cli.main)
assert result.exit_code == 0
assert '{{ cookiecutter.project_slug }}.cli.main' in result.output
help_result = runner.invoke(cli.main, ['--help'])
assert help_result.exit_code == 0
assert '--help Show this message and exit.' in help_result.output
{%- endif %}
{%- else %}


Expand All @@ -60,16 +40,4 @@ def tearDown(self):

def test_000_something(self):
"""Test something."""
{%- if cookiecutter.command_line_interface|lower == 'click' %}

def test_command_line_interface(self):
"""Test the CLI."""
runner = CliRunner()
result = runner.invoke(cli.main)
assert result.exit_code == 0
assert '{{ cookiecutter.project_slug }}.cli.main' in result.output
help_result = runner.invoke(cli.main, ['--help'])
assert help_result.exit_code == 0
assert '--help Show this message and exit.' in help_result.output
{%- endif %}
{%- endif %}

0 comments on commit d032573

Please sign in to comment.