Click: a beautiful python library to write CLI applications

An image of a terminal running top command
An image of my terminal running top command

Basic concepts

import click

@click.argument('input_file', type=click.Path(dir_okay=False))
def cli(input_file):
"""INPUT_FILE represents the path to a file on your file tree"""


pip install click

Project setup

name = "click_tutorial"
version = "0.1.0"
description = "A tutorial about using click"
authors = ["le_woudar <XXX@XX>"]
license = "MIT"

python = "^3.7"
click = "^7.1.2"

pytest = "^6.1.2"

requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
name = "click_tutorial"
version = "0.1.0"
description = "A tutorial about using click"
authors = ["le_woudar <>"]
license = "MIT"

packages = [
{ include = "scripts" }

python = "^3.7"


import click

@click.option('-n', '--name', prompt='Your name', help='Name to greet')
def cli(name):
"""Greets a user who gives his name as input"""
click.echo(f'Hello {name}!')
hello = "scripts.hello:cli"
$ hello --help
Usage: hello [OPTIONS]
Greets a user who gives his name as inputOptions:
-n, --name TEXT Name to greet
$ hello -n kevin
Hello kevin!
$ hello --name=kevin
Hello kevin!
$ hello
Your name: kevin
Hello kevin!
$ pycat
Usage: pycat [OPTIONS] FILE
Try 'pycat --help' for help.
Error: Missing argument 'FILE'.$ pycat hello.txt
Hello world!
Snippet code of pycat command
Snippet code of pyless command
$ pyless lorem_ipsum.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est velit, lobortis ut magna vel, venenatis ultrices magna. Maecenas accumsan mi ut metus vehicula, sed facilisis enim sollicitudin. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse quis tellus scelerisque, semper risus sed, efficitur erat. Proin semper quis leo iaculis sodales. Mauris in diam tortor. Aenean condimentum felis odio, non consequat nisi lacinia eu.
... # use the down arrow or Enter key to continue reading the text
Snippet code of pywc command
$ pywc scripts/ 
17 55 512 scripts/
$ pywc -wl scripts/
17 55 scripts/
Snippet code of ptr command
$ ptr
$ ptr 2001:db8::1
$ ptr 4
Try 'ptr --help' for help.
Error: Invalid value for 'IP_ADDRESS': 4 is not a valid ip address
$ poetry add httpx pygments
$ httpx get -q foo:bar -q hello:world
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 325
Content-Type: application/json
Date: Tue, 01 Dec 2020 19:18:52 GMT
Server: gunicorn/19.9.0
"args": {
"foo": "bar",
"hello": "world"
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "",
"User-Agent": "HTTPie/2.0.0",
"X-Amzn-Trace-Id": "Root=1-5fc6979b-38e769ff191d5526046ef5f4"
"origin": "",
"url": ""
$ httpx --no-verify get -q foo:bar -q hello:world
# again we need to have the same answer as before, the difference is that we use https instead of http and we don't want to check the certificate, hence the "--no-verify" option
snippet code of httpx command
$ poetry add anyio rich
$ imgdl --version
imgdl version 0.1.0
$ imgdl -h # note that this time I use "-h" and not "--help"
Downloads multiple images given their urls in FILE and store them in
$ imgdl images.txt ./images
Downloading ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00
Downloads are finished! 🌟



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store