Coder Social home page Coder Social logo

Add support for Typer about rich-click HOT 8 CLOSED

Benjamin-Lee avatar Benjamin-Lee commented on May 19, 2024
Add support for Typer

from rich-click.

Comments (8)

wasi-master avatar wasi-master commented on May 19, 2024 6

So I tested a bit and found a workaround (The try/except is "unnecessary")

try:
    import rich_click as click
    from rich_click import RichCommand as Command, RichGroup as Group
except ImportError:
    import click
    from click import Command, Group

class MyTyper(typer.Typer):
    """A custom subclassed version of typer.Typer to allow rich help"""

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

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

Now this may not be the best solution but it works I guess.

And I've also published it with pypi-command-line version 1.6.12

from rich-click.

browniebroke avatar browniebroke commented on May 19, 2024 2

I was just wondering what API you were envisioning, but what that suggestion, I think it's now all clear. I can definitely do that, yes.

from rich-click.

wasi-master avatar wasi-master commented on May 19, 2024

So I used click-help-colors in my pypi-command-line project before and although click-help-colors officially wasn't supported for Typer. I found a workaround by searching for open issues in the Typer repository. tiangolo/typer#47

I am not sure if the same solution could be used here, but if it works, then great.

from rich-click.

ewels avatar ewels commented on May 19, 2024

Super nice!

I guess then you need to use the custom class name MyTyper in your code.. I wonder if we can do the same trick for Typer that we do for Click and have a drop-in import rich_click.typer as typer 🤔

I'm on holiday this week but will have a play when I'm back. Need to learn Typer first and get a minimal example to play with 😅

from rich-click.

Benjamin-Lee avatar Benjamin-Lee commented on May 19, 2024

This worked for me! Thanks @wasi-master for the workaround.

from rich-click.

browniebroke avatar browniebroke commented on May 19, 2024

Thanks for the snippet @wasi-master I tweaked it a bit to integrate it in my project: https://github.com/browniebroke/deezer-oauth-cli/pull/30/files

from rich-click.

wasi-master avatar wasi-master commented on May 19, 2024

@browniebroke Well done!

from rich-click.

ewels avatar ewels commented on May 19, 2024

@browniebroke - this looks great! Do you fancy prepping a PR to this repo with that, or are you happy if I pinch your code? I'm thinking that we can then have something like:

import rich_click.typer as typer

from rich-click.

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.