textual icon indicating copy to clipboard operation
textual copied to clipboard

set argv when importing app

Open jspv opened this issue 2 years ago • 2 comments

The fix for #1064 covered only when debugging a script ending in .py, this ensures the correct arguments are passed through to the application when imported.

Example. Using sample/sample.py:

from textual.app import App, ComposeResult
from textual.widgets import Static
from sys import argv

class app(App):
    def __init__(self) -> None:
        self.args = argv
        print(argv)
        super().__init__()

    def compose(self) -> ComposeResult:
        yield Static(f"{self.args}")

if __name__ == "__main__":
    app().run()

Before - it works correctly with scripts ending in .py, but when importing the app the textual path and run command are incorrectly passed to the app:

$ textual run "sample/sample.py arg1 arg2"
['/home/pi/src/sample/sample.py', 'arg1', 'arg2']

$ textual run "sample.sample arg1 arg2"
['/home/pi/.cache/pypoetry/virtualenvs/spotbot-VA7g20E_-py3.10/bin/textual', 'run', 'sample.sample arg1 arg2']

After - just the indicated arguments are passed and argv[0] is set to import_name.

$ textual run "sample/sample.py arg1 arg2"
['/home/pi/src/sample/sample.py', 'arg1', 'arg2']

$ textual run "sample.sample arg1 arg2"
['sample.sample', 'arg1', 'arg2']

jspv avatar Nov 03 '22 23:11 jspv

@davep please review on Monday.

willmcgugan avatar Nov 05 '22 15:11 willmcgugan

Will do. Added to my reminders.

davep avatar Nov 05 '22 16:11 davep