-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
88 lines (79 loc) · 3.24 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"strings"
)
func help() {
firstLine := `Usage: git-exec [options ...] [key=value ...] <command> [args ...]`
examples := `Examples:
* Specify environment variables.
git-exec FOO=fooooo make args1 args2
* Use shell to work with redirect operator.
git-exec /bin/bash -c 'echo "foo" >> README.md'
`
indent := " "
optionItems := make([]string, len(optionTypes))
maxLongNameLength := 0
for _, opt := range optionTypes {
if maxLongNameLength < len(opt.LongName) {
maxLongNameLength = len(opt.LongName)
}
}
envVarItems := []string{}
longNameFormat := "%-" + fmt.Sprintf("%ds", maxLongNameLength)
for i, opt := range optionTypes {
var item string
if opt.ShortName == "" {
item = fmt.Sprintf("%s "+longNameFormat, indent, opt.LongName)
} else {
item = fmt.Sprintf("%s%s, "+longNameFormat, indent, opt.ShortName, opt.LongName)
}
item += " " + optionMessageMap[opt.LongName]
if defaultGetter, ok := defaultValueGetterMap[opt.LongName]; ok {
item += fmt.Sprintf(" (default: %s)", defaultGetter())
}
optionItems[i] = item
if !opt.WithoutEnv {
envVarItems = append(envVarItems, fmt.Sprintf(longNameFormat+" %s", opt.LongName, opt.envKey()))
}
}
options := "Options:\n" + strings.Join(optionItems, "\n")
envVars := "Environment variable mapping:\n" + strings.Join(envVarItems, "\n")
// git-exec は <command>よりも前に 複数のキーと値の組み合わせを指定可能で、
// <command> 以後は 複数の引数を指定可能です。
fmt.Println(firstLine + "\n\n" + options + "\n\n" + envVars + "\n\n" + examples)
}
var optionMessageMap = map[string]string{
"--help": "Show this message.",
"--version": "Show version.",
"--directory": "Specify the directory where the command is executed.",
"--emoji": "Specify the emoji used in commit message.",
"--prompt": "Specify the prompt used in commit message.",
"--template": "Specify the template to build commit message.",
"--skip-guard": "Skip the guard check for uncommitted changes and untracked files before executing command.",
"--skip-guard-uncommitted-changes": "Skip the guard check for uncommitted changes before executing command.",
"--skip-guard-untracked-files": "Skip the guard check for untracked files before executing command.",
}
var defaultValueGetterMap = func() map[string]func() string {
boolToString := func(b bool) string {
if b {
return "true"
} else {
return "false"
}
}
quote := func(s string) string {
return fmt.Sprintf("%q", s)
}
o := defaultOptions
return map[string]func() string{
"--directory": func() string { return quote(o.Directory) },
"--emoji": func() string { return quote(o.Emoji) },
"--prompt": func() string { return quote(o.Prompt) },
"--template": func() string { return quote(o.Template) },
// skip guard
"--skip-guard": func() string { return boolToString(o.SkipGuard) },
"--skip-guard-uncommitted-changes": func() string { return boolToString(o.SkipGuardUncommittedChanges) },
"--skip-guard-untracked-files": func() string { return boolToString(o.SkipGuardUntrackedFiles) },
}
}()