Skip to content

Commit

Permalink
Use a callback to prevent recursively adding typer to every subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkram committed Oct 23, 2024
1 parent e0ea191 commit 47e0f5f
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion binstar_client/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
from argparse import ArgumentParser
from typing import Any
from typing import Callable
from typing import Optional

import typer
import typer.colors
from anaconda_cli_base import console
from anaconda_cli_base.cli import app as main_app
from typer import Context, Typer

Expand Down Expand Up @@ -69,10 +71,28 @@
name="org",
help="Interact with anaconda.org",
no_args_is_help=True,
context_settings={"help_option_names": ["-h", "--help"]},
)


@app.callback(invoke_without_command=True, no_args_is_help=True)
def main(
ctx: typer.Context,
show_help: Optional[bool] = typer.Option(
False,
"-h",
"--help",
help="Show this message and exit.",
),
) -> None:
"""Add -h and --help options to anaconda org base subcommand."""
# We do this instead of using context_settings for now so we can fallback
# on the existing help for all subcommands. This callback can go away once
# we are okay with typer recursively adding -h and --help to every subcommand.
if show_help:
console.print(ctx.get_help())
raise typer.Exit()


def _get_help_text(parser: ArgumentParser, name: str) -> str:
"""Extract the help text from the anaconda-client CLI Argument Parser."""
if parser._subparsers is None: # pylint: disable=protected-access
Expand Down

0 comments on commit 47e0f5f

Please sign in to comment.