skillfed

cli-exit-tools

functions to exit an cli application properly

cli-exit-tools v1.2.7 2.5M downloads/30d#3,015 on PyPI1
Permissive license MIT Active released

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-tools

uv

uv add cli-exit-tools

poetry

poetry add cli-exit-tools

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: PythonTopic :: Software Development :: Libraries :: Python Modules

Tags

cli exit code handlingpython exception to exit codecli application cleanupstream flushing utilitiestraceback printing for cliproper cli shutdownexception exit handler
cli-utilitiesexception-handling

More Python Modules packages