From beda1945ad1e95784d78fdad8d4252a9da723521 Mon Sep 17 00:00:00 2001 From: bep Date: Wed, 1 Apr 2015 21:14:40 +0200 Subject: [PATCH] Add mousetrap for Windows users Fixes #80 --- cobra.go | 8 ++++++++ command.go | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cobra.go b/cobra.go index 1b5ca360..78b92b0a 100644 --- a/cobra.go +++ b/cobra.go @@ -31,6 +31,14 @@ var initializers []func() // Set this to true to enable it var EnablePrefixMatching bool = false +// enables an information splash screen on Windows if the CLI is started from explorer.exe. +var EnableWindowsMouseTrap bool = true + +var MousetrapHelpText string = `This is a command line tool + +You need to open cmd.exe and run it from there. +` + //OnInitialize takes a series of func() arguments and appends them to a slice of func(). func OnInitialize(y ...func()) { for _, x := range y { diff --git a/command.go b/command.go index a7d90886..0ca13384 100644 --- a/command.go +++ b/command.go @@ -18,11 +18,13 @@ package cobra import ( "bytes" "fmt" + "github.com/inconshreveable/mousetrap" + flag "github.com/spf13/pflag" "io" "os" + "runtime" "strings" - - flag "github.com/spf13/pflag" + "time" ) // Command is just that, a command for your application. @@ -474,6 +476,14 @@ func (c *Command) Execute() (err error) { 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.initHelp()