Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to include a Prolog in --help #537

Open
7 tasks done
AngryMaciek opened this issue Jan 19, 2023 · 2 comments
Open
7 tasks done

Is it possible to include a Prolog in --help #537

AngryMaciek opened this issue Jan 19, 2023 · 2 comments
Labels
question Question or problem

Comments

@AngryMaciek
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

`--help`

Description

I have seen it is possible to add an epilog to the --help command, as here.

Would it be possible to add a custom Prolog above the "Usage..." line too?

Operating System

Linux

Operating System Details

No response

Typer Version

0.7.0

Python Version

Python 3.11.0

Additional Context

No response

@AngryMaciek AngryMaciek added the question Question or problem label Jan 19, 2023
@lazToum
Copy link

lazToum commented Jan 24, 2023

Here is one way (overriding get_usage from the default TyperGroup cls):
You could also use the format_usage.

import click
import typer
from typer.core import TyperGroup as TyperGroupBase


class TyperGroup(TyperGroupBase):
    """Custom TyperGroup class."""

    def get_usage(self, ctx: click.Context) -> str:
        """Override get_usage."""
        usage = super().get_usage(ctx)
        message = (
            'Message Above.\n'
            + usage
            + '\nMessage Below.'
        )
        return message


app = typer.Typer(
    name='myapp',
    help='MyApp cli actions.',
    no_args_is_help=True,
    context_settings={"help_option_names": ["-h", "--help"]},
    invoke_without_command=True,
    options_metavar="[OPTIONS]",
    cls=TyperGroup,
)


@app.callback(invoke_without_command=True)
def get_version(
    version: bool = typer.Option(
        False,
        "--version",
        "-v",
        help="Print version and exit.",
    ),
) -> None:
    """Display the version."""
    if version:
        typer.echo(f"myapp 0.0.1")
        raise typer.Exit()


if __name__ == '__main__':
    app()
                                                                                                                        
 Message Above.                                                                                                         
 Usage: app.py [OPTIONS] COMMAND [ARGS]...                                                                               
 Message Below.                                                                                                         
                                                                                                                        
 MyApp cli actions.                                                                                                     
                                                                                                                        
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --version             -v        Print version and exit.                                                              │
│ --install-completion            Install completion for the current shell.                                            │
│ --show-completion               Show completion for the current shell, to copy it or customize the installation.     │
│ --help                -h        Show this message and exit.                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯


@reubenfirmin
Copy link

Maybe this needs to be a separate ticket, but I would like to be able to call a custom function in either prolog or epilog. The reason is that I use rich to create a table of data sources that can be accessed by various commands; I can't just dump that table into a decorator (as far as I know). Ideally I could just add a @prolog or @epilog decorator to a function, and let it do whatever it wants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
Projects
None yet
Development

No branches or pull requests

3 participants