--- id: cli-exit-tools version: "1.2.7" license: MIT license_treatment: permissive maintenance: active --- # cli-exit-tools — functions to exit an cli application properly License: permissive · Maintenance: active · Downloads: 2.5M/mo ## 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 above — 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 pip install cli-exit-tools uv add cli-exit-tools 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_current - Install friction: low - Maintenance: active - Downloads: 2.5M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags cli exit code handling, python exception to exit code, cli application cleanup, stream flushing utilities, traceback printing for cli, proper cli shutdown, exception exit handler, cli-utilities, exception-handling [View on SkillFed](https://skillfed.io/packages/cli-exit-tools) · [View on PyPI](https://pypi.org/project/cli-exit-tools/)