verless / verless

A Static Site Generator designed for Markdown-based content with a focus on simplicity and performance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't print Cobra error and Usage when an Error occurs

dominikbraun opened this issue · comments

This is a good first issue for anyone who wants to get started with spf13/cobra.

The verless CLI is built using Cobra. In case an errors occurs, Cobra prints the error and the usage of the particular command, and verless prints the same error again:

$ verless create project --abc
Error: unknown flag: --abc
Usage:
  verless create project NAME [flags]

Flags:
  -h, --help        help for project
      --overwrite   overwrite the directory if it already exists

❌ unknown flag: --abc
  • Error: unknown flag: --abc is the error message printed by Cobra.
  • Usage: ... is the command usage printed by Cobra.
  • ❌ unknown flag: --abc is the error message printed by verless.

We only want to print the error message from verless.

For this purpose, Cobra offers two fields in the Command type:

  • SilenceErrors to prevent that errors are printed
  • SilenceUsage to prevent that the usage is printed

This fields must be set to true for the root command, which is this one:

verless/cli/root.go

Lines 10 to 17 in 4bec8a4

rootCmd := cobra.Command{
Use: "verless",
Short: `A simple and lightweight Static Site Generator.`,
Version: config.GitTag,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

That's it! The output should look like so:

$ verless create project --abc
❌ unknown flag: --abc

I would like to work on this