Fix Typer Context initialization crashing the CLI and missing clicker requiremnt in pipx toml.

This commit is contained in:
Colin J Fakley
2026-05-30 10:32:29 -04:00
parent 51098c553d
commit 6ac2729239
3 changed files with 27 additions and 28 deletions
+25 -24
View File
@@ -14,7 +14,7 @@ from pathlib import Path
import click
from rich.console import Console
from typer import Option, Typer
from typer import Option, Typer, Context
from typer.core import TyperGroup
import cli.modules
@@ -79,6 +79,7 @@ def setup_logging(log_level: str = "WARNING") -> None:
@app.callback(invoke_without_command=True)
def main(
ctx: Context,
_version: bool | None = Option(
None,
"--version",
@@ -104,9 +105,6 @@ def main(
# Silence all logging (including third-party) unless user explicitly requests it
logging.disable(logging.CRITICAL)
# Get context without type annotation (compatible with all Typer versions)
ctx = click.get_current_context()
# Store log level in context for potential use by other commands
ctx.ensure_object(dict)
ctx.obj["log_level"] = log_level
@@ -241,26 +239,29 @@ def run() -> None:
except (ValueError, IndexError):
pass # Let Typer handle argument parsing errors
try:
init_app()
app()
except (ValueError, RuntimeError) as e:
# Handle configuration and initialization errors cleanly
display.error(str(e))
sys.exit(1)
except ImportError as e:
# Handle module import errors with detailed info
display.error(f"Module Import Error: {e}")
sys.exit(1)
except KeyboardInterrupt:
# Handle Ctrl+C gracefully
display.warning("Operation cancelled by user")
sys.exit(130)
except Exception as e:
# Handle unexpected errors - show simplified message
display.error(str(e))
display.info("Use --log-level DEBUG for more details")
sys.exit(1)
init_app()
app()
# try:
# init_app()
# app()
# except (ValueError, RuntimeError) as e:
# # Handle configuration and initialization errors cleanly
# display.error(str(e))
# sys.exit(1)
# except ImportError as e:
# # Handle module import errors with detailed info
# display.error(f"Module Import Error: {e}")
# sys.exit(1)
# except KeyboardInterrupt:
# # Handle Ctrl+C gracefully
# display.warning("Operation cancelled by user")
# sys.exit(130)
# except Exception as e:
# # Handle unexpected errors - show simplified message
# display.error(str(e))
# display.info("Use --log-level DEBUG for more details")
# sys.exit(1)
if __name__ == "__main__":
+1 -4
View File
@@ -194,10 +194,7 @@ def apply_cli_overrides(template, var: list[str] | None, ctx=None) -> None:
# Get context if not provided (compatible with all Typer versions)
if ctx is None:
try:
ctx = click.get_current_context()
except RuntimeError:
ctx = None
ctx = click.get_current_context(silent=True)
extra_args = list(ctx.args) if ctx and hasattr(ctx, "args") else []
cli_overrides = parse_var_inputs(var or [], extra_args)
+1
View File
@@ -23,6 +23,7 @@ dependencies = [
"python-frontmatter>=1.0.0",
"Jinja2>=3.0",
"email-validator>=2.0.0",
"click==8.1.4",
]
[project.optional-dependencies]