| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "runtime" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | func prefixFor(name string) (prefix string) { |
| 11 | if len(name) == 1 { |
| 12 | prefix = "-" |
| 13 | } else { |
| 14 | prefix = "--" |
| 15 | } |
| 16 | |
| 17 | return prefix |
| 18 | } |
| 19 | |
| 20 | // Returns the placeholder, if any, and the unquoted usage string. |
| 21 | func unquoteUsage(usage string) (string, string) { |
| 22 | for i := 0; i < len(usage); i++ { |
| 23 | if usage[i] == '`' { |
| 24 | for j := i + 1; j < len(usage); j++ { |
| 25 | if usage[j] == '`' { |
| 26 | name := usage[i+1 : j] |
| 27 | usage = usage[:i] + name + usage[j+1:] |
| 28 | return name, usage |
| 29 | } |
| 30 | } |
| 31 | break |
| 32 | } |
| 33 | } |
| 34 | return "", usage |
| 35 | } |
| 36 | |
| 37 | func prefixedNames(names []string, placeholder string) string { |
| 38 | var prefixed strings.Builder |
| 39 | for i, name := range names { |
| 40 | if name == "" { |
| 41 | continue |
| 42 | } |
| 43 | |
| 44 | prefixed.WriteString(prefixFor(name) + name) |
| 45 | if placeholder != "" { |
| 46 | prefixed.WriteString(" " + placeholder) |
| 47 | } |
| 48 | if i < len(names)-1 { |
| 49 | prefixed.WriteString(", ") |
| 50 | } |
| 51 | } |
| 52 | return prefixed.String() |
| 53 | } |
| 54 | |
| 55 | func envFormat(envVars []string, prefix, sep, suffix string) string { |
| 56 | if len(envVars) > 0 { |
| 57 | return fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(envVars, sep), suffix) |
| 58 | } |
| 59 | return "" |
| 60 | } |
| 61 | |
| 62 | func defaultEnvFormat(envVars []string) string { |
| 63 | return envFormat(envVars, "$", ", $", "") |
| 64 | } |
| 65 | |
| 66 | func withEnvHint(envVars []string, str string) string { |
| 67 | envText := "" |
| 68 | if runtime.GOOS != "windows" || os.Getenv("PSHOME") != "" { |
| 69 | envText = defaultEnvFormat(envVars) |
| 70 | } else { |
| 71 | envText = envFormat(envVars, "%", "%, %", "%") |
| 72 | } |
| 73 | return str + envText |
| 74 | } |
| 75 | |
| 76 | func withFileHint(filePath, str string) string { |
| 77 | fileText := "" |
| 78 | if filePath != "" { |
| 79 | fileText = fmt.Sprintf(" [%s]", filePath) |
| 80 | } |
| 81 | return str + fileText |
| 82 | } |
| 83 | |
| 84 | func formatDefault(format string) string { |
| 85 | return " (default: " + format + ")" |
| 86 | } |
| 87 | |
| 88 | func stringifyFlag(f Flag) string { |
| 89 | // enforce DocGeneration interface on flags to avoid reflection |
| 90 | df, ok := f.(DocGenerationFlag) |
| 91 | if !ok { |
| 92 | return "" |
| 93 | } |
| 94 | placeholder, usage := unquoteUsage(df.GetUsage()) |
| 95 | needsPlaceholder := df.TakesValue() |
| 96 | // if needsPlaceholder is true, placeholder is empty |
| 97 | if needsPlaceholder && placeholder == "" { |
| 98 | // try to get type from flag |
| 99 | if tname := df.TypeName(); tname != "" { |
| 100 | placeholder = tname |
| 101 | } else { |
| 102 | placeholder = defaultPlaceholder |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | defaultValueString := "" |
| 107 | |
| 108 | // don't print default text for required flags |
| 109 | if rf, ok := f.(RequiredFlag); !ok || !rf.IsRequired() { |
| 110 | if df.IsDefaultVisible() { |
| 111 | if s := df.GetDefaultText(); s != "" { |
| 112 | defaultValueString = fmt.Sprintf(formatDefault("%s"), s) |
| 113 | } else if df.TakesValue() && df.GetValue() != "" { |
| 114 | defaultValueString = fmt.Sprintf(formatDefault("%s"), df.GetValue()) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | usageWithDefault := strings.TrimSpace(usage + defaultValueString) |
| 120 | |
| 121 | pn := prefixedNames(f.Names(), placeholder) |
| 122 | sliceFlag, ok := f.(DocGenerationMultiValueFlag) |
| 123 | if ok && sliceFlag.IsMultiValueFlag() { |
| 124 | pn = pn + " [ " + pn + " ]" |
| 125 | } |
| 126 | |
| 127 | return withEnvHint(df.GetEnvVars(), fmt.Sprintf("%s\t%s", pn, usageWithDefault)) |
| 128 | } |