github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/everdrone/grab/internal/utils"
     7  
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var RootCmd = &cobra.Command{
    12  	Use: "grab",
    13  	// TODO: describe both short and long
    14  	Short: "Download and scrape web pages based on a set of regex patterns",
    15  	Example: `  Generate the starter config file:
    16      grab config generate
    17  
    18    After customizing it, check for errors:
    19      grab config check
    20  
    21    Scrape and download assets from a url:
    22      grab get http://example.com
    23  
    24    Scrape and download assets from a list of urls:
    25      grab get list.ini`,
    26  	SilenceErrors: true,
    27  	SilenceUsage:  true,
    28  	Args:          cobra.NoArgs,
    29  }
    30  
    31  func Execute() {
    32  	err := RootCmd.Execute()
    33  	if err != nil {
    34  		os.Exit(1)
    35  	}
    36  }
    37  
    38  func init() {
    39  	// from: https://github.com/spf13/cobra/issues/914#issuecomment-548411337
    40  	RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
    41  		cmd.PrintErrf("Error: %s\n", err)
    42  		cmd.Println(cmd.UsageString())
    43  		return utils.ErrSilent
    44  	})
    45  }