Last active
April 25, 2026 10:16
-
-
Save vnvmar/679e7590b5313fd082c1b9c19a864589 to your computer and use it in GitHub Desktop.
A simple __main__.py file to turn any folder into an autodiscovering typer app composer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pathlib import Path | |
| import importlib | |
| import typer | |
| app = typer.Typer( | |
| no_args_is_help=True | |
| ) | |
| parent_dir = Path(__file__).parent | |
| for file in parent_dir.glob("*.py"): | |
| if file.name.startswith("_") or file.name == Path(__file__).name: | |
| continue | |
| name = f"{parent_dir.stem}.{file.stem}" | |
| mod = importlib.import_module(name) | |
| sub = getattr(mod, "app", None) | |
| if sub is not None: | |
| app.add_typer(sub, name=file.stem) | |
| app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment