Skip to content

celie56/using_click_cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using Click in Python

Click is a tool used to make quick CLI interfaces.

This doc is intended to outline how to set up a click interface and test that it is working.

Setup

pipenv install click

A basic file

"""A simple click interface example."""
import click


@click.command()
@click.argument('name', required=True)
def say_hello_to(name):
    """print name that is specified using CLI."""
    print(f'hello {name}')


if __name__ == '__main__':
    say_hello_to()

Quickstart — Click Documentation (7.x)

Running a command

(temp)  🌈  ~/dev/temp  python say_hello_to.py chris
hello chris

Creating a unit test

"""Tests for our greet program."""
import string
import random

from click.testing import CliRunner
import greet


def make_fake_name() -> str:
    """Fake name generator."""
    return ''.join(random.choices(string.ascii_uppercase, k=10))


def test_say_hello_to() -> None:
    """Test our click CLI."""
    runner = CliRunner()
    fake_name = make_fake_name()
    result = runner.invoke(greet.say_hello_to, [fake_name])

    assert result.exit_code == 0
    assert result.output == f'hello {fake_name}\n'

Testing Click Applications — Click Documentation (7.x)

(temp) 🌈  ~/dev/temp  pytest
============================= test session starts ==============================
platform linux -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /home/chris/dev/temp
collected 1 item                                                               

test_greet.py .                                                          [100%]

============================== 1 passed in 0.01s ===============================

About

A brief intro to the click cli library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages