From 14d1cf27afbf887e4c10cb16f3f653f93a4e7aa4 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 20 Dec 2022 21:36:52 +0000 Subject: [PATCH] Add a FAQ about passing arguments to an application --- questions/pass-args-to-app.question.md | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 questions/pass-args-to-app.question.md diff --git a/questions/pass-args-to-app.question.md b/questions/pass-args-to-app.question.md new file mode 100644 index 000000000..3fef2a326 --- /dev/null +++ b/questions/pass-args-to-app.question.md @@ -0,0 +1,38 @@ +--- +title: "How do I pass arguments to an app?" +alt_titles: + - "pass arguments to an application" + - "pass parameters to an app" + - "pass parameters to an application" +--- + +When creating your `App` class, override `__init__` as you would when +inheriting normally. For example: + +```python +from textual.app import App, ComposeResult +from textual.widgets import Static + +class Greetings(App[None]): + + def __init__(self, greeting: str="Hello", to_greet: str="World") -> None: + self.greeting = greeting + self.to_greet = to_greet + super().__init__() + + def compose(self) -> ComposeResult: + yield Static(f"{self.greeting}, {self.to_greet}") +``` + +Then the app can be run, passing in various arguments; for example: + +```python +# Running with default arguments. +Greetings().run() + +# Running with a keyword arguyment. +Greetings(to_greet="davep").run() + +# Running with both positional arguments. +Greetings("Well hello", "there").run() +```