4.7 Your first CLI
Libraries
For building a cli
(Command Line Interface) you can write pure Python (no external libraries) or use a library like:
Getting started
Install
click
library.pip install click
Create the directory and the file structure.
cli # directory project ├── devops_cli # directory package │ ├── __init__.py │ └── devops_cli.py # your script └── setup.py
Note
The Python interpreter is informed that a directory contains code for a Python module by the
__init__.py
file. A file named__init__.py
may be empty. You cannot import modules into your project from another location without one.Write your first
cli
.import click import psutil @click.group() def cli(): """Simple command line tool to extract system information.""" @cli.command("cpu", short_help="Show CPU") def cpu(): """Shows CPU Resources.""" click.echo(psutil.cpu_times()) @cli.command("disk", short_help="Show Disk") def disk(): """Shows Disk Status.""" click.echo(psutil.disk_partitions())