command-line interface (CLI)
A command-line interface (CLI) is a text-based method of interacting with a program by typing commands into a terminal or console. Unlike a graphical user interface (GUI), which relies on visual elements like windows, buttons, and menus, a CLI accepts text input and returns text output. Users type exact commands, so they control precisely what a program does.
In Python, you can build CLIs for your scripts and applications using the standard library module argparse, or third-party libraries like Click and Typer. These tools let you define commands, options, and arguments that users can pass when running a script from the terminal.
CLIs are especially useful for creating developer tools and automation scripts because they support scripting and pipelines.
Example
A minimal CLI built with argparse:
>>> import argparse
>>> parser = argparse.ArgumentParser(description="Greet someone")
>>> parser.add_argument("name", help="name to greet")
>>> args = parser.parse_args(["Pythonista"])
>>> print(f"Hello, {args.name}!")
Hello, Pythonista!
Running the same script from the terminal:
$ python greet.py --help
usage: greet.py [-h] name
Greet someone
positional arguments:
name name to greet
options:
-h, --help show this help message and exit
$ python greet.py Pythonista
Hello, Pythonista!
Related Resources
Tutorial
Build Command-Line Interfaces With Python's argparse
In this step-by-step Python tutorial, you'll learn how to take your command-line Python scripts to the next level by adding a convenient command-line interface (CLI) that you can write with the argparse module from the standard library.
For additional information on related topics, take a look at the following resources:
- Python Command-Line Arguments (Tutorial)
- Build a Command-Line To-Do App With Python and Typer (Tutorial)
- 4 Techniques for Testing Python Command-Line (CLI) Apps (Tutorial)
- The Terminal: First Steps and Useful Commands for Python Developers (Tutorial)
- Click and Python: Build Extensible and Composable CLI Apps (Tutorial)
- Building Command Line Interfaces With argparse (Course)
- Build Command-Line Interfaces With Python's argparse (Quiz)
- Command Line Interfaces in Python (Course)
- Building a Python Command-Line To-Do App With Typer (Course)
- Using the Terminal on Linux (Course)
- Using the Terminal on Windows (Course)
- Using the Terminal on macOS (Course)