Replies: 1 comment
-
I understand that I can follow the "complex application like Git" example and have But for one, there is no logical way to handle exceptions. Every subcommand has to handle exceptions from within I could handle them in the class, but then I'd have to infect the class with Also, right now, to pass data from parent to child, I have to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am developing a CLI that uses
asyncio
and spawns a web server and … well, it does a bunch of things. I am usingclick.Group
extensively, and I am loving the fact that a Group's callback's function body (let's call itmain
) is executed for a subcommand, too. This allows me to set up the API wrapper and/or Web server, and hand it over to the subcommand usingctx.obj
.So far so good. Now I've found myself in a situation where I really want control flow to return to
main
once the subcommand is done. I am aware that I can useresult_callback
, or that I could pass another type of callback inctx.obj
for the child to call, but I find neither of these very satisfying. Usingresult_callback
foregos the possibility to use closure/context ofmain
, and the second approach imposes responsibility to call the callback to the subcommand.Let me illustrate:
When run with
subcmd1
, this yields:if I run with
subcmd2
, thecallback()…
line is omitted due toa programming error. I'd like to avoid that.
All this made me think that it could be such an improvement to borrow from
context managers and treat each parent as a context manager of its child.
Imagine:
and this will print:
I thought about implementing this as a custom decorator, or subclass of
Group
, but it turns out that this would require quite a bit of work on theintestines of click.
Hence my question: is this a path worth travelling? An idea worth
entertaining?
Beta Was this translation helpful? Give feedback.
All reactions