{"id":3924,"library":"cli-exit-tools","title":"cli-exit-tools","description":"cli-exit-tools (v1.2.7) is an actively maintained Python library designed to facilitate the proper and controlled exiting of command-line interface (CLI) applications. It provides functionalities to print formatted exception messages, optionally include traceback information, retrieve appropriate exit codes from exceptions, and ensure output streams are flushed correctly before termination. The library targets Python 3.8 and newer, with a focus on robust error handling for CLI tools.","status":"active","version":"1.2.7","language":"en","source_language":"en","source_url":"https://github.com/bitranox/cli_exit_tools.git","tags":["cli","exit","error handling","traceback","command-line","utility"],"install":[{"cmd":"pip install cli-exit-tools","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for command-line interface foundations.","package":"click","optional":false},{"reason":"Used for detecting and configuring behavior in test environments.","package":"lib-detect-testenv","optional":false}],"imports":[{"note":"The core utilities like `print_exception_message` and `get_exit_code_from_exception` are part of the `cli_exit_tools` module within the `cli_exit_tools` package.","wrong":"from cli_exit_tools import print_exception_message","symbol":"cli_exit_tools","correct":"from cli_exit_tools import cli_exit_tools"}],"quickstart":{"code":"import sys\nfrom cli_exit_tools import cli_exit_tools\n\ndef run_cli_task():\n    \"\"\"Simulates a task in a CLI application that might raise an error.\"\"\"\n    try:\n        # Simulate an operation that causes an error, e.g., division by zero\n        # Or an invalid type conversion\n        value = int(\"not_a_number\")\n        print(f\"Processed value: {value}\")\n        return 0 # Success exit code\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\", file=sys.stderr)\n        # Use cli_exit_tools to print detailed exception message and get an appropriate exit code\n        cli_exit_tools.print_exception_message(trace_back=True, stream=sys.stderr)\n        return cli_exit_tools.get_exit_code_from_exception(e)\n\nif __name__ == \"__main__\":\n    # In a real CLI application, you would typically call sys.exit(exit_code)\n    exit_code = run_cli_task()\n    print(f\"\\nApplication finished with exit code: {exit_code}\", file=sys.stderr)\n    # sys.exit(exit_code)","lang":"python","description":"This quickstart demonstrates how to use `cli_exit_tools` to gracefully handle an exception within a simulated CLI task. It shows how to print a detailed exception message, including traceback, to `stderr` and obtain a proper exit code from the exception, which can then be used with `sys.exit()`."},"warnings":[{"fix":"Explicitly pass `trace_back=True` to `cli_exit_tools.print_exception_message()` if full tracebacks are desired regardless of external configuration.","message":"Default traceback behavior might be unexpected. The `print_exception_message` function's `trace_back` parameter defaults to a value (often from an internal `config` object) that can be influenced by CLI options. Developers should explicitly set `trace_back=True` if they consistently want full tracebacks, or ensure their CLI configuration correctly controls this behavior, otherwise, tracebacks might be suppressed by default.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always import the module as `from cli_exit_tools import cli_exit_tools` and then call functions like `cli_exit_tools.print_exception_message()`.","message":"Incorrect import path for core utilities. Users commonly attempt to import functions like `print_exception_message` directly from the top-level `cli_exit_tools` package. However, these utilities are located within the `cli_exit_tools` module itself, requiring `from cli_exit_tools import cli_exit_tools` to access them.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pin your `cli-exit-tools` version in `requirements.txt` or similar, and carefully review release notes and changelogs for any changes to exit codes or output formats when upgrading. Implement robust parsing that is resilient to minor output variations.","message":"Changes to exit code semantics or output structure can break consuming scripts. As with any CLI utility, future versions changing the default exit codes returned by `get_exit_code_from_exception` or modifying the format of messages printed by `print_exception_message` (e.g., switching from plain text to JSON, or reordering output) could break automated scripts or parsers relying on consistent output.","severity":"breaking","affected_versions":"Any future major or minor version"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}