-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide construct for command close/tear-down #395
Comments
I imagine it would be helpful to be able to register closable object on the context so that subcommands can use them. Then all a command's registered objects could be closed once all subcommands have completed. something like fun <T: AutoClosable> Context.use(t: T) : T
fun run() {
currentContext.obj = currentContext.use(file.open()) // close() called automatically at end of execution
} we'd have to use the experimental |
Great, that'd support
fun <T: AutoClosable> Context.registerCloseable(t: T) : T
fun run() {
currentContext.obj = currentContext.registerCloseable(file.open())
}
// or when we don't need to store anything on Context.obj (my case, actually)
fun run() {
currentContext.registerCloseable { shutdown() }
}
// And also leaves obj free to store something else
fun run() {
currentContext.obj = aSharedObjectThatDoesntNeedClosing
currentContext.registerCloseable { shutdown() }
}
fun Context.afterAllCommands(block: () -> Unit)
fun run() {
context.afterAllCommands {
shutdown()
myFile.close()
}
} |
I don't feel strongly about the name.
|
Thanks for the link, I wasn't aware of that. Do you think we should wait until
|
I would wait until 1.8.20 is released, but we don't have to wait until AutoClosable is stable; we can just make |
I would greatly benefit from this feature. Is it ready to implement? |
I have a group of commands which will each one use a certain resource and must close it in the end. The current way of doing this is simply rely on each one closing in its own
run
method, but I feel it'd be useful to implement this once in the parent command, instead of each subcommand.If one of them sound good, I'd be happy to contribute with a PR.
Current solution
Proposal
Clikt could check if a command is
Closeable
and callclose()
afterrun()
ends.Alternatively,
close
could be a new open method inCliktCommand
.The text was updated successfully, but these errors were encountered: