github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/cmd/noms/util/help.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 // This is the Help facility used by the noms utility. It is packaged in a separate util can be used by other programs as well. 23 package util 24 25 import ( 26 "bufio" 27 "fmt" 28 "io" 29 "os" 30 "strings" 31 "text/template" 32 ) 33 34 var usageTemplate = `{{.UsageLine}} 35 36 Usage: 37 38 {{.ProgName}} command [arguments] 39 40 The commands are: 41 {{range .Commands}} 42 {{.Name | printf "%-11s"}} {{.Short}}{{end}} 43 44 Use "{{.ProgName}} help [command]" for more information about a command. 45 46 ` 47 48 var helpTemplate = `usage: {{.ProgName}} {{.Cmd.UsageLine}} 49 50 {{.Cmd.Long | trim}} 51 ` 52 53 var ( 54 commands = []*Command{} 55 usageLine = "" 56 progName = "" 57 ) 58 59 func InitHelp(name string, cmds []*Command, usage string) { 60 progName = name 61 commands = cmds 62 usageLine = usage 63 } 64 65 // tmpl executes the given template text on data, writing the result to w. 66 func tmpl(w io.Writer, text string, data interface{}) { 67 t := template.New("top") 68 t.Funcs(template.FuncMap{"trim": strings.TrimSpace}) 69 template.Must(t.Parse(text)) 70 if err := t.Execute(w, data); err != nil { 71 panic(err) 72 } 73 } 74 75 func printUsage(w io.Writer) { 76 bw := bufio.NewWriter(w) 77 data := struct { 78 ProgName string 79 Commands []*Command 80 UsageLine string 81 }{ 82 progName, 83 commands, 84 usageLine, 85 } 86 tmpl(bw, usageTemplate, data) 87 bw.Flush() 88 } 89 90 func Usage() { 91 printUsage(os.Stderr) 92 os.Exit(1) 93 } 94 95 // help implements the 'help' command. 96 func Help(args []string) { 97 if len(args) == 0 { 98 printUsage(os.Stdout) 99 // not exit 2: succeeded at 'help'. 100 return 101 } 102 if len(args) != 1 { 103 fmt.Fprintf(os.Stderr, "usage: %s help command\n\nToo many arguments given.\n", progName) 104 os.Exit(1) // failed at 'help' 105 } 106 107 arg := args[0] 108 109 for _, cmd := range commands { 110 if cmd.Name() == arg { 111 data := struct { 112 ProgName string 113 Cmd *Command 114 }{ 115 progName, 116 cmd, 117 } 118 tmpl(os.Stdout, helpTemplate, data) 119 flags := cmd.Flags() 120 if countFlags(flags) > 0 { 121 fmt.Fprintf(os.Stdout, "\noptions:\n") 122 flags.PrintDefaults() 123 } 124 // not exit 2: succeeded at 'help cmd'. 125 return 126 } 127 } 128 129 fmt.Fprintf(os.Stderr, "Unknown help topic %#q\n", arg) 130 Usage() // failed at 'help cmd' 131 }