Repository: spf13/cobra (go)

Issue:
Expand upon config file usage in README
I didn't see this in the README about the usage of the config file aside from the flag option of it being there. Does Cobra automatically read the config file from the location specified in the root.go, or is that for us to read and parse on our own?  

Pull request:
Add explanation for viper.BindPFlag in README.md
Fix #429

/cc @deltaskelta

Patch:
diff --git a/README.md b/README.md
index b45f401..f26db62 100644
--- a/README.md
+++ b/README.md
@@ -114,7 +114,7 @@ and flags that are only available to that command.
 In the example above, 'port' is the flag.
 
 Flag functionality is provided by the [pflag
-library](https://github.com/ogier/pflag), a fork of the flag standard library
+library](https://github.com/spf13/pflag), a fork of the flag standard library
 which maintains the same interface while adding POSIX compliance.
 
 ## Usage
@@ -127,10 +127,10 @@ tree is assigned to the commander which is finally executed.
 
 # Installing
 Using Cobra is easy. First, use `go get` to install the latest version
-of the library. This command will install the `cobra` generator executible
-along with the library:
+of the library. This command will install the `cobra` generator executable
+along with the library and its dependencies:
 
-    go get -v github.com/spf13/cobra/cobra
+    go get -u github.com/spf13/cobra
 
 Next, include Cobra in your application:
 
@@ -276,7 +276,6 @@ You will optionally provide additional commands as you see fit.
 
 The root command represents your binary itself.
 
-
 #### Manually create rootCmd
 
 Cobra doesn't require any special constructors. Simply create your commands.
@@ -298,9 +297,18 @@ var RootCmd = &cobra.Command{
 
 You will additionally define flags and handle configuration in your init() function.
 
-for example cmd/root.go:
+For example cmd/root.go:
 
 ```go
+import (
+	"fmt"
+	"os"
+
+	homedir "github.com/mitchellh/go-homedir"
+	"github.com/spf13/cobra"
+	"github.com/spf13/viper"
+)
+
 func init() {
 	cobra.OnInitialize(initConfig)
 	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
@@ -314,6 +322,30 @@ func init() {
 	viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
 	viper.SetDefault("license", "apache")
 }
+
+func main() {
+  // Don't forget to read config either from cfgFile or from home directory!
+	if cfgFile != "" {
+		// Use config file from the flag.
+		viper.SetConfigFile(cfgFile)
+	} else {
+		// Find home directory.
+		home, err := homedir.Dir()
+		if err != nil {
+			fmt.Println(home)
+			os.Exit(1)
+		}
+
+		// Search config in home directory with name ".cobra" (without extension).
+		viper.AddConfigPath(home)
+		viper.SetConfigName(".cobra")
+	}
+
+	if err := viper.ReadInConfig(); err != nil {
+		fmt.Println("Can't read config:", err)
+    os.Exit(1)
+	}
+}
 ```
 
 ### Create your main.go
@@ -341,7 +373,6 @@ func main() {
 }
 ```
 
-
 ### Create additional commands
 
 Additional commands can be defined and typically are each given their own file
@@ -431,6 +462,23 @@ A flag can also be assigned locally which will only apply to that specific comma
 RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
 ```
 
+### Bind Flags with Config
+
+You can also bind your flags with [viper](https://github.com/spf13/viper):
+```go
+var author string
+
+func init() {
+	RootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")
+	viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))
+}
+```
+
+In this example the persistent flag `author` is bound with `viper`.
+**Note**, that the variable `author` will not be set to the value from config,
+when the `--author` flag is not provided by user.
+
+More in [viper documentation](https://github.com/spf13/viper#working-with-flags).
 
 ## Example
 
diff --git a/bash_completions.go b/bash_completions.go
index a0d0402..5d6238b 100644
--- a/bash_completions.go
+++ b/bash_completions.go
@@ -1,6 +1,7 @@
 package cobra
 
 import (
+	"bytes"
 	"fmt"
 	"io"
 	"os"
@@ -18,12 +19,9 @@ const (
 	BashCompSubdirsInDir    = "cobra_annotation_bash_completion_subdirs_in_dir"
 )
 
-func preamble(out io.Writer, name string) error {
-	_, err := fmt.Fprintf(out, "# bash completion for %-36s -*- shell-script -*-\n", name)
-	if err != nil {
-		return err
-	}
-	preamStr := `
+func writePreamble(buf *bytes.Buffer, name string) {
+	buf.WriteString(fmt.Sprintf("# bash completion for %-36s -*- shell-script -*-\n", name))
+	buf.WriteString(`
 __debug()
 {
     if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
@@ -247,18 +245,13 @@ __handle_word()
     __handle_word
 }
 
-`
-	_, err = fmt.Fprint(out, preamStr)
-	return err
+`)
 }
 
-func postscript(w io.Writer, name string) error {
+func writePostscript(buf *bytes.Buffer, name string) {
 	name = strings.Replace(name, ":", "__", -1)
-	_, err := fmt.Fprintf(w, "__start_%s()\n", name)
-	if err != nil {
-		return err
-	}
-	_, err = fmt.Fprintf(w, `{
+	buf.WriteString(fmt.Sprintf("__start_%s()\n", name))
+	buf.WriteString(fmt.Sprintf(`{
     local cur prev words cword
     declare -A flaghash 2>/dev/null || :
     if declare -F _init_completion >/dev/null 2>&1; then
@@ -282,197 +275,132 @@ func postscript(w io.Writer, name string) error {
     __handle_word
 }
 
-`, name)
-	if err != nil {
-		return err
-	}
-	_, err = fmt.Fprintf(w, `if [[ $(type -t compopt) = "builtin" ]]; then
+`, name))
+	buf.WriteString(fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then
     complete -o default -F __start_%s %s
 else
     complete -o default -o nospace -F __start_%s %s
 fi
 
-`, name, name, name, name)
-	if err != nil {
-		return err
-	}
-	_, err = fmt.Fprintf(w, "# ex: ts=4 sw=4 et filetype=sh\n")
-	return err
+`, name, name, name, name))
+	buf.WriteString("# ex: ts=4 sw=4 et filetype=sh\n")
 }
 
-func writeCommands(cmd *Command, w io.Writer) error {
-	if _, err := fmt.Fprintf(w, "    commands=()\n"); err != nil {
-		return err
-	}
+func writeCommands(buf *bytes.Buffer, cmd *Command) {
+	buf.WriteString("    commands=()\n")
 	for _, c := range cmd.Commands() {
 		if !c.IsAvailableCommand() || c == cmd.helpCommand {
 			continue
 		}
-		if _, err := fmt.Fprintf(w, "    commands+=(%q)\n", c.Name()); err != nil {
-			return err
-		}
+		buf.WriteString(fmt.Sprintf("    commands+=(%q)\n", c.Name()))
 	}
-	_, err := fmt.Fprintf(w, "\n")
-	return err
+	buf.WriteString("\n")
 }
 
-func writeFlagHandler(name string, annotations map[string][]string, w io.Writer) error {
+func writeFlagHandler(buf *bytes.Buffer, name string, annotations map[string][]string) {
 	for key, value := range annotations {
 		switch key {
 		case BashCompFilenameExt:
-			_, err := fmt.Fprintf(w, "    flags_with_completion+=(%q)\n", name)
-			if err != nil {
-				return err
-			}
+			buf.WriteString(fmt.Sprintf("    flags_with_completion+=(%q)\n", name))
 
+			var ext string
 			if len(value) > 0 {
-				ext := "__handle_filename_extension_flag " + strings.Join(value, "|")
-				_, err = fmt.Fprintf(w, "    flags_completion+=(%q)\n", ext)
+				ext = "__handle_filename_extension_flag " + strings.Join(value, "|")
 			} else {
-				ext := "_filedir"
-				_, err = fmt.Fprintf(w, "    flags_completion+=(%q)\n", ext)
-			}
-			if err != nil {
-				return err
+				ext = "_filedir"
 			}
+			buf.WriteString(fmt.Sprintf("    flags_completion+=(%q)\n", ext))
 		case BashCompCustom:
-			_, err := fmt.Fprintf(w, "    flags_with_completion+=(%q)\n", name)
-			if err != nil {
-				return err
-			}
+			buf.WriteString(fmt.Sprintf("    flags_with_completion+=(%q)\n", name))
 			if len(value) > 0 {
 				handlers := strings.Join(value, "; ")
-				_, err = fmt.Fprintf(w, "    flags_completion+=(%q)\n", handlers)
+				buf.WriteString(fmt.Sprintf("    flags_completion+=(%q)\n", handlers))
 			} else {
-				_, err = fmt.Fprintf(w, "    flags_completion+=(:)\n")
-			}
-			if err != nil {
-				return err
+				buf.WriteString("    flags_completion+=(:)\n")
 			}
 		case BashCompSubdirsInDir:
-			_, err := fmt.Fprintf(w, "    flags_with_completion+=(%q)\n", name)
+			buf.WriteString(fmt.Sprintf("    flags_with_completion+=(%q)\n", name))
 
+			var ext string
 			if len(value) == 1 {
-				ext := "__handle_subdirs_in_dir_flag " + value[0]
-				_, err = fmt.Fprintf(w, "    flags_completion+=(%q)\n", ext)
+				ext = "__handle_subdirs_in_dir_flag " + value[0]
 			} else {
-				ext := "_filedir -d"
-				_, err = fmt.Fprintf(w, "    flags_completion+=(%q)\n", ext)
-			}
-			if err != nil {
-				return err
+				ext = "_filedir -d"
 			}
+			buf.WriteString(fmt.Sprintf("    flags_completion+=(%q)\n", ext))
 		}
 	}
-	return nil
 }
 
-func writeShortFlag(flag *pflag.Flag, w io.Writer) error {
-	b := (len(flag.NoOptDefVal) > 0)
+func writeShortFlag(buf *bytes.Buffer, flag *pflag.Flag) {
 	name := flag.Shorthand
 	format := "    "
-	if !b {
+	if len(flag.NoOptDefVal) == 0 {
 		format += "two_word_"
 	}
 	format += "flags+=(\"-%s\")\n"
-	if _, err := fmt.Fprintf(w, format, name); err != nil {
-		return err
-	}
-	return writeFlagHandler("-"+name, flag.Annotations, w)
+	buf.WriteString(fmt.Sprintf(format, name))
+	writeFlagHandler(buf, "-"+name, flag.Annotations)
 }
 
-func writeFlag(flag *pflag.Flag, w io.Writer) error {
-	b := (len(flag.NoOptDefVal) > 0)
+func writeFlag(buf *bytes.Buffer, flag *pflag.Flag) {
 	name := flag.Name
 	format := "    flags+=(\"--%s"
-	if !b {
+	if len(flag.NoOptDefVal) == 0 {
 		format += "="
 	}
 	format += "\")\n"
-	if _, err := fmt.Fprintf(w, format, name); err != nil {
-		return err
-	}
-	return writeFlagHandler("--"+name, flag.Annotations, w)
+	buf.WriteString(fmt.Sprintf(format, name))
+	writeFlagHandler(buf, "--"+name, flag.Annotations)
 }
 
-func writeLocalNonPersistentFlag(flag *pflag.Flag, w io.Writer) error {
-	b := (len(flag.NoOptDefVal) > 0)
+func writeLocalNonPersistentFlag(buf *bytes.Buffer, flag *pflag.Flag) {
 	name := flag.Name
 	format := "    local_nonpersistent_flags+=(\"--%s"
-	if !b {
+	if len(flag.NoOptDefVal) == 0 {
 		format += "="
 	}
 	format += "\")\n"
-	_, err := fmt.Fprintf(w, format, name)
-	return err
+	buf.WriteString(fmt.Sprintf(format, name))
 }
 
-func writeFlags(cmd *Command, w io.Writer) error {
-	_, err := fmt.Fprintf(w, `    flags=()
+func writeFlags(buf *bytes.Buffer, cmd *Command) {
+	buf.WriteString(`    flags=()
     two_word_flags=()
     local_nonpersistent_flags=()
     flags_with_completion=()
     flags_completion=()
 
 `)
-	if err != nil {
-		return err
-	}
 	localNonPersistentFlags := cmd.LocalNonPersistentFlags()
-	var visitErr error
 	cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {
 		if nonCompletableFlag(flag) {
 			return
 		}
-		if err := writeFlag(flag, w); err != nil {
-			visitErr = err
-			return
-		}
+		writeFlag(buf, flag)
 		if len(flag.Shorthand) > 0 {
-			if err := writeShortFlag(flag, w); err != nil {
-				visitErr = err
-				return
-			}
+			writeShortFlag(buf, flag)
 		}
 		if localNonPersistentFlags.Lookup(flag.Name) != nil {
-			if err := writeLocalNonPersistentFlag(flag, w); err != nil {
-				visitErr = err
-				return
-			}
+			writeLocalNonPersistentFlag(buf, flag)
 		}
 	})
-	if visitErr != nil {
-		return visitErr
-	}
 	cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {
 		if nonCompletableFlag(flag) {
 			return
 		}
-		if err := writeFlag(flag, w); err != nil {
-			visitErr = err
-			return
-		}
+		writeFlag(buf, flag)
 		if len(flag.Shorthand) > 0 {
-			if err := writeShortFlag(flag, w); err != nil {
-				visitErr = err
-				return
-			}
+			writeShortFlag(buf, flag)
 		}
 	})
-	if visitErr != nil {
-		return visitErr
-	}
 
-	_, err = fmt.Fprintf(w, "\n")
-	return err
+	buf.WriteString("\n")
 }
 
-func writeRequiredFlag(cmd *Command, w io.Writer) error {
-	if _, err := fmt.Fprintf(w, "    must_have_one_flag=()\n"); err != nil {
-		return err
-	}
+func writeRequiredFlag(buf *bytes.Buffer, cmd *Command) {
+	buf.WriteString("    must_have_one_flag=()\n")
 	flags := cmd.NonInheritedFlags()
-	var visitErr error
 	flags.VisitAll(func(flag *pflag.Flag) {
 		if nonCompletableFlag(flag) {
 			return
@@ -481,107 +409,68 @@ func writeRequiredFlag(cmd *Command, w io.Writer) error {
 			switch key {
 			case BashCompOneRequiredFlag:
 				format := "    must_have_one_flag+=(\"--%s"
-				b := (flag.Value.Type() == "bool")
-				if !b {
+				if flag.Value.Type() != "bool" {
 					format += "="
 				}
 				format += "\")\n"
-				if _, err := fmt.Fprintf(w, format, flag.Name); err != nil {
-					visitErr = err
-					return
-				}
+				buf.WriteString(fmt.Sprintf(format, flag.Name))
 
 				if len(flag.Shorthand) > 0 {
-					if _, err := fmt.Fprintf(w, "    must_have_one_flag+=(\"-%s\")\n", flag.Shorthand); err != nil {
-						visitErr = err
-						return
-					}
+					buf.WriteString(fmt.Sprintf("    must_have_one_flag+=(\"-%s\")\n", flag.Shorthand))
 				}
 			}
 		}
 	})
-	return visitErr
 }
 
-func writeRequiredNouns(cmd *Command, w io.Writer) error {
-	if _, err := fmt.Fprintf(w, "    must_have_one_noun=()\n"); err != nil {
-		return err
-	}
+func writeRequiredNouns(buf *bytes.Buffer, cmd *Command) {
+	buf.WriteString("    must_have_one_noun=()\n")
 	sort.Sort(sort.StringSlice(cmd.ValidArgs))
 	for _, value := range cmd.ValidArgs {
-		if _, err := fmt.Fprintf(w, "    must_have_one_noun+=(%q)\n", value); err != nil {
-			return err
-		}
+		buf.WriteString(fmt.Sprintf("    must_have_one_noun+=(%q)\n", value))
 	}
-	return nil
 }
 
-func writeArgAliases(cmd *Command, w io.Writer) error {
-	if _, err := fmt.Fprintf(w, "    noun_aliases=()\n"); err != nil {
-		return err
-	}
+func writeArgAliases(buf *bytes.Buffer, cmd *Command) {
+	buf.WriteString("    noun_aliases=()\n")
 	sort.Sort(sort.StringSlice(cmd.ArgAliases))
 	for _, value := range cmd.ArgAliases {
-		if _, err := fmt.Fprintf(w, "    noun_aliases+=(%q)\n", value); err != nil {
-			return err
-		}
+		buf.WriteString(fmt.Sprintf("    noun_aliases+=(%q)\n", value))
 	}
-	return nil
 }
 
-func gen(cmd *Command, w io.Writer) error {
+func gen(buf *bytes.Buffer, cmd *Command) {
 	for _, c := range cmd.Commands() {
 		if !c.IsAvailableCommand() || c == cmd.helpCommand {
 			continue
 		}
-		if err := gen(c, w); err != nil {
-			return err
-		}
+		gen(buf, c)
 	}
 	commandName := cmd.CommandPath()
 	commandName = strings.Replace(commandName, " ", "_", -1)
 	commandName = strings.Replace(commandName, ":", "__", -1)
-	if _, err := fmt.Fprintf(w, "_%s()\n{\n", commandName); err != nil {
-		return err
-	}
-	if _, err := fmt.Fprintf(w, "    last_command=%q\n", commandName); err != nil {
-		return err
-	}
-	if err := writeCommands(cmd, w); err != nil {
-		return err
-	}
-	if err := writeFlags(cmd, w); err != nil {
-		return err
-	}
-	if err := writeRequiredFlag(cmd, w); err != nil {
-		return err
-	}
-	if err := writeRequiredNouns(cmd, w); err != nil {
-		return err
-	}
-	if err := writeArgAliases(cmd, w); err != nil {
-		return err
-	}
-	if _, err := fmt.Fprintf(w, "}\n\n"); err != nil {
-		return err
-	}
-	return nil
+	buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName))
+	buf.WriteString(fmt.Sprintf("    last_command=%q\n", commandName))
+	writeCommands(buf, cmd)
+	writeFlags(buf, cmd)
+	writeRequiredFlag(buf, cmd)
+	writeRequiredNouns(buf, cmd)
+	writeArgAliases(buf, cmd)
+	buf.WriteString("}\n\n")
 }
 
 // GenBashCompletion generates bash completion file and writes to the passed writer.
 func (cmd *Command) GenBashCompletion(w io.Writer) error {
-	if err := preamble(w, cmd.Name()); err != nil {
-		return err
-	}
+	buf := new(bytes.Buffer)
+	writePreamble(buf, cmd.Name())
 	if len(cmd.BashCompletionFunction) > 0 {
-		if _, err := fmt.Fprintf(w, "%s\n", cmd.BashCompletionFunction); err != nil {
-			return err
-		}
+		buf.WriteString(cmd.BashCompletionFunction + "\n")
 	}
-	if err := gen(cmd, w); err != nil {
-		return err
-	}
-	return postscript(w, cmd.Name())
+	gen(buf, cmd)
+	writePostscript(buf, cmd.Name())
+
+	_, err := buf.WriteTo(w)
+	return err
 }
 
 func nonCompletableFlag(flag *pflag.Flag) bool {
diff --git a/bash_completions.md b/bash_completions.md
index 7fa970f..52bd39d 100644
--- a/bash_completions.md
+++ b/bash_completions.md
@@ -106,7 +106,7 @@ node                 pod                    replicationcontroller  service
 
 If your nouns have a number of aliases, you can define them alongside `ValidArgs` using `ArgAliases`:
 
-```go`
+```go
 argAliases []string = { "pods", "nodes", "services", "svc", "replicationcontrollers", "rc" }
 
 cmd := &cobra.Command{
diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go
index 20c6538..8897f8f 100644
--- a/cobra/cmd/add.go
+++ b/cobra/cmd/add.go
@@ -20,7 +20,6 @@ import (
 	"unicode"
 
 	"github.com/spf13/cobra"
-	"github.com/spf13/viper"
 )
 
 func init() {
@@ -117,7 +116,7 @@ func createCmdFile(license License, path, cmdName string) {
 	template := `{{comment .copyright}}
 {{comment .license}}
 
-package cmd
+package {{.cmdPackage}}
 
 import (
 	"fmt"
@@ -158,7 +157,7 @@ func init() {
 	data := make(map[string]interface{})
 	data["copyright"] = copyrightLine()
 	data["license"] = license.Header
-	data["viper"] = viper.GetBool("useViper")
+	data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path
 	data["parentName"] = parentName
 	data["cmdName"] = cmdName
 
diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go
index e5324c5..6114227 100644
--- a/cobra/cmd/helpers.go
+++ b/cobra/cmd/helpers.go
@@ -23,19 +23,16 @@ import (
 	"text/template"
 )
 
-var projectPath string
-
 var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}
 var srcPaths []string
 
 func init() {
-	// Initialize goPaths and srcPaths
+	// Initialize srcPaths.
 	envGoPath := os.Getenv("GOPATH")
-	if envGoPath == "" {
+	goPaths := filepath.SplitList(envGoPath)
+	if len(goPaths) == 0 {
 		er("$GOPATH is not set")
 	}
-
-	goPaths := filepath.SplitList(envGoPath)
 	srcPaths = make([]string, 0, len(goPaths))
 	for _, goPath := range goPaths {
 		srcPaths = append(srcPaths, filepath.Join(goPath, "src"))
diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go
index 788bdb5..0e2b22a 100644
--- a/cobra/cmd/init.go
+++ b/cobra/cmd/init.go
@@ -197,13 +197,13 @@ func initConfig() {
 		// Find home directory.
 		home, err := homedir.Dir()
 		if err != nil {
-			fmt.Println(home)
+			fmt.Println(err)
 			os.Exit(1)
 		}
 
-		// Search config in home directory with name ".cobra" (without extension).
+		// Search config in home directory with name ".{{ .appName }}" (without extension).
 		viper.AddConfigPath(home)
-		viper.SetConfigName(".cobra")
+		viper.SetConfigName(".{{ .appName }}")
 	}
 
 	viper.AutomaticEnv() // read in environment variables that match
diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go
index a3e6d91..de1168a 100644
--- a/cobra/cmd/project.go
+++ b/cobra/cmd/project.go
@@ -3,6 +3,7 @@ package cmd
 import (
 	"os"
 	"path/filepath"
+	"runtime"
 	"strings"
 )
 
@@ -37,7 +38,7 @@ func NewProject(projectName string) *Project {
 		}
 		for _, srcPath := range srcPaths {
 			goPath := filepath.Dir(srcPath)
-			if filepath.HasPrefix(wd, goPath) {
+			if filepathHasPrefix(wd, goPath) {
 				p.absPath = filepath.Join(srcPath, projectName)
 				break
 			}
@@ -90,7 +91,7 @@ func NewProjectFromPath(absPath string) *Project {
 func trimSrcPath(absPath, srcPath string) string {
 	relPath, err := filepath.Rel(srcPath, absPath)
 	if err != nil {
-		er("Cobra supports project only within $GOPATH")
+		er("Cobra supports project only within $GOPATH: " + err.Error())
 	}
 	return relPath
 }
@@ -172,7 +173,7 @@ func (p *Project) SrcPath() string {
 	}
 
 	for _, srcPath := range srcPaths {
-		if strings.HasPrefix(p.absPath, srcPath) {
+		if filepathHasPrefix(p.absPath, srcPath) {
 			p.srcPath = srcPath
 			break
 		}
@@ -180,3 +181,15 @@ func (p *Project) SrcPath() string {
 
 	return p.srcPath
 }
+
+func filepathHasPrefix(path string, prefix string) bool {
+	if len(path) <= len(prefix) {
+		return false
+	}
+	if runtime.GOOS == "windows" {
+		// Paths in windows are case-insensitive.
+		return strings.EqualFold(path[0:len(prefix)], prefix)
+	}
+	return path[0:len(prefix)] == prefix
+
+}
diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go
index 3e0d5fd..cfc7852 100644
--- a/cobra/cmd/root.go
+++ b/cobra/cmd/root.go
@@ -15,28 +15,29 @@ package cmd
 
 import (
 	"fmt"
-	"os"
 
 	homedir "github.com/mitchellh/go-homedir"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 )
 
-var cfgFile, projectBase, userLicense string // are used for flags
+var (
+	// Used for flags.
+	cfgFile, projectBase, userLicense string
 
-var rootCmd = &cobra.Command{
-	Use:   "cobra",
-	Short: "A generator for Cobra based Applications",
-	Long: `Cobra is a CLI library for Go that empowers applications.
+	rootCmd = &cobra.Command{
+		Use:   "cobra",
+		Short: "A generator for Cobra based Applications",
+		Long: `Cobra is a CLI library for Go that empowers applications.
 This application is a tool to generate the needed files
 to quickly create a Cobra application.`,
-}
+	}
+)
 
 // Execute executes the root command.
 func Execute() {
 	if err := rootCmd.Execute(); err != nil {
-		fmt.Println(err)
-		os.Exit(1)
+		er(err)
 	}
 }
 
@@ -67,8 +68,7 @@ func initViper() {
 		// Find home directory.
 		home, err := homedir.Dir()
 		if err != nil {
-			fmt.Println(home)
-			os.Exit(1)
+			er(err)
 		}
 
 		// Search config in home directory with name ".cobra" (without extension).
diff --git a/command.go b/command.go
index 9e6bdf3..f37667f 100644
--- a/command.go
+++ b/command.go
@@ -430,37 +430,28 @@ func stripFlags(args []string, c *Command) []string {
 	c.mergePersistentFlags()
 
 	commands := []string{}
-	inQuote := false
 	flags := c.Flags()
 
 Loop:
 	for len(args) > 0 {
 		s := args[0]
 		args = args[1:]
-		if !inQuote {
-			switch {
-			case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):
-				inQuote = true
-			case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
-				// If '--flag arg' then
-				// delete arg from args.
-				fallthrough // (do the same as below)
-			case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
-				// If '-f arg' then
-				// delete 'arg' from args or break the loop if len(args) <= 1.
-				if len(args) <= 1 {
-					break Loop
-				} else {
-					args = args[1:]
-					continue
-				}
-			case s != "" && !strings.HasPrefix(s, "-"):
-				commands = append(commands, s)
+		switch {
+		case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
+			// If '--flag arg' then
+			// delete arg from args.
+			fallthrough // (do the same as below)
+		case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
+			// If '-f arg' then
+			// delete 'arg' from args or break the loop if len(args) <= 1.
+			if len(args) <= 1 {
+				break Loop
+			} else {
+				args = args[1:]
+				continue
 			}
-		}
-
-		if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {
-			inQuote = false
+		case s != "" && !strings.HasPrefix(s, "-"):
+			commands = append(commands, s)
 		}
 	}
 
@@ -709,7 +700,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
 
 	// initialize help as the last point possible to allow for user
 	// overriding
-	c.initHelpCmd()
+	c.InitDefaultHelpCmd()
 
 	var args []string
 
@@ -773,31 +764,32 @@ func (c *Command) InitDefaultHelpFlag() {
 	}
 }
 
-func (c *Command) initHelpCmd() {
-	if c.helpCommand == nil {
-		if !c.HasSubCommands() {
-			return
-		}
+// InitDefaultHelpCmd adds default help command to c.
+// It is called automatically by executing the c or by calling help and usage.
+// If c already has help command or c has no subcommands, it will do nothing.
+func (c *Command) InitDefaultHelpCmd() {
+	if c.helpCommand != nil || !c.HasSubCommands() {
+		return
+	}
 
-		c.helpCommand = &Command{
-			Use:   "help [command]",
-			Short: "Help about any command",
-			Long: `Help provides help for any command in the application.
+	c.helpCommand = &Command{
+		Use:   "help [command]",
+		Short: "Help about any command",
+		Long: `Help provides help for any command in the application.
     Simply type ` + c.Name() + ` help [path to command] for full details.`,
-			PersistentPreRun:  func(cmd *Command, args []string) {},
-			PersistentPostRun: func(cmd *Command, args []string) {},
-
-			Run: func(c *Command, args []string) {
-				cmd, _, e := c.Root().Find(args)
-				if cmd == nil || e != nil {
-					c.Printf("Unknown help topic %#q\n", args)
-					c.Root().Usage()
-				} else {
-					cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
-					cmd.Help()
-				}
-			},
-		}
+		PersistentPreRun:  func(cmd *Command, args []string) {},
+		PersistentPostRun: func(cmd *Command, args []string) {},
+
+		Run: func(c *Command, args []string) {
+			cmd, _, e := c.Root().Find(args)
+			if cmd == nil || e != nil {
+				c.Printf("Unknown help topic %#q\n", args)
+				c.Root().Usage()
+			} else {
+				cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
+				cmd.Help()
+			}
+		},
 	}
 	c.RemoveCommand(c.helpCommand)
 	c.AddCommand(c.helpCommand)
@@ -1275,8 +1267,8 @@ func (c *Command) Parent() *Command {
 // mergePersistentFlags merges c.PersistentFlags() to c.Flags()
 // and adds missing persistent flags of all parents.
 func (c *Command) mergePersistentFlags() {
-	c.Flags().AddFlagSet(c.PersistentFlags())
 	c.updateParentsPflags()
+	c.Flags().AddFlagSet(c.PersistentFlags())
 	c.Flags().AddFlagSet(c.parentsPflags)
 }
 
diff --git a/doc/man_docs.go b/doc/man_docs.go
index 74b284d..ce92332 100644
--- a/doc/man_docs.go
+++ b/doc/man_docs.go
@@ -190,6 +190,9 @@ func manPrintOptions(buf *bytes.Buffer, command *cobra.Command) {
 }
 
 func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
+	cmd.InitDefaultHelpCmd()
+	cmd.InitDefaultHelpFlag()
+
 	// something like `rootcmd-subcmd1-subcmd2`
 	dashCommandName := strings.Replace(cmd.CommandPath(), " ", "-", -1)
 
diff --git a/doc/md_docs.go b/doc/md_docs.go
index 9bbfa36..68cf5bf 100644
--- a/doc/md_docs.go
+++ b/doc/md_docs.go
@@ -52,6 +52,9 @@ func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
 
 // GenMarkdownCustom creates custom markdown output.
 func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
+	cmd.InitDefaultHelpCmd()
+	cmd.InitDefaultHelpFlag()
+
 	buf := new(bytes.Buffer)
 	name := cmd.CommandPath()
 
diff --git a/doc/yaml_docs.go b/doc/yaml_docs.go
index 54c244d..ea00af0 100644
--- a/doc/yaml_docs.go
+++ b/doc/yaml_docs.go
@@ -89,6 +89,9 @@ func GenYaml(cmd *cobra.Command, w io.Writer) error {
 
 // GenYamlCustom creates custom yaml output.
 func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
+	cmd.InitDefaultHelpCmd()
+	cmd.InitDefaultHelpFlag()
+
 	yamlDoc := cmdDoc{}
 	yamlDoc.Name = cmd.CommandPath()
 


Test patch:
diff --git a/bash_completions_test.go b/bash_completions_test.go
index 185570b..a626360 100644
--- a/bash_completions_test.go
+++ b/bash_completions_test.go
@@ -2,16 +2,13 @@ package cobra
 
 import (
 	"bytes"
-	"fmt"
+	"io/ioutil"
 	"os"
 	"os/exec"
 	"strings"
 	"testing"
 )
 
-var _ = fmt.Println
-var _ = os.Stderr
-
 func checkOmit(t *testing.T, found, unexpected string) {
 	if strings.Contains(found, unexpected) {
 		t.Errorf("Unexpected response.\nGot: %q\nBut should not have!\n", unexpected)
@@ -178,3 +175,22 @@ func TestBashCompletionDeprecatedFlag(t *testing.T) {
 		t.Errorf("expected completion to not include %q flag: Got %v", flagName, bashCompletion)
 	}
 }
+
+func BenchmarkBashCompletion(b *testing.B) {
+	c := initializeWithRootCmd()
+	cmdEcho.AddCommand(cmdTimes)
+	c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated, cmdColon)
+
+	file, err := ioutil.TempFile("", "")
+	if err != nil {
+		b.Fatal(err)
+	}
+	defer os.Remove(file.Name())
+
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		if err := c.GenBashCompletion(file); err != nil {
+			b.Fatal(err)
+		}
+	}
+}
diff --git a/cobra/cmd/init_test.go b/cobra/cmd/init_test.go
index f9c9c46..9a918b9 100644
--- a/cobra/cmd/init_test.go
+++ b/cobra/cmd/init_test.go
@@ -54,7 +54,9 @@ func TestGoldenInitCmd(t *testing.T) {
 				if err != nil {
 					return err
 				}
-				ioutil.WriteFile(goldenPath, got, 0644)
+				if err := ioutil.WriteFile(goldenPath, got, 0644); err != nil {
+					t.Fatal("Error while updating file:", err)
+				}
 			}
 			return compareFiles(path, goldenPath)
 		}
diff --git a/cobra/cmd/project_test.go b/cobra/cmd/project_test.go
index 2774094..037f7c5 100644
--- a/cobra/cmd/project_test.go
+++ b/cobra/cmd/project_test.go
@@ -1,7 +1,6 @@
 package cmd
 
 import (
-	"path/filepath"
 	"testing"
 )
 
@@ -17,7 +16,7 @@ func TestFindExistingPackage(t *testing.T) {
 
 func hasGoPathPrefix(path string) bool {
 	for _, srcPath := range srcPaths {
-		if filepath.HasPrefix(path, srcPath) {
+		if filepathHasPrefix(path, srcPath) {
 			return true
 		}
 	}
diff --git a/cobra/cmd/testdata/root.go.golden b/cobra/cmd/testdata/root.go.golden
index a8f4ff1..13e9a77 100644
--- a/cobra/cmd/testdata/root.go.golden
+++ b/cobra/cmd/testdata/root.go.golden
@@ -70,13 +70,13 @@ func initConfig() {
 		// Find home directory.
 		home, err := homedir.Dir()
 		if err != nil {
-			fmt.Println(home)
+			fmt.Println(err)
 			os.Exit(1)
 		}
 
-		// Search config in home directory with name ".cobra" (without extension).
+		// Search config in home directory with name ".testproject" (without extension).
 		viper.AddConfigPath(home)
-		viper.SetConfigName(".cobra")
+		viper.SetConfigName(".testproject")
 	}
 
 	viper.AutomaticEnv() // read in environment variables that match
diff --git a/cobra_test.go b/cobra_test.go
index 6f3ceb1..576c97d 100644
--- a/cobra_test.go
+++ b/cobra_test.go
@@ -13,13 +13,12 @@ import (
 	"github.com/spf13/pflag"
 )
 
-var tp, te, tt, t1, tr []string
+var tp, te, tt, tr []string
 var rootPersPre, echoPre, echoPersPre, timesPersPre []string
 var flagb1, flagb2, flagb3, flagbr, flagbp bool
 var flags1, flags2a, flags2b, flags3, outs string
 var flagi1, flagi2, flagi3, flagi4, flagir int
-var globalFlag1 bool
-var flagEcho, rootcalled bool
+var rootcalled bool
 var versionUsed int
 
 const strtwoParentHelp = "help message for parent flag strtwo"
@@ -227,36 +226,36 @@ type resulter struct {
 	Command *Command
 }
 
-func fullSetupTest(input string) resulter {
+func fullSetupTest(args ...string) resulter {
 	c := initializeWithRootCmd()
 
-	return fullTester(c, input)
+	return fullTester(c, args...)
 }
 
-func noRRSetupTestSilenced(input string) resulter {
+func noRRSetupTestSilenced(args ...string) resulter {
 	c := initialize()
 	c.SilenceErrors = true
 	c.SilenceUsage = true
-	return fullTester(c, input)
+	return fullTester(c, args...)
 }
 
-func noRRSetupTest(input string) resulter {
+func noRRSetupTest(args ...string) resulter {
 	c := initialize()
 
-	return fullTester(c, input)
+	return fullTester(c, args...)
 }
 
-func rootOnlySetupTest(input string) resulter {
+func rootOnlySetupTest(args ...string) resulter {
 	c := initializeWithRootCmd()
 
-	return simpleTester(c, input)
+	return simpleTester(c, args...)
 }
 
-func simpleTester(c *Command, input string) resulter {
+func simpleTester(c *Command, args ...string) resulter {
 	buf := new(bytes.Buffer)
 	// Testing flag with invalid input
 	c.SetOutput(buf)
-	c.SetArgs(strings.Split(input, " "))
+	c.SetArgs(args)
 
 	err := c.Execute()
 	output := buf.String()
@@ -264,11 +263,11 @@ func simpleTester(c *Command, input string) resulter {
 	return resulter{err, output, c}
 }
 
-func simpleTesterC(c *Command, input string) resulter {
+func simpleTesterC(c *Command, args ...string) resulter {
 	buf := new(bytes.Buffer)
 	// Testing flag with invalid input
 	c.SetOutput(buf)
-	c.SetArgs(strings.Split(input, " "))
+	c.SetArgs(args)
 
 	cmd, err := c.ExecuteC()
 	output := buf.String()
@@ -276,13 +275,13 @@ func simpleTesterC(c *Command, input string) resulter {
 	return resulter{err, output, cmd}
 }
 
-func fullTester(c *Command, input string) resulter {
+func fullTester(c *Command, args ...string) resulter {
 	buf := new(bytes.Buffer)
 	// Testing flag with invalid input
 	c.SetOutput(buf)
 	cmdEcho.AddCommand(cmdTimes)
 	c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdCustomFlags, cmdDeprecated)
-	c.SetArgs(strings.Split(input, " "))
+	c.SetArgs(args)
 
 	err := c.Execute()
 	output := buf.String()
@@ -332,7 +331,7 @@ func checkOutputContains(t *testing.T, c *Command, check string) {
 }
 
 func TestSingleCommand(t *testing.T) {
-	noRRSetupTest("print one two")
+	noRRSetupTest("print", "one", "two")
 
 	if te != nil || tt != nil {
 		t.Error("Wrong command called")
@@ -346,7 +345,7 @@ func TestSingleCommand(t *testing.T) {
 }
 
 func TestChildCommand(t *testing.T) {
-	noRRSetupTest("echo times one two")
+	noRRSetupTest("echo", "times", "one", "two")
 
 	if te != nil || tp != nil {
 		t.Error("Wrong command called")
@@ -360,7 +359,7 @@ func TestChildCommand(t *testing.T) {
 }
 
 func TestCommandAlias(t *testing.T) {
-	noRRSetupTest("say times one two")
+	noRRSetupTest("say", "times", "one", "two")
 
 	if te != nil || tp != nil {
 		t.Error("Wrong command called")
@@ -375,7 +374,7 @@ func TestCommandAlias(t *testing.T) {
 
 func TestPrefixMatching(t *testing.T) {
 	EnablePrefixMatching = true
-	noRRSetupTest("ech times one two")
+	noRRSetupTest("ech", "times", "one", "two")
 
 	if te != nil || tp != nil {
 		t.Error("Wrong command called")
@@ -393,7 +392,7 @@ func TestPrefixMatching(t *testing.T) {
 func TestNoPrefixMatching(t *testing.T) {
 	EnablePrefixMatching = false
 
-	noRRSetupTest("ech times one two")
+	noRRSetupTest("ech", "times", "one", "two")
 
 	if !(tt == nil && te == nil && tp == nil) {
 		t.Error("Wrong command called")
@@ -402,7 +401,7 @@ func TestNoPrefixMatching(t *testing.T) {
 
 func TestAliasPrefixMatching(t *testing.T) {
 	EnablePrefixMatching = true
-	noRRSetupTest("sa times one two")
+	noRRSetupTest("sa", "times", "one", "two")
 
 	if te != nil || tp != nil {
 		t.Error("Wrong command called")
@@ -419,7 +418,7 @@ func TestAliasPrefixMatching(t *testing.T) {
 func TestChildSameName(t *testing.T) {
 	c := initializeWithSameName()
 	c.AddCommand(cmdPrint, cmdEcho)
-	c.SetArgs(strings.Split("print one two", " "))
+	c.SetArgs([]string{"print", "one", "two"})
 	c.Execute()
 
 	if te != nil || tt != nil {
@@ -437,7 +436,7 @@ func TestGrandChildSameName(t *testing.T) {
 	c := initializeWithSameName()
 	cmdTimes.AddCommand(cmdPrint)
 	c.AddCommand(cmdTimes)
-	c.SetArgs(strings.Split("times print one two", " "))
+	c.SetArgs([]string{"times", "print", "one", "two"})
 	c.Execute()
 
 	if te != nil || tt != nil {
@@ -454,13 +453,13 @@ func TestGrandChildSameName(t *testing.T) {
 func TestUsage(t *testing.T) {
 	x := fullSetupTest("help")
 	checkResultContains(t, x, cmdRootWithRun.Use+" [flags]")
-	x = fullSetupTest("help customflags")
+	x = fullSetupTest("help", "customflags")
 	checkResultContains(t, x, cmdCustomFlags.Use)
 	checkResultOmits(t, x, cmdCustomFlags.Use+" [flags]")
 }
 
 func TestFlagLong(t *testing.T) {
-	noRRSetupTest("echo --intone=13 something -- here")
+	noRRSetupTest("echo", "--intone=13", "something", "--", "here")
 
 	if cmdEcho.ArgsLenAtDash() != 1 {
 		t.Errorf("expected argsLenAtDash: %d but got %d", 1, cmdRootNoRun.ArgsLenAtDash())
@@ -477,7 +476,7 @@ func TestFlagLong(t *testing.T) {
 }
 
 func TestFlagShort(t *testing.T) {
-	noRRSetupTest("echo -i13 -- something here")
+	noRRSetupTest("echo", "-i13", "--", "something", "here")
 
 	if cmdEcho.ArgsLenAtDash() != 0 {
 		t.Errorf("expected argsLenAtDash: %d but got %d", 0, cmdRootNoRun.ArgsLenAtDash())
@@ -492,7 +491,7 @@ func TestFlagShort(t *testing.T) {
 		t.Errorf("default flag value changed, 234 expected, %d given", flagi2)
 	}
 
-	noRRSetupTest("echo -i 13 something here")
+	noRRSetupTest("echo", "-i", "13", "something", "here")
 
 	if strings.Join(te, " ") != "something here" {
 		t.Errorf("flags didn't leave proper args remaining..%s given", te)
@@ -504,7 +503,7 @@ func TestFlagShort(t *testing.T) {
 		t.Errorf("default flag value changed, 234 expected, %d given", flagi2)
 	}
 
-	noRRSetupTest("print -i99 one two")
+	noRRSetupTest("print", "-i99", "one", "two")
 
 	if strings.Join(tp, " ") != "one two" {
 		t.Errorf("flags didn't leave proper args remaining..%s given", tp)
@@ -518,14 +517,14 @@ func TestFlagShort(t *testing.T) {
 }
 
 func TestChildCommandFlags(t *testing.T) {
-	noRRSetupTest("echo times -j 99 one two")
+	noRRSetupTest("echo", "times", "-j", "99", "one", "two")
 
 	if strings.Join(tt, " ") != "one two" {
 		t.Errorf("flags didn't leave proper args remaining..%s given", tt)
 	}
 
 	// Testing with flag that shouldn't be persistent
-	r := noRRSetupTest("echo times -j 99 -i77 one two")
+	r := noRRSetupTest("echo", "times", "-j", "99", "-i77", "one", "two")
 
 	if r.Error == nil {
 		t.Errorf("invalid flag should generate error")
@@ -544,7 +543,7 @@ func TestChildCommandFlags(t *testing.T) {
 	}
 
 	// Testing with flag only existing on child
-	r = noRRSetupTest("echo -j 99 -i77 one two")
+	r = noRRSetupTest("echo", "-j", "99", "-i77", "one", "two")
 
 	if r.Error == nil {
 		t.Errorf("invalid flag should generate error")
@@ -554,7 +553,7 @@ func TestChildCommandFlags(t *testing.T) {
 	}
 
 	// Testing with persistent flag overwritten by child
-	noRRSetupTest("echo times --strtwo=child one two")
+	noRRSetupTest("echo", "times", "--strtwo=child", "one", "two")
 
 	if flags2b != "child" {
 		t.Errorf("flag value should be child, %s given", flags2b)
@@ -565,7 +564,7 @@ func TestChildCommandFlags(t *testing.T) {
 	}
 
 	// Testing flag with invalid input
-	r = noRRSetupTest("echo -i10E")
+	r = noRRSetupTest("echo", "-i10E")
 
 	if r.Error == nil {
 		t.Errorf("invalid input should generate error")
@@ -576,7 +575,7 @@ func TestChildCommandFlags(t *testing.T) {
 }
 
 func TestTrailingCommandFlags(t *testing.T) {
-	x := fullSetupTest("echo two -x")
+	x := fullSetupTest("echo", "two", "-x")
 
 	if x.Error == nil {
 		t.Errorf("invalid flag should generate error")
@@ -587,7 +586,7 @@ func TestInvalidSubcommandFlags(t *testing.T) {
 	cmd := initializeWithRootCmd()
 	cmd.AddCommand(cmdTimes)
 
-	result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")
+	result := simpleTester(cmd, "times", "--inttwo=2", "--badflag=bar")
 	// given that we are not checking here result.Error we check for
 	// stock usage message
 	checkResultContains(t, result, "cobra-test times [# times]")
@@ -617,14 +616,14 @@ func TestSubcommandExecuteC(t *testing.T) {
 
 	cmd.AddCommand(double, echo)
 
-	result := simpleTesterC(cmd, "double hello world")
+	result := simpleTesterC(cmd, "double", "hello", "world")
 	checkResultContains(t, result, "hello world hello world")
 
 	if result.Command.Name() != "double" {
 		t.Errorf("invalid cmd returned from ExecuteC: should be 'double' but got %s", result.Command.Name())
 	}
 
-	result = simpleTesterC(cmd, "echo msg to be echoed")
+	result = simpleTesterC(cmd, "echo", "msg", "to", "be", "echoed")
 	checkResultContains(t, result, "msg to be echoed")
 
 	if result.Command.Name() != "echo" {
@@ -650,16 +649,16 @@ func TestSubcommandArgEvaluation(t *testing.T) {
 	}
 	first.AddCommand(second)
 
-	result := simpleTester(cmd, "first second first third")
+	result := simpleTester(cmd, "first", "second", "first", "third")
 
-	expectedOutput := fmt.Sprintf("%v", []string{"first third"})
+	expectedOutput := fmt.Sprint([]string{"first third"})
 	if result.Output != expectedOutput {
 		t.Errorf("exptected %v, got %v", expectedOutput, result.Output)
 	}
 }
 
 func TestPersistentFlags(t *testing.T) {
-	fullSetupTest("echo -s something -p more here")
+	fullSetupTest("echo", "-s", "something", "-p", "more", "here")
 
 	// persistentFlag should act like normal flag on its own command
 	if strings.Join(te, " ") != "more here" {
@@ -673,7 +672,7 @@ func TestPersistentFlags(t *testing.T) {
 	}
 
 	// persistentFlag should act like normal flag on its own command
-	fullSetupTest("echo times -s again -c -p test here")
+	fullSetupTest("echo", "times", "-s", "again", "-c", "-p", "test", "here")
 
 	if strings.Join(tt, " ") != "test here" {
 		t.Errorf("flags didn't leave proper args remaining. %s given", tt)
@@ -695,17 +694,17 @@ func TestHelpCommand(t *testing.T) {
 	x := fullSetupTest("help")
 	checkResultContains(t, x, cmdRootWithRun.Long)
 
-	x = fullSetupTest("help echo")
+	x = fullSetupTest("help", "echo")
 	checkResultContains(t, x, cmdEcho.Long)
 
-	x = fullSetupTest("help echo times")
+	x = fullSetupTest("help", "echo", "times")
 	checkResultContains(t, x, cmdTimes.Long)
 }
 
 func TestChildCommandHelp(t *testing.T) {
-	c := noRRSetupTest("print --help")
+	c := noRRSetupTest("print", "--help")
 	checkResultContains(t, c, strtwoParentHelp)
-	r := noRRSetupTest("echo times --help")
+	r := noRRSetupTest("echo", "times", "--help")
 	checkResultContains(t, r, strtwoChildHelp)
 }
 
@@ -717,7 +716,7 @@ func TestNonRunChildHelp(t *testing.T) {
 func TestRunnableRootCommand(t *testing.T) {
 	x := fullSetupTest("")
 
-	if rootcalled != true {
+	if !rootcalled {
 		t.Errorf("Root Function was not called\n out:%v", x.Error)
 	}
 }
@@ -751,7 +750,6 @@ func TestVisitParents(t *testing.T) {
 }
 
 func TestRunnableRootCommandNilInput(t *testing.T) {
-	var emptyArg []string
 	c := initializeWithRootCmd()
 
 	buf := new(bytes.Buffer)
@@ -759,23 +757,20 @@ func TestRunnableRootCommandNilInput(t *testing.T) {
 	c.SetOutput(buf)
 	cmdEcho.AddCommand(cmdTimes)
 	c.AddCommand(cmdPrint, cmdEcho)
-	c.SetArgs(emptyArg)
+	c.SetArgs([]string{})
 
 	err := c.Execute()
 	if err != nil {
 		t.Errorf("Execute() failed with %v", err)
 	}
 
-	if rootcalled != true {
+	if !rootcalled {
 		t.Errorf("Root Function was not called")
 	}
 }
 
 func TestRunnableRootCommandEmptyInput(t *testing.T) {
-	args := make([]string, 3)
-	args[0] = ""
-	args[1] = "--introot=12"
-	args[2] = ""
+	args := []string{"", "--introot=12", ""}
 	c := initializeWithRootCmd()
 
 	buf := new(bytes.Buffer)
@@ -787,13 +782,13 @@ func TestRunnableRootCommandEmptyInput(t *testing.T) {
 
 	c.Execute()
 
-	if rootcalled != true {
-		t.Errorf("Root Function was not called.\n\nOutput was:\n\n%s\n", buf)
+	if !rootcalled {
+		t.Errorf("Root Function was not called.\nOutput was:\n%s\n", buf)
 	}
 }
 
 func TestInvalidSubcommandWhenArgsAllowed(t *testing.T) {
-	fullSetupTest("echo invalid-sub")
+	fullSetupTest("echo", "invalid-sub")
 
 	if te[0] != "invalid-sub" {
 		t.Errorf("Subcommand didn't work...")
@@ -801,9 +796,9 @@ func TestInvalidSubcommandWhenArgsAllowed(t *testing.T) {
 }
 
 func TestRootFlags(t *testing.T) {
-	fullSetupTest("-i 17 -b")
+	fullSetupTest("-i", "17", "-b")
 
-	if flagbr != true {
+	if !flagbr {
 		t.Errorf("flag value should be true, %v given", flagbr)
 	}
 
@@ -826,7 +821,7 @@ func TestRootHelp(t *testing.T) {
 		t.Errorf("--help shouldn't display subcommand's usage, Got: \n %s", x.Output)
 	}
 
-	x = fullSetupTest("echo --help")
+	x = fullSetupTest("echo", "--help")
 
 	if strings.Contains(x.Output, cmdTimes.Use) {
 		t.Errorf("--help shouldn't display subsubcommand's usage, Got: \n %s", x.Output)
@@ -861,7 +856,6 @@ func TestFlagAccess(t *testing.T) {
 }
 
 func TestNoNRunnableRootCommandNilInput(t *testing.T) {
-	var args []string
 	c := initialize()
 
 	buf := new(bytes.Buffer)
@@ -869,7 +863,7 @@ func TestNoNRunnableRootCommandNilInput(t *testing.T) {
 	c.SetOutput(buf)
 	cmdEcho.AddCommand(cmdTimes)
 	c.AddCommand(cmdPrint, cmdEcho)
-	c.SetArgs(args)
+	c.SetArgs([]string{})
 
 	c.Execute()
 
@@ -888,7 +882,7 @@ func TestRootNoCommandHelp(t *testing.T) {
 		t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
 	}
 
-	x = rootOnlySetupTest("echo --help")
+	x = rootOnlySetupTest("echo", "--help")
 
 	checkResultOmits(t, x, "Available Commands:")
 	checkResultOmits(t, x, "for more information about a command")
@@ -906,7 +900,7 @@ func TestRootUnknownCommand(t *testing.T) {
 		t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
 	}
 
-	r = noRRSetupTest("--strtwo=a bogus")
+	r = noRRSetupTest("--strtwo=a", "bogus")
 	if r.Output != s {
 		t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
 	}
@@ -919,7 +913,7 @@ func TestRootUnknownCommandSilenced(t *testing.T) {
 		t.Errorf("Unexpected response.\nExpecting to be: \n\"\"\n Got:\n %q\n", r.Output)
 	}
 
-	r = noRRSetupTestSilenced("--strtwo=a bogus")
+	r = noRRSetupTestSilenced("--strtwo=a", "bogus")
 	if r.Output != "" {
 		t.Errorf("Unexpected response.\nExpecting to be:\n\"\"\nGot:\n %q\n", r.Output)
 	}
@@ -966,56 +960,27 @@ func TestRootSuggestions(t *testing.T) {
 
 func TestFlagsBeforeCommand(t *testing.T) {
 	// short without space
-	x := fullSetupTest("-i10 echo")
+	x := fullSetupTest("-i10", "echo")
 	if x.Error != nil {
 		t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
 	}
 
-	// short (int) with equals
-	// It appears that pflags doesn't support this...
-	// Commenting out until support can be added
-
-	//x = noRRSetupTest("echo -i=10")
-	//if x.Error != nil {
-	//t.Errorf("Valid Input shouldn't have errors, got:\n %s", x.Error)
-	//}
+	x = noRRSetupTest("echo", "-i=10")
+	if x.Error != nil {
+		t.Errorf("Valid Input shouldn't have errors, got:\n %s", x.Error)
+	}
 
 	// long with equals
-	x = noRRSetupTest("--intone=123 echo one two")
+	x = noRRSetupTest("--intone=123", "echo", "one", "two")
 	if x.Error != nil {
 		t.Errorf("Valid Input shouldn't have errors, got:\n %s", x.Error)
 	}
 
 	// With parsing error properly reported
-	x = fullSetupTest("-i10E echo")
+	x = fullSetupTest("-i10E", "echo")
 	if !strings.Contains(x.Error.Error(), "invalid syntax") {
 		t.Errorf("Wrong error message displayed, \n %s", x.Error)
 	}
-
-	//With quotes
-	x = fullSetupTest("-s=\"walking\" echo")
-	if x.Error != nil {
-		t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
-	}
-
-	//With quotes and space
-	x = fullSetupTest("-s=\"walking fast\" echo")
-	if x.Error != nil {
-		t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
-	}
-
-	//With inner quote
-	x = fullSetupTest("-s=\"walking \\\"Inner Quote\\\" fast\" echo")
-	if x.Error != nil {
-		t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
-	}
-
-	//With quotes and space
-	x = fullSetupTest("-s=\"walking \\\"Inner Quote\\\" fast\" echo")
-	if x.Error != nil {
-		t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
-	}
-
 }
 
 func TestRemoveCommand(t *testing.T) {
@@ -1081,7 +1046,7 @@ func TestDeprecatedSub(t *testing.T) {
 }
 
 func TestPreRun(t *testing.T) {
-	noRRSetupTest("echo one two")
+	noRRSetupTest("echo", "one", "two")
 	if echoPre == nil || echoPersPre == nil {
 		t.Error("PreRun or PersistentPreRun not called")
 	}
@@ -1089,7 +1054,7 @@ func TestPreRun(t *testing.T) {
 		t.Error("Wrong *Pre functions called!")
 	}
 
-	noRRSetupTest("echo times one two")
+	noRRSetupTest("echo", "times", "one", "two")
 	if timesPersPre == nil {
 		t.Error("PreRun or PersistentPreRun not called")
 	}
@@ -1097,7 +1062,7 @@ func TestPreRun(t *testing.T) {
 		t.Error("Wrong *Pre functions called!")
 	}
 
-	noRRSetupTest("print one two")
+	noRRSetupTest("print", "one", "two")
 	if rootPersPre == nil {
 		t.Error("Parent PersistentPreRun not called but should not have been")
 	}
@@ -1115,10 +1080,10 @@ func TestPeristentPreRunPropagation(t *testing.T) {
 	// Now add cmdPrint to rootCmd
 	rootCmd.AddCommand(cmdPrint)
 
-	rootCmd.SetArgs(strings.Split("print echosub lala", " "))
+	rootCmd.SetArgs([]string{"print", "echosub", "lala"})
 	rootCmd.Execute()
 
-	if rootPersPre == nil || len(rootPersPre) == 0 || rootPersPre[0] != "lala" {
+	if len(rootPersPre) == 0 || rootPersPre[0] != "lala" {
 		t.Error("RootCmd PersistentPreRun not called but should have been")
 	}
 }
@@ -1150,12 +1115,12 @@ func TestGlobalNormFuncPropagation(t *testing.T) {
 
 func TestFlagOnPflagCommandLine(t *testing.T) {
 	flagName := "flagOnCommandLine"
-	pflag.CommandLine.String(flagName, "", "about my flag")
+	pflag.String(flagName, "", "about my flag")
 	r := fullSetupTest("--help")
 
 	checkResultContains(t, r, flagName)
 
-	// reset CommandLine flagset
+	// Reset pflag.CommandLine flagset.
 	pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
 }
 
diff --git a/command_test.go b/command_test.go
index 24abd3e..978bacc 100644
--- a/command_test.go
+++ b/command_test.go
@@ -227,7 +227,7 @@ func TestFlagErrorFunc(t *testing.T) {
 
 // TestSortedFlags checks,
 // if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
-// https://github.com/spf13/cobra/issues/404
+// Related to https://github.com/spf13/cobra/issues/404.
 func TestSortedFlags(t *testing.T) {
 	cmd := &Command{}
 	cmd.Flags().SortFlags = false
@@ -263,13 +263,13 @@ func isStringInStringSlice(s string, ss []string) bool {
 // TestHelpFlagInHelp checks,
 // if '--help' flag is shown in help for child (executing `parent help child`),
 // that has no other flags.
-// https://github.com/spf13/cobra/issues/302
+// Related to https://github.com/spf13/cobra/issues/302.
 func TestHelpFlagInHelp(t *testing.T) {
 	output := new(bytes.Buffer)
-	parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }}
+	parent := &Command{Use: "parent", Run: func(*Command, []string) {}}
 	parent.SetOutput(output)
 
-	child := &Command{Use: "child", Long: "long", Run: func(*Command, []string) { return }}
+	child := &Command{Use: "child", Run: func(*Command, []string) {}}
 	parent.AddCommand(child)
 
 	parent.SetArgs([]string{"help", "child"})
@@ -279,6 +279,22 @@ func TestHelpFlagInHelp(t *testing.T) {
 	}
 
 	if !strings.Contains(output.String(), "[flags]") {
-		t.Fatalf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())
+		t.Errorf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())
 	}
 }
+
+// TestMergeCommandLineToFlags checks,
+// if pflag.CommandLine is correctly merged to c.Flags() after first call
+// of c.mergePersistentFlags.
+// Related to https://github.com/spf13/cobra/issues/443.
+func TestMergeCommandLineToFlags(t *testing.T) {
+	pflag.Bool("boolflag", false, "")
+	c := &Command{Use: "c", Run: func(*Command, []string) {}}
+	c.mergePersistentFlags()
+	if c.Flags().Lookup("boolflag") == nil {
+		t.Fatal("Expecting to have flag from CommandLine in c.Flags()")
+	}
+
+	// Reset pflag.CommandLine flagset.
+	pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
+}