Merge pull request #82 from bep/mousetrap
Add mousetrap for Windows users
This commit is contained in:
		
							
								
								
									
										8
									
								
								cobra.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								cobra.go
									
									
									
									
									
								
							@ -31,6 +31,14 @@ var initializers []func()
 | 
				
			|||||||
// Set this to true to enable it
 | 
					// Set this to true to enable it
 | 
				
			||||||
var EnablePrefixMatching bool = false
 | 
					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().
 | 
					//OnInitialize takes a series of func() arguments and appends them to a slice of func().
 | 
				
			||||||
func OnInitialize(y ...func()) {
 | 
					func OnInitialize(y ...func()) {
 | 
				
			||||||
	for _, x := range y {
 | 
						for _, x := range y {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										14
									
								
								command.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								command.go
									
									
									
									
									
								
							@ -18,11 +18,13 @@ package cobra
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/inconshreveable/mousetrap"
 | 
				
			||||||
 | 
						flag "github.com/spf13/pflag"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"runtime"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
	flag "github.com/spf13/pflag"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Command is just that, a command for your application.
 | 
					// Command is just that, a command for your application.
 | 
				
			||||||
@ -465,6 +467,14 @@ func (c *Command) Execute() (err error) {
 | 
				
			|||||||
		return c.Root().Execute()
 | 
							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
 | 
						// initialize help as the last point possible to allow for user
 | 
				
			||||||
	// overriding
 | 
						// overriding
 | 
				
			||||||
	c.initHelp()
 | 
						c.initHelp()
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user