| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package flags |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"bytes" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"reflect" |
| 13 |
"strings" |
| 14 |
"unicode/utf8" |
| 15 |
) |
| 16 |
|
| 17 |
type alignmentInfo struct { |
| 18 |
maxLongLen int |
| 19 |
hasShort bool |
| 20 |
hasValueName bool |
| 21 |
terminalColumns int |
| 22 |
} |
| 23 |
|
| 24 |
func (p *Parser) getAlignmentInfo() alignmentInfo { |
| 25 |
ret := alignmentInfo{ |
| 26 |
maxLongLen: 0, |
| 27 |
hasShort: false, |
| 28 |
hasValueName: false, |
| 29 |
terminalColumns: getTerminalColumns(), |
| 30 |
} |
| 31 |
|
| 32 |
if ret.terminalColumns <= 0 { |
| 33 |
ret.terminalColumns = 80 |
| 34 |
} |
| 35 |
|
| 36 |
alfunc := func(index int, grp *Group) { |
| 37 |
for _, info := range grp.Options { |
| 38 |
if info.ShortName != 0 { |
| 39 |
ret.hasShort = true |
| 40 |
} |
| 41 |
|
| 42 |
lv := utf8.RuneCountInString(info.ValueName) |
| 43 |
|
| 44 |
if lv != 0 { |
| 45 |
ret.hasValueName = true |
| 46 |
} |
| 47 |
|
| 48 |
l := utf8.RuneCountInString(info.LongName) + lv |
| 49 |
|
| 50 |
if l > ret.maxLongLen { |
| 51 |
ret.maxLongLen = l |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if p.currentCommand != nil { |
| 57 |
|
| 58 |
|
| 59 |
p.eachTopLevelGroup(alfunc) |
| 60 |
} |
| 61 |
|
| 62 |
p.EachGroup(alfunc) |
| 63 |
|
| 64 |
return ret |
| 65 |
} |
| 66 |
|
| 67 |
func (p *Parser) writeHelpOption(writer *bufio.Writer, option *Option, info alignmentInfo) { |
| 68 |
line := &bytes.Buffer{} |
| 69 |
|
| 70 |
distanceBetweenOptionAndDescription := 2 |
| 71 |
paddingBeforeOption := 2 |
| 72 |
|
| 73 |
line.WriteString(strings.Repeat(" ", paddingBeforeOption)) |
| 74 |
|
| 75 |
if option.ShortName != 0 { |
| 76 |
line.WriteRune(defaultShortOptDelimiter) |
| 77 |
line.WriteRune(option.ShortName) |
| 78 |
} else if info.hasShort { |
| 79 |
line.WriteString(" ") |
| 80 |
} |
| 81 |
|
| 82 |
descstart := info.maxLongLen + paddingBeforeOption + distanceBetweenOptionAndDescription |
| 83 |
|
| 84 |
if info.hasShort { |
| 85 |
descstart += 2 |
| 86 |
} |
| 87 |
|
| 88 |
if info.maxLongLen > 0 { |
| 89 |
descstart += 4 |
| 90 |
} |
| 91 |
|
| 92 |
if info.hasValueName { |
| 93 |
descstart += 3 |
| 94 |
} |
| 95 |
|
| 96 |
if len(option.LongName) > 0 { |
| 97 |
if option.ShortName != 0 { |
| 98 |
line.WriteString(", ") |
| 99 |
} else if info.hasShort { |
| 100 |
line.WriteString(" ") |
| 101 |
} |
| 102 |
|
| 103 |
line.WriteString(defaultLongOptDelimiter) |
| 104 |
line.WriteString(option.LongName) |
| 105 |
} |
| 106 |
|
| 107 |
if !option.isBool() { |
| 108 |
line.WriteRune(defaultNameArgDelimiter) |
| 109 |
|
| 110 |
if len(option.ValueName) > 0 { |
| 111 |
line.WriteString(option.ValueName) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
written := line.Len() |
| 116 |
line.WriteTo(writer) |
| 117 |
|
| 118 |
if option.Description != "" { |
| 119 |
dw := descstart - written |
| 120 |
writer.WriteString(strings.Repeat(" ", dw)) |
| 121 |
|
| 122 |
def := "" |
| 123 |
defs := option.Default |
| 124 |
|
| 125 |
if len(option.defaultMask) != 0 { |
| 126 |
if option.defaultMask != "-" { |
| 127 |
def = option.defaultMask |
| 128 |
} |
| 129 |
} else if len(defs) == 0 && !option.isBool() { |
| 130 |
var showdef bool |
| 131 |
|
| 132 |
switch option.Field.Type.Kind() { |
| 133 |
case reflect.Func, reflect.Ptr: |
| 134 |
showdef = !option.Value.IsNil() |
| 135 |
case reflect.Slice, reflect.String, reflect.Array: |
| 136 |
showdef = option.Value.Len() > 0 |
| 137 |
case reflect.Map: |
| 138 |
showdef = !option.Value.IsNil() && option.Value.Len() > 0 |
| 139 |
default: |
| 140 |
zeroval := reflect.Zero(option.Field.Type) |
| 141 |
showdef = !reflect.DeepEqual(zeroval.Interface(), option.Value.Interface()) |
| 142 |
} |
| 143 |
|
| 144 |
if showdef { |
| 145 |
def, _ = convertToString(option.Value, option.tag) |
| 146 |
} |
| 147 |
} else if len(defs) != 0 { |
| 148 |
def = strings.Join(defs, ", ") |
| 149 |
} |
| 150 |
|
| 151 |
var desc string |
| 152 |
|
| 153 |
if def != "" { |
| 154 |
desc = fmt.Sprintf("%s (%v)", option.Description, def) |
| 155 |
} else { |
| 156 |
desc = option.Description |
| 157 |
} |
| 158 |
|
| 159 |
writer.WriteString(wrapText(desc, |
| 160 |
info.terminalColumns-descstart, |
| 161 |
strings.Repeat(" ", descstart))) |
| 162 |
} |
| 163 |
|
| 164 |
writer.WriteString("\n") |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
func (p *Parser) WriteHelp(writer io.Writer) { |
| 173 |
if writer == nil { |
| 174 |
return |
| 175 |
} |
| 176 |
|
| 177 |
wr := bufio.NewWriter(writer) |
| 178 |
aligninfo := p.getAlignmentInfo() |
| 179 |
|
| 180 |
if p.ApplicationName != "" { |
| 181 |
wr.WriteString("Usage:\n") |
| 182 |
fmt.Fprintf(wr, " %s", p.ApplicationName) |
| 183 |
|
| 184 |
if p.Usage != "" { |
| 185 |
fmt.Fprintf(wr, " %s", p.Usage) |
| 186 |
} |
| 187 |
|
| 188 |
if len(p.currentCommandString) > 0 { |
| 189 |
cmdusage := fmt.Sprintf("[%s-OPTIONS]", p.currentCommandString[len(p.currentCommandString)-1]) |
| 190 |
|
| 191 |
if p.currentCommand != nil { |
| 192 |
if us, ok := p.currentCommand.data.(Usage); ok { |
| 193 |
cmdusage = us.Usage() |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
fmt.Fprintf(wr, " %s %s", |
| 198 |
strings.Join(p.currentCommandString, " "), |
| 199 |
cmdusage) |
| 200 |
} |
| 201 |
|
| 202 |
fmt.Fprintln(wr) |
| 203 |
|
| 204 |
if p.currentCommand != nil && len(p.currentCommand.LongDescription) != 0 { |
| 205 |
fmt.Fprintln(wr) |
| 206 |
|
| 207 |
t := wrapText(p.currentCommand.LongDescription, |
| 208 |
aligninfo.terminalColumns, |
| 209 |
"") |
| 210 |
|
| 211 |
fmt.Fprintln(wr, t) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
seen := make(map[*Group]bool) |
| 216 |
|
| 217 |
writeHelp := func(index int, grp *Group) { |
| 218 |
if len(grp.Options) == 0 || seen[grp] { |
| 219 |
return |
| 220 |
} |
| 221 |
|
| 222 |
seen[grp] = true |
| 223 |
|
| 224 |
wr.WriteString("\n") |
| 225 |
|
| 226 |
fmt.Fprintf(wr, "%s:\n", grp.Name) |
| 227 |
|
| 228 |
for _, info := range grp.Options { |
| 229 |
p.writeHelpOption(wr, info, aligninfo) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
if p.currentCommand != nil { |
| 235 |
p.eachTopLevelGroup(writeHelp) |
| 236 |
} |
| 237 |
|
| 238 |
p.EachGroup(writeHelp) |
| 239 |
|
| 240 |
commander := p.currentCommander() |
| 241 |
names := commander.sortedNames() |
| 242 |
|
| 243 |
if len(names) > 0 { |
| 244 |
maxnamelen := len(names[0]) |
| 245 |
|
| 246 |
for i := 1; i < len(names); i++ { |
| 247 |
l := len(names[i]) |
| 248 |
|
| 249 |
if l > maxnamelen { |
| 250 |
maxnamelen = l |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
fmt.Fprintln(wr) |
| 255 |
fmt.Fprintln(wr, "Available commands:") |
| 256 |
|
| 257 |
for _, name := range names { |
| 258 |
fmt.Fprintf(wr, " %s", name) |
| 259 |
|
| 260 |
cmd := commander.Commands[name] |
| 261 |
|
| 262 |
if len(cmd.Name) > 0 { |
| 263 |
pad := strings.Repeat(" ", maxnamelen-len(name)) |
| 264 |
fmt.Fprintf(wr, "%s %s", pad, cmd.Name) |
| 265 |
} |
| 266 |
|
| 267 |
fmt.Fprintln(wr) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
wr.Flush() |
| 272 |
} |