Documentation ¶
Overview ¶
Package clicontext contains utilities for using context.Context in command-line-interfaces.
Example ¶
package main import ( "context" "fmt" "os" "htdvisser.dev/exp/clicontext" ) func main() { var app interface { Run(ctx context.Context, args ...string) error } // func main() { ctx, exit := clicontext.WithInterruptAndExit(context.Background()) defer exit() if err := app.Run(ctx, os.Args[1:]...); err != nil { fmt.Fprintln(os.Stderr, err) return } } }
Output:
Index ¶
- func GetExitCode(ctx context.Context) (code int, ok bool)
- func SetExitCode(ctx context.Context, code int) bool
- func WithExitCode(parent context.Context, dst *int) context.Context
- func WithInterrupt(parent context.Context, extraInterruptSignals ...os.Signal) context.Context
- func WithInterruptAndExit(parent context.Context, extraInterruptSignals ...os.Signal) (ctx context.Context, exit func())
- func WithTemporaryInterrupt(parent context.Context, extraInterruptSignals ...os.Signal) (context.Context, context.CancelFunc)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetExitCode ¶
GetExitCode returns the exit code from the context.
func SetExitCode ¶
SetExitCode sets the exit code into the context and returns true if it did so successfully.
func WithExitCode ¶
WithExitCode returns a context that can be used to set exit code
func WithInterrupt ¶
WithInterrupt returns a copy of parent with a new Done channel, which is closed when the process receives an interrupt signal or when the parent context's Done channel is closed. The optional extra interrupt signals are added to the default interrupt signals.
func WithInterruptAndExit ¶
func WithInterruptAndExit(parent context.Context, extraInterruptSignals ...os.Signal) (ctx context.Context, exit func())
WithInterruptAndExit is similar to WithInterrupt, but also returns a func that exits the program with an appropriate status code for the interrupt signal.
func WithTemporaryInterrupt ¶
func WithTemporaryInterrupt(parent context.Context, extraInterruptSignals ...os.Signal) (context.Context, context.CancelFunc)
WithTemporaryInterrupt is similar to WithInterrupt, but also releases resources when the returned context.CancelFunc is called.
Types ¶
This section is empty.