Skip to content

Commit 99e5c53

Browse files
committed
Add way to add custom args for bridge programs
1 parent 0e5359c commit 99e5c53

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

mautrix/bridge/bridge.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Bridge:
4343
startup_actions: Optional[Iterable[Awaitable]]
4444
shutdown_actions: Optional[Iterable[Awaitable]]
4545
name: str
46+
args: argparse.Namespace
4647
version: str
4748
repo_url: str
4849
markdown_version: str
@@ -55,22 +56,12 @@ def __init__(self, name: str = None, description: str = None, command: str = Non
5556
config_class: Type[BaseBridgeConfig] = None,
5657
matrix_class: Type[BaseMatrixHandler] = None,
5758
state_store_class: Type[SQLStateStore] = None) -> None:
58-
self.parser = argparse.ArgumentParser(description=description or self.description,
59-
prog=command or self.command)
60-
self.parser.add_argument("-c", "--config", type=str, default="config.yaml",
61-
metavar="<path>", help="the path to your config file")
62-
self.parser.add_argument("-b", "--base-config", type=str, default="example-config.yaml",
63-
metavar="<path>", help="the path to the example config "
64-
"(for automatic config updates)")
65-
self.parser.add_argument("-g", "--generate-registration", action="store_true",
66-
help="generate registration and quit")
67-
self.parser.add_argument("-r", "--registration", type=str, default="registration.yaml",
68-
metavar="<path>",
69-
help="the path to save the generated registration to (not needed "
70-
"for running the bridge)")
71-
7259
if name:
7360
self.name = name
61+
if description:
62+
self.description = description
63+
if command:
64+
self.command = command
7465
if real_user_content_key:
7566
self.real_user_content_key = real_user_content_key
7667
if version:
@@ -92,15 +83,30 @@ def run(self) -> None:
9283
self._prepare()
9384
self._run()
9485

86+
def prepare_arg_parser(self) -> None:
87+
self.parser = argparse.ArgumentParser(description=self.description, prog=self.command)
88+
self.parser.add_argument("-c", "--config", type=str, default="config.yaml",
89+
metavar="<path>", help="the path to your config file")
90+
self.parser.add_argument("-b", "--base-config", type=str, default="example-config.yaml",
91+
metavar="<path>", help="the path to the example config "
92+
"(for automatic config updates)")
93+
self.parser.add_argument("-g", "--generate-registration", action="store_true",
94+
help="generate registration and quit")
95+
self.parser.add_argument("-r", "--registration", type=str, default="registration.yaml",
96+
metavar="<path>",
97+
help="the path to save the generated registration to (not needed "
98+
"for running the bridge)")
99+
95100
def _prepare(self) -> None:
96101
start_ts = time()
97-
args = self.parser.parse_args()
102+
self.prepare_arg_parser()
103+
self.args = self.parser.parse_args()
98104

99-
self.prepare_config(args.config, args.registration, args.base_config)
105+
self.prepare_config(self.args.config, self.args.registration, self.args.base_config)
100106
self.prepare_log()
101-
self.check_config(check_tokens=not args.generate_registration)
107+
self.check_config(check_tokens=not self.args.generate_registration)
102108

103-
if args.generate_registration:
109+
if self.args.generate_registration:
104110
self.generate_registration()
105111
sys.exit(0)
106112

0 commit comments

Comments
 (0)