{"id":16495648,"url":"https://github.com/bitranox/cli_exit_tools","last_synced_at":"2025-06-26T20:08:54.933Z","repository":{"id":39021354,"uuid":"282888759","full_name":"bitranox/cli_exit_tools","owner":"bitranox","description":"functions to exit an cli application properly","archived":false,"fork":false,"pushed_at":"2025-06-12T10:19:04.000Z","size":184,"stargazers_count":1,"open_issues_count":7,"forks_count":3,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-12T11:38:13.753Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bitranox.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-07-27T12:17:16.000Z","updated_at":"2025-01-24T23:22:17.000Z","dependencies_parsed_at":"2024-03-09T05:21:44.900Z","dependency_job_id":"e1ae7ce5-2a91-456f-8579-59a92347368a","html_url":"https://github.com/bitranox/cli_exit_tools","commit_stats":{"total_commits":193,"total_committers":2,"mean_commits":96.5,"dds":"0.010362694300518172","last_synced_commit":"4bf9d4f40715ec5dace1257f050c1caefd14385a"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/bitranox/cli_exit_tools","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitranox%2Fcli_exit_tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitranox%2Fcli_exit_tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitranox%2Fcli_exit_tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitranox%2Fcli_exit_tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitranox","download_url":"https://codeload.github.com/bitranox/cli_exit_tools/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitranox%2Fcli_exit_tools/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259459298,"owners_count":22861119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T14:31:53.622Z","updated_at":"2025-06-13T09:38:14.818Z","avatar_url":"https://github.com/bitranox.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"cli_exit_tools\n==============\n\n\nVersion v1.2.7 as of 2024-10-02 see `Changelog`_\n\n|build_badge| |codeql| |license| |jupyter| |pypi|\n|pypi-downloads| |black| |codecov| |cc_maintain| |cc_issues| |cc_coverage| |snyk|\n\n\n\n.. |build_badge| image:: https://github.com/bitranox/cli_exit_tools/actions/workflows/python-package.yml/badge.svg\n   :target: https://github.com/bitranox/cli_exit_tools/actions/workflows/python-package.yml\n\n\n.. |codeql| image:: https://github.com/bitranox/cli_exit_tools/actions/workflows/codeql-analysis.yml/badge.svg?event=push\n   :target: https://github.com//bitranox/cli_exit_tools/actions/workflows/codeql-analysis.yml\n\n.. |license| image:: https://img.shields.io/github/license/webcomics/pywine.svg\n   :target: http://en.wikipedia.org/wiki/MIT_License\n\n.. |jupyter| image:: https://mybinder.org/badge_logo.svg\n   :target: https://mybinder.org/v2/gh/bitranox/cli_exit_tools/master?filepath=cli_exit_tools.ipynb\n\n.. for the pypi status link note the dashes, not the underscore !\n.. |pypi| image:: https://img.shields.io/pypi/status/cli-exit-tools?label=PyPI%20Package\n   :target: https://badge.fury.io/py/cli_exit_tools\n\n.. badge until 2023-10-08:\n.. https://img.shields.io/codecov/c/github/bitranox/cli_exit_tools\n.. badge from 2023-10-08:\n.. |codecov| image:: https://codecov.io/gh/bitranox/cli_exit_tools/graph/badge.svg\n   :target: https://codecov.io/gh/bitranox/cli_exit_tools\n\n.. |cc_maintain| image:: https://img.shields.io/codeclimate/maintainability-percentage/bitranox/cli_exit_tools?label=CC%20maintainability\n   :target: https://codeclimate.com/github/bitranox/cli_exit_tools/maintainability\n   :alt: Maintainability\n\n.. |cc_issues| image:: https://img.shields.io/codeclimate/issues/bitranox/cli_exit_tools?label=CC%20issues\n   :target: https://codeclimate.com/github/bitranox/cli_exit_tools/maintainability\n   :alt: Maintainability\n\n.. |cc_coverage| image:: https://img.shields.io/codeclimate/coverage/bitranox/cli_exit_tools?label=CC%20coverage\n   :target: https://codeclimate.com/github/bitranox/cli_exit_tools/test_coverage\n   :alt: Code Coverage\n\n.. |snyk| image:: https://snyk.io/test/github/bitranox/cli_exit_tools/badge.svg\n   :target: https://snyk.io/test/github/bitranox/cli_exit_tools\n\n.. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg\n   :target: https://github.com/psf/black\n\n.. |pypi-downloads| image:: https://img.shields.io/pypi/dm/cli-exit-tools\n   :target: https://pypi.org/project/cli-exit-tools/\n   :alt: PyPI - Downloads\n\nsmall toolset to properly exit a cli application:\n\n- print the traceback information (can be set with commandline option)\n- get a proper exit code from the Exception\n- flush the streams, to make sure output is written in proper order\n- demo how to integrate into Your cli module (see usage)\n\n----\n\nautomated tests, Github Actions, Documentation, Badges, etc. are managed with `PizzaCutter \u003chttps://github\n.com/bitranox/PizzaCutter\u003e`_ (cookiecutter on steroids)\n\nPython version required: 3.8.0 or newer\n\ntested on recent linux with python 3.8, 3.9, 3.10, 3.11, 3.12, pypy-3.9, pypy-3.10, graalpy-24.1 - architectures: amd64\n\n`100% code coverage \u003chttps://codeclimate.com/github/bitranox/cli_exit_tools/test_coverage\u003e`_, flake8 style checking ,mypy static type checking ,tested under `Linux, macOS, Windows \u003chttps://github.com/bitranox/cli_exit_tools/actions/workflows/python-package.yml\u003e`_, automatic daily builds and monitoring\n\n----\n\n- `Try it Online`_\n- `Usage`_\n- `Usage from Commandline`_\n- `Installation and Upgrade`_\n- `Requirements`_\n- `Acknowledgements`_\n- `Contribute`_\n- `Report Issues \u003chttps://github.com/bitranox/cli_exit_tools/blob/master/ISSUE_TEMPLATE.md\u003e`_\n- `Pull Request \u003chttps://github.com/bitranox/cli_exit_tools/blob/master/PULL_REQUEST_TEMPLATE.md\u003e`_\n- `Code of Conduct \u003chttps://github.com/bitranox/cli_exit_tools/blob/master/CODE_OF_CONDUCT.md\u003e`_\n- `License`_\n- `Changelog`_\n\n----\n\nTry it Online\n-------------\n\nYou might try it right away in Jupyter Notebook by using the \"launch binder\" badge, or click `here \u003chttps://mybinder.org/v2/gh/{{rst_include.\nrepository_slug}}/master?filepath=cli_exit_tools.ipynb\u003e`_\n\nUsage\n-----------\n\n- example for the main_cli\n\n.. code-block:: python\n\n    # STDLIB\n    import platform\n    import signal\n    import sys\n    from typing import Any\n    from typing import Callable\n    from typing import Optional\n    from typing import Union\n    from types import FrameType\n\n    # EXT\n    import click\n\n    # CONSTANTS\n    CLICK_CONTEXT_SETTINGS = dict(help_option_names=[\"-h\", \"--help\"])\n\n    try:\n        from . import __init__conf__\n        from . import cli_exit_tools\n    except (ImportError, ModuleNotFoundError):  # pragma: no cover\n        # imports for doctest\n        import __init__conf__  # type: ignore  # pragma: no cover\n        import cli_exit_tools  # type: ignore  # pragma: no cover\n\n    is_platform_windows = platform.system().lower() == \"windows\"\n    is_platform_linux = platform.system().lower() == \"linux\"\n    is_platform_darwin = platform.system().lower() == \"darwin\"\n    is_platform_posix = not is_platform_windows\n\n\n    class SigIntError(Exception):\n        \"\"\"wird bei Signal SigInt ausgelöst\"\"\"\n        pass\n\n\n    class SigTermError(Exception):\n        \"\"\"wird bei Signal SigTerm ausgelöst\"\"\"\n        pass\n\n\n    if is_platform_windows:\n        \"\"\"import win32 api on windows systems\"\"\"\n        try:\n            import win32api  # type: ignore # noqa\n        except ModuleNotFoundError:  # for install_python_libs_python3.py - at that time pywin32 (win32api) might not be installed\n            pass\n\n\n    def _set_signal_handlers() -\u003e None:\n        \"\"\"\n        setzt die signal handler so, das entsprechende Exceptions geraised werden.\n        Dies dient dazu ein sauberes Handling für Cleanup in den Applikationen\n        zu gewährleisten\n        \"\"\"\n        # sigterm handler setzen\n        if is_platform_linux:\n            signal.signal(signal.SIGTERM, _sigterm_handler)\n        elif is_platform_windows:\n            try:\n                win32api.SetConsoleCtrlHandler(_sigterm_handler, True)\n            except NameError:  # for install_python_libs_python3.py - at that time pywin32 (win32api) might not be installed\n                pass\n\n        # sigint handler setzen\n        signal.signal(signal.SIGINT, _sigint_handler)\n\n\n    def _sigint_handler(_signo: int, _stack_frame: Optional[FrameType]) -\u003e None:\n        raise SigIntError\n\n\n    def _sigterm_handler(_signo: int, _stack_frame: Optional[FrameType]) -\u003e None:\n        raise SigTermError\n\n\n    def info() -\u003e None:\n        \"\"\"\n        \u003e\u003e\u003e info()\n        Info for ...\n\n        \"\"\"\n        __init__conf__.print_info()\n\n\n    @click.group(help=__init__conf__.title, context_settings=CLICK_CONTEXT_SETTINGS)    # type: ignore\n    @click.version_option(\n        version=__init__conf__.version, prog_name=__init__conf__.shell_command, message=f\"{__init__conf__.shell_command} version {__init__conf__.version}\"\n    )\n    @click.option(\"--traceback/--no-traceback\", is_flag=True, type=bool, default=None, help=\"return traceback information on cli\")\n    def cli_main(traceback: Optional[bool] = None) -\u003e None:\n        if traceback is not None:\n            cli_exit_tools.config.traceback = traceback\n\n\n    @cli_main.command(\"info\", context_settings=CLICK_CONTEXT_SETTINGS)  # type: ignore\n    def cli_info() -\u003e None:\n        \"\"\"get program information\"\"\"\n        info()\n\n\n    # entry point if main\n    if __name__ == \"__main__\":\n        try:\n            _set_signal_handlers()\n            cli_main()      # type: ignore\n        except Exception as exc:\n            cli_exit_tools.print_exception_message()\n            sys.exit(cli_exit_tools.get_system_exit_code(exc))\n        finally:\n            cli_exit_tools.flush_streams()\n\n- get the system exit code\n\n.. code-block:: python\n\n    def get_system_exit_code(exc: BaseException) -\u003e int:\n        \"\"\"\n        Return the exit code for linux or Windows os, based on the exception.\n        If, on windows, the winerror code is passed with the Exception, we return that winerror code.\n\n\n        Parameter\n        ---------\n        exc\n            the exception to analyze\n\n\n        Result\n        ------\n        exit_code\n            as integer\n\n\n        Examples\n        --------\n\n        \u003e\u003e\u003e try:\n        ...     raise RuntimeError()\n        ... except RuntimeError as my_exc:\n        ...     assert get_system_exit_code(my_exc) == 1\n        ...     setattr(my_exc, 'winerror', 42)\n        ...     assert get_system_exit_code(my_exc) == 42\n        ...     setattr(my_exc, 'winerror', None)\n        ...     assert get_system_exit_code(my_exc) == 1\n        \u003e\u003e\u003e try:\n        ...     exit(99)\n        ... except SystemExit as my_exc:\n        ...     assert get_system_exit_code(my_exc) == 99\n\n        \"\"\"\n\n- print the exception message\n\n.. code-block:: python\n\n    def print_exception_message(trace_back: bool = config.traceback, length_limit: int = 500, stream: Optional[TextIO] = None) -\u003e None:\n        \"\"\"\n        Prints the Exception Message to stderr. If trace_back is True, it also prints the traceback information.\n        If the exception has stdout, stderr attributes (like subprocess.CalledProcessError), those will also be printed.\n\n        Parameters\n        ----------\n        trace_back : bool, optional\n            Whether to print traceback information. Default is False.\n        length_limit : int, optional\n            Maximum length of the exception message to be printed. Default is 500.\n        stream : Optional[TextIO], optional\n            The stream to print to. Default is sys.stderr.\n\n        Examples\n        --------\n\n        \u003e\u003e\u003e # test with exc_info = None\n        \u003e\u003e\u003e print_exception_message()\n\n        \u003e\u003e\u003e # test with exc_info\n        \u003e\u003e\u003e try:\n        ...     raise FileNotFoundError('unknown_command_test1')\n        ... except Exception:       # noqa\n        ...     print_exception_message(True, length_limit=15, stream=sys.stdout)\n        ...     print_exception_message(False, stream=sys.stdout)\n        ...     print_exception_message(True, stream=sys.stdout)\n        Traceback Info...\n\n        \u003e\u003e\u003e # test with subprocess to get stdout, stderr\n        \u003e\u003e\u003e import subprocess\n        \u003e\u003e\u003e try:\n        ...     discard=subprocess.run('unknown_command_test2', shell=True, check=True)\n        ... except subprocess.CalledProcessError:\n        ...     print_exception_message(False, stream=sys.stdout)\n        ...     print_exception_message(True, stream=sys.stdout)\n        ...     print_exception_message(True, stream=sys.stdout)\n        CalledProcessError...\n\n        \"\"\"\n\n- flush the streams\n\n.. code-block:: python\n\n    def flush_streams() -\u003e None:\n        \"\"\"\n        flush the streams - make sure the output is written early,\n        otherwise the output might be printed even after another CLI\n        command is launched\n\n\n        Examples\n        --------\n\n\n        \u003e\u003e\u003e flush_streams()\n\n        \"\"\"\n\nUsage from Commandline\n------------------------\n\n.. code-block::\n\n   Usage: cli_exit_tools [OPTIONS] COMMAND [ARGS]...\n\n     functions to exit an cli application properly\n\n   Options:\n     --version                     Show the version and exit.\n     --traceback / --no-traceback  return traceback information on cli\n     -h, --help                    Show this message and exit.\n\n   Commands:\n     info  get program information\n\nInstallation and Upgrade\n------------------------\n\n- Before You start, its highly recommended to update pip:\n\n\n.. code-block::\n\n    python -m pip --upgrade pip\n\n- to install the latest release from PyPi via pip (recommended):\n\n.. code-block::\n\n    python -m pip install --upgrade cli_exit_tools\n\n\n- to install the latest release from PyPi via pip, including test dependencies:\n\n.. code-block::\n\n    python -m pip install --upgrade cli_exit_tools[test]\n\n- to install the latest version from github via pip:\n\n\n.. code-block::\n\n    python -m pip install --upgrade git+https://github.com/bitranox/cli_exit_tools.git\n\n\n- include it into Your requirements.txt:\n\n.. code-block::\n\n    # Insert following line in Your requirements.txt:\n    # for the latest Release on pypi:\n    cli_exit_tools\n\n    # for the latest development version :\n    cli_exit_tools @ git+https://github.com/bitranox/cli_exit_tools.git\n\n    # to install and upgrade all modules mentioned in requirements.txt:\n    python -m pip install --upgrade -r /\u003cpath\u003e/requirements.txt\n\n\n- to install the latest development version, including test dependencies from source code:\n\n.. code-block::\n\n    # cd ~\n    $ git clone https://github.com/bitranox/cli_exit_tools.git\n    $ cd cli_exit_tools\n    python -m pip install -e .[test]\n\n- via makefile:\n  makefiles are a very convenient way to install. Here we can do much more,\n  like installing virtual environments, clean caches and so on.\n\n.. code-block:: shell\n\n    # from Your shell's homedirectory:\n    $ git clone https://github.com/bitranox/cli_exit_tools.git\n    $ cd cli_exit_tools\n\n    # to run the tests:\n    $ make test\n\n    # to install the package\n    $ make install\n\n    # to clean the package\n    $ make clean\n\n    # uninstall the package\n    $ make uninstall\n\nRequirements\n------------\nfollowing modules will be automatically installed :\n\n.. code-block:: bash\n\n    ## Project Requirements\n    click\n    lib_detect_testenv\n\nAcknowledgements\n----------------\n\n- special thanks to \"uncle bob\" Robert C. Martin, especially for his books on \"clean code\" and \"clean architecture\"\n\nContribute\n----------\n\nI would love for you to fork and send me pull request for this project.\n- `please Contribute \u003chttps://github.com/bitranox/cli_exit_tools/blob/master/CONTRIBUTING.md\u003e`_\n\nLicense\n-------\n\nThis software is licensed under the `MIT license \u003chttp://en.wikipedia.org/wiki/MIT_License\u003e`_\n\n---\n\nChangelog\n=========\n\n- new MAJOR version for incompatible API changes,\n- new MINOR version for added functionality in a backwards compatible manner\n- new PATCH version for backwards compatible bug fixes\n\n\nv1.2.7\n---------\n2024-10-02:\n    - setup-python@v5\n    - sigterm_handler for CLI\n    - graalpy 24.1 tests\n    - CODECOV_TOKEN\n    - refractor\n\nv1.2.6\n---------\n2023-07-14:\n    - add codeql badge\n    - move 3rd_party_stubs outside the src directory\n    - add pypy 3.10 tests\n    - add python 3.12-dev tests\n\nv1.2.5\n---------\n2023-07-13:\n    - require minimum python 3.8\n    - remove python 3.7 tests\n\nv1.2.4\n---------\n2023-07-12:\n    - introduce PEP517 packaging standard\n    - introduce pyproject.toml build-system\n    - remove mypy.ini\n    - remove pytest.ini\n    - remove setup.cfg\n    - remove setup.py\n    - remove .bettercodehub.yml\n    - remove .travis.yml\n    - update black config\n    - clean ./tests/test_cli.py\n\nv1.2.3.2\n---------\n2022-06-02: update to github actions checkout@v3 and setup-python@v3\n\nv1.2.3.1\n--------\n2022-06-01: update github actions test matrix\n\nv1.2.3\n--------\n2022-03-29: remedy mypy Untyped decorator makes function \"cli_info\" untyped\n\nv1.2.2\n--------\n2022-03-25: fix github actions windows test\n\nv1.2.1\n-------\n2021-11-22: Patch Release\n    - fix minor readme.rst bugs\n    - remove second github action yml\n    - fix \"setup.py test\"\n\nv1.2.0\n------\n2021-11-21: Minor Release\n    - implement github actions\n    - implement system.exit()\n\nv1.1.8\n--------\n2020-10-09: service release\n    - update travis build matrix for linux 3.9-dev\n    - update travis build matrix (paths) for windows 3.9 / 3.10\n\nv1.1.7\n--------\n2020-08-08: service release\n    - fix documentation\n    - fix travis\n    - deprecate pycodestyle\n    - implement flake8\n\nv1.1.6\n--------\n2020-08-07: fix wheels\n\nv1.1.5\n--------\n2020-07-31: fix wheels\n\nv1.1.3\n--------\n2020-07-31: initial release\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitranox%2Fcli_exit_tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitranox%2Fcli_exit_tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitranox%2Fcli_exit_tools/lists"}