Pasu4
Pasu4

Reputation: 11

How can I print a help message to the console from a pyproject GUI script?

I have a python project that uses a pyproject.toml file to define a GUI script:

[project.gui-scripts]
my-script = "my_project.cli:cli_run"

I have a --help option that should print a help message to the console, but it doesn't, since the script is started in a separate process. If I use [project.scripts] instead, the problem is that it is tied to the console it is called from, which is also not desirable, since if I close the console, it also closes the GUI. So, is there a way to make it print a help message if I specify --help but still detach from the console otherwise?

In case it helps, the project uses setuptools and wxpython.

I tried:

Minimal example

Project structure:

my-project
├── pyproject.toml
└── src
    └── my_project
        └── cli.py

pyproject.toml

[build-system]
requires = ["setuptools >= 64"]
build-backend = "setuptools.build_meta"

[project]
name = "my-project"
version = "0.1.0"

dependencies = [
    "wxPython~=4.2.2",
]

[project.gui-scripts]
my-project = "my_project.cli:cli_run"

cli.py

import sys
import wx

def cli_run() -> None:
    if "--help" in sys.argv:
        print("Help message")
        return
    
    app = wx.App()
    frame = wx.Frame(None, title="Hello, World!")
    frame.Show()
    app.MainLoop()

Run pip install . to install and then my-project to run the application.

Upvotes: 1

Views: 37

Answers (0)

Related Questions