Skip to content
Discussion options

You must be logged in to vote

You can use ExitErrHandler for this. It replaces the default error handler, so you can add your own formatting. Here's a small v3 (github.com/urfave/cli/v3) example:

package main

import (
    "context"
    "errors"
    "fmt"
    "os"

    "github.com/urfave/cli/v3"
)

func main() {
    cmd := &cli.Command{
        Name: "demo",
        ExitErrHandler: func(_ context.Context, cmd *cli.Command, err error) {
            if err == nil {
                return
            }
            code := 1
            var ec cli.ExitCoder
            if errors.As(err, &ec) {
                code = ec.ExitCode()
            }
            fmt.Fprintf(cmd.ErrWriter, "\x1b[31merror:\x1b[0m %v\n", err)
     …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by stevewi-niteflyte
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants