Skip to content

fix: set SilenceUsage/SilenceErrors and print errors to stderr #57

Description

@pageton

Summary

The root command is missing two critical Cobra settings, and errors are printed to stdout instead of stderr.

1. Missing SilenceUsage and SilenceErrors

main.go:13-16:

rootCmd := &cobra.Command{
    Use:   "gorm",
    Short: "GORM CLI Tool",
}

Without SilenceUsage: true, Cobra prints the full usage/help text on every RunE error. Without SilenceErrors: true, Cobra also prints the error itself, so the manual fmt.Println(err) in main causes the error to appear twice.

2. Error printed to stdout

main.go:22:

fmt.Println(err)

This writes to stdout. Errors must go to stderr so they don't corrupt piped output when the CLI is used in pipelines.

Fix

rootCmd := &cobra.Command{
    Use:           "gorm",
    Short:         "GORM CLI Tool",
    SilenceUsage:  true,
    SilenceErrors: true,
}

// In main():
if err := rootCmd.Execute(); err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
}

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions