Beispiel #1
0
func preExecHook(c *Command) {
	if mousetrap.StartedByExplorer() {
		c.Print(MousetrapHelpText)
		time.Sleep(5 * time.Second)
		os.Exit(1)
	}
}
Beispiel #2
0
// Call execute to use the args (os.Args[1:] by default)
// and run through the command tree finding appropriate matches
// for commands and then corresponding flags.
func (c *Command) Execute() (err error) {

	// Regardless of what command execute is called on, run on Root only
	if c.HasParent() {
		return c.Root().Execute()
	}

	if EnableWindowsMouseTrap && runtime.GOOS == "windows" {
		if mousetrap.StartedByExplorer() {
			c.Print(MousetrapHelpText)
			time.Sleep(5 * time.Second)
			os.Exit(1)
		}
	}

	// initialize help as the last point possible to allow for user
	// overriding
	c.initHelpCmd()

	var args []string

	if len(c.args) == 0 {
		args = os.Args[1:]
	} else {
		args = c.args
	}

	cmd, flags, err := c.Find(args)
	if err != nil {
		// If found parse to a subcommand and then failed, talk about the subcommand
		if cmd != nil {
			c = cmd
		}
		c.Println("Error:", err.Error())
		c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
		return err
	}

	err = cmd.execute(flags)
	if err != nil {
		if err == flag.ErrHelp {
			cmd.HelpFunc()(cmd, args)
			return nil
		}
		c.Println(cmd.UsageString())
		c.Println("Error:", err.Error())
	}

	return
}