Code Monkey home page Code Monkey logo

Comments (5)

tiangolo avatar tiangolo commented on May 18, 2024 1

You have to also provide the Command class, like:

import typer
from click_help_colors import HelpColorsGroup, HelpColorsCommand


class CustomHelpColorsGroup(HelpColorsGroup):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.help_headers_color = "blue"
        self.help_options_color = "yellow"


class CustomHelpColorsCommand(HelpColorsCommand):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.help_headers_color = "blue"
        self.help_options_color = "yellow"


app = typer.Typer(cls=CustomHelpColorsGroup)


@app.command(cls=CustomHelpColorsCommand)
def test_function(
    name: str = typer.Option("World", "--name", "-n", help="name to say hello")
) -> None:
    typer.echo(f"Hello {name}!")


@app.command(cls=CustomHelpColorsCommand)
def other() -> None:
    typer.echo("other")


if __name__ == "__main__":
    app()

This is because HelpColorsGroup defines its own method command that overrides the default command method from Click, to provide its own HelpColorsCommand class instead of the default Command class.

But Typer doesn't modify nor use Click classes directly, when you use @app.command() that is not the same as with a Click @cli.command().

Typer doesn't modify the function to replace it with an instance of a class, instead, it registers the function to use and the class to use, and then it creates the specific objects when you call it. But doing it as described above should work fine (I just tested it 😄 ).

from typer.

daddycocoaman avatar daddycocoaman commented on May 18, 2024 1

Just wanted to add, if you want to avoid having to add the cls to every command and/or Typer, you can just subclass them. So adding onto @tiangolo, it would be something like this:

class LootMarshalTyper(typer.Typer):

    def __init__(self, *args, cls=CustomHelpColorsGroup, **kwargs) -> None:
        super().__init__(*args, cls=cls, **kwargs)

    def command(self, *args, cls=CustomHelpColorsCommand, **kwargs) -> typer.Typer.command:
        return super().command(*args, cls=cls, **kwargs)

Hope this helps.

from typer.

github-actions avatar github-actions commented on May 18, 2024

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

from typer.

ssbarnea avatar ssbarnea commented on May 18, 2024

Here is a full example I found of using click-help-color with typer https://github.com/daddycocoaman/AzDummy/blob/78438183c95102c5aed5af3578af31601bd7f423/azdummy/core/typer.py#L2

from typer.

ssbarnea avatar ssbarnea commented on May 18, 2024

Apparently typer is no longer compatible with click-help-colors since 0.7

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.