func completion(args []string) error {
	root := root.CmdForDocs()
	bash := "completion/bash.sh"
	fish := "completion/fish.sh"
	powershell := "completion/powershell.ps1"
	zsh := "completion/zsh.sh"
	if len(args) < 3 {
		if err := root.GenBashCompletionFile(bash); err != nil {
			return err
		}
		if err := root.GenFishCompletionFile(fish, true); err != nil {
			return err
		}
		if err := root.GenPowerShellCompletionFile(powershell); err != nil {
			return err
		}
		return root.GenZshCompletionFile(zsh)
	}
	switch args[2] {
	case "bash":
		return root.GenBashCompletionFile(bash)
	case "fish":
		return root.GenFishCompletionFile(fish, true)
	case "powershell":
		return root.GenPowerShellCompletionFile(powershell)
	case "zsh":
		return root.GenZshCompletionFile(zsh)
	}
	return nil
}
Generate code completion files. By default it generates all the files.

Usage: main --completion bash.
package main
import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"strings"
	"text/template"
	"unicode"
	"github.com/GGP1/kure/commands/root"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)
func main() {
	cmd := flag.Bool("cmd", false, "specific command documentation")
	comp := flag.Bool("completion", false, "generate completion")
	summ := flag.Bool("summary", false, "commands summary")
	flag.Parse()
	if *cmd {
		if err := cmdDocs(os.Args); err != nil {
			log.Fatalf("failed generating %s documentation: %v", os.Args[1], err)
		}
	} else if *comp {
		if err := completion(os.Args); err != nil {
			log.Fatalf("failed generating %s completion: %v", os.Args[1], err)
		}
	} else if *summ {
		if err := summary(os.Args); err != nil {
			log.Fatalf("failed generating commands summary: %v", err)
		}
	}
}
func cmdDocs(args []string) error {
	root := root.CmdForDocs()
	cmd, _, err := root.Find(args[2:])
	if err != nil {
		return err
	}
	return customMarkdown(cmd, os.Stdout)
}
func customMarkdown(cmd *cobra.Command, w io.Writer) error {
	funcs := template.FuncMap{
		"sub": func(a, b int) int {
			return a - b
		},
		"replace": func(s, old, new string, n int) string {
			return strings.Replace(s, old, new, n)
		},
		"visitFlags": func(cmd *cobra.Command) string {
			return visitFlags(cmd)
		},
		"visitFlagsTable": func(cmd *cobra.Command) string {
			buf := new(bytes.Buffer)
			cmd.Flags().VisitAll(func(f *pflag.Flag) {
				usage := []byte(f.Usage)
				usage[0] = byte(unicode.ToUpper(rune(usage[0])))
				buf.WriteString(fmt.Sprintf("| %s | %s | %s | %s | %s |\n", f.Name, f.Shorthand, f.Value.Type(), f.DefValue, usage))
			})
			return buf.String()
		},
		"getURL": func(cmd *cobra.Command) string {
			return getURL(cmd)
		},
		"split": func(s, sep string) []string {
			return strings.Split(s, sep)
		},
		"hasPrefix": func(s, prefix string) bool {
			return strings.HasPrefix(s, prefix)
		},
	}
	t := template.Must(template.New("cmd.go.tpl").
		Funcs(funcs).
		ParseFiles("./templates/cmd.go.tpl"))
	return t.Execute(w, cmd)
}
func getURL(cmd *cobra.Command) string {
	url := "https://github.com/GGP1/kure/tree/master/docs/commands/"
	split := strings.Split(cmd.CommandPath(), " ")
	for _, s := range split[1:] {
		url += s + "/subcommands/"
	}
	return url
}
func visitFlags(cmd *cobra.Command) string {
	buf := new(bytes.Buffer)
	cmd.Flags().VisitAll(func(f *pflag.Flag) {
		var shorthand string
		if f.Shorthand != "" {
			shorthand = fmt.Sprintf("-%s ", f.Shorthand)
		}
		buf.WriteString(fmt.Sprintf("[%s%s] ", shorthand, f.Name))
	})
	return buf.String()
}