Giter Club home page Giter Club logo

Comments (2)

lazToum avatar lazToum commented on May 24, 2024 2

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.                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯


from typer.

reubenfirmin avatar reubenfirmin commented on May 24, 2024

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.

from typer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.