cli-exit-tools
functions to exit an cli application properly
What it is and what it does
cli_exit_tools is a small utility library that standardizes how Python CLI applications shut down cleanly. It provides three main functions: extracting the correct system exit code from an exception (including Windows winerror codes), printing exception messages with optional traceback to stderr, and flushing output streams to ensure output appears in the correct order before exit.
The package is designed to integrate into the exception handler of a click-based CLI application or any other command-line tool. It handles platform differences (Windows, Linux, macOS) transparently and works with exceptions that carry subprocess stdout/stderr attributes. The library has no external dependencies beyond click and lib-detect-testenv, making it lightweight to add to existing projects.
Use it for:
- Ensure a click-based CLI application exits with the correct code when an exception occurs, including Windows-specific winerror codes.
- Print exception messages and optional tracebacks to stderr in a controlled way before exiting a CLI tool.
- Flush stdout and stderr streams in the right order to prevent buffered output from appearing after the program has already exited.
- Integrate signal handlers (SIGINT, SIGTERM) into a CLI application to raise custom exceptions for clean shutdown.
- Display subprocess errors (from CalledProcessError) with their captured stdout and stderr alongside the exception message.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Handles proper exit procedures for CLI applications: prints traceback information, derives correct exit codes from exceptions, and flushes output streams in the right order.
Yes. The package is lightweight, actively maintained, has no known vulnerabilities, and solves a real problem in CLI development—proper exception handling and stream flushing are easy to get wrong and this library handles the platform-specific details. Install it if you're building a CLI tool with click or similar frameworks and want reliable, correct exit behavior.
Install
cli-exit-tools on PyPI
pip
pip install cli-exit-toolsuv
uv add cli-exit-toolspoetry
poetry add cli-exit-toolsInstalling cli-exit-tools
Before you install
Low friction install with only two runtime dependencies (click and lib-detect-testenv). Actively maintained with recent commits and tested across Python 3.8, 3.9, 3.10, 3.11, 3.12, and PyPy variants.
License in practice
MIT license (permissive) — you can use, modify, and distribute this package freely in commercial or private projects with minimal restrictions.
Quickstart
import sys
import cli_exit_tools
try:
# your CLI code here
pass
except Exception as exc:
cli_exit_tools.print_exception_message()
sys.exit(cli_exit_tools.get_system_exit_code(exc))
finally:
cli_exit_tools.flush_streams()
Requires Python 3.8.0 or newer.
Verify before relying
- Whether lib-detect-testenv is a runtime dependency or a development-only tool (not clear from fact sheet).
- Specific behavior differences across Windows, Linux, and macOS for signal handling and exit codes.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — click, lib-detect-testenv |
| Maintenance | actively maintained — 681 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,536,130/month — #3,015 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: cli_exit_tools-1.2.7-py3-none-any.whl
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
lib-cli-exit-toolsProvides portable signal handling,…
permissive · top 15,000 on PyPI
wrapt-timeout-decoratorApplies timeout limits to Python functions and…
permissive · top 5,000 on PyPI
scripttestscripttest runs command-line applications in…
permissive · top 15,000 on PyPI
stackprinterReplaces Python's default exception traceback…
permissive · top 15,000 on PyPI
subprocess-multiteeMultiplexes subprocess stdout/stderr to…
unclear · top 15,000 on PyPI
traceriteFormats Python exceptions and tracebacks into…
permissive · top 5,000 on PyPI
EasyProcessEasyProcess wraps Python's subprocess module to…
permissive · top 5,000 on PyPI
exitstatusProvides portable, type-safe POSIX exit status…
permissive · top 15,000 on PyPI
click-help-colorsAdds color customization to Click command-line…
permissive · top 5,000 on PyPI
subprocess-teeA drop-in replacement for subprocess.run that…
permissive · top 5,000 on PyPI