You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have the following use case: I want to take another Python library's CLI, make some slight changes, and add it to my app's CLI with click.Group.add_command().
In order to get that library using Rich-Click, I need to patch manually. This is my __main__.py:
# __main__.pyfromrich_clickimportcommandasrich_commandfromrich_clickimportgroupasrich_groupfromrich_clickimportRichCommandfromrich_clickimportRichGroupimportclickclick.group=rich_groupclick.command=rich_commandclick.Command=RichCommandclick.Group=RichGroup# this is where my CLI is.fromapp.cliimportclicli()
Instead, what we could do is have a function called patch() that does this. Here is what I am thinking:
# __main__.pyfromrich_click.cliimportpatchpatch()
# this is where my CLI is.fromapp.cliimportclicli()
This doesn't need to be a majorly advertised feature-- using it the way I want to is a bit niche-- and it can and should take a back seat to the rich-click CLI. The main use of this would be internally inside rich_click.cli.main, where the following lines of code...
iflen(args) >1:
ifargs[1] =="--":
delargs[1]
sys.argv= [prog, *args[1:]]
# patch click before importing the program functionclick.group=rich_groupclick.command=rich_commandclick.Group=RichGroupclick.Command=RichCommand# import the program functionmodule=import_module(module_path)
function=getattr(module, function_name)
# simply run it: it should be patched as wellreturnfunction()
... could instead look like this ...
iflen(args) >1:
ifargs[1] =="--":
delargs[1]
sys.argv= [prog, *args[1:]]
# patch click before importing the program functionpatch()
# import the program functionmodule=import_module(module_path)
function=getattr(module, function_name)
# simply run it: it should be patched as wellreturnfunction()
where patch() is defined as (you guessed it):
defpatch() ->None:
"""Patch Click internals to use Rich-Click types."""click.group=rich_groupclick.command=rich_commandclick.Group=RichGroupclick.Command=RichCommand
But I suggest separating out the patching function so that it can be exposed for niche situations like the case I'm working on.
WDYT?
The text was updated successfully, but these errors were encountered:
Sure - so this is essentially refactoring the code with no change to current behaviour so that it can be better accessed by other tools..? I don't see any downsides.. 🤔
I have the following use case: I want to take another Python library's CLI, make some slight changes, and add it to my app's CLI with
click.Group.add_command()
.In order to get that library using Rich-Click, I need to patch manually. This is my
__main__.py
:Instead, what we could do is have a function called
patch()
that does this. Here is what I am thinking:This doesn't need to be a majorly advertised feature-- using it the way I want to is a bit niche-- and it can and should take a back seat to the rich-click CLI. The main use of this would be internally inside
rich_click.cli.main
, where the following lines of code...... could instead look like this ...
where
patch()
is defined as (you guessed it):But I suggest separating out the patching function so that it can be exposed for niche situations like the case I'm working on.
WDYT?
The text was updated successfully, but these errors were encountered: