/
option_stringifier.go
120 lines (96 loc) · 3.92 KB
/
option_stringifier.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt
package getopt
import (
"fmt"
"strings"
)
func valueIze(originalValue string) (value string) {
value = "<" + strings.ToLower(originalValue) + ">"
return
}
func (option Option) HelpText(longOptLength int) (output string) {
fmtStringLongAndShort := fmt.Sprintf(" -%%-1s, --%%-%ds %%s", longOptLength) // "p", "port=PORT", "the port that should be used"
fmtStringShort := fmt.Sprintf(" -%%-1s%%-%ds %%s", longOptLength) // "p", "PORT", "the port that should be used"
fmtStringLong := fmt.Sprintf(" --%%-%ds %%s", longOptLength) // "port=PORT", "the port that should be used"
fmtArgument := fmt.Sprintf(" %%-%ds %%s", longOptLength) // "port=PORT", "the port that should be used"
if option.Description != "" {
switch {
case option.Flags&(IsArg|IsPassThrough) > 0:
output = fmt.Sprintf(fmtArgument, valueIze(option.Key()), option.DescriptionText())
case option.HasLongOpt() && option.HasShortOpt():
output = fmt.Sprintf(fmtStringLongAndShort, option.ShortOptString(), option.LongOptString(), option.DescriptionText())
case !option.HasLongOpt() && option.HasShortOpt() && option.Flags&Flag > 0:
output = fmt.Sprintf(fmtStringShort, option.ShortOptString(), "", option.DescriptionText())
case option.HasShortOpt():
output = fmt.Sprintf(fmtStringShort, option.ShortOptString(), valueIze(option.Key()), option.DescriptionText())
case option.HasLongOpt():
output = fmt.Sprintf(fmtStringLong, option.LongOptString(), option.DescriptionText())
}
}
return output
}
func (option Option) LongOptString() (longOptString string) {
if option.HasLongOpt() {
longOptString = option.LongOpt()
if option.Flags&(Flag|Usage|Help|IsPassThrough) == 0 {
longOptString = longOptString + "=" + valueIze(option.LongOpt())
}
}
return
}
func (option Option) ShortOptString() (shortOptString string) {
if option.HasShortOpt() {
shortOptString = option.ShortOpt()
if !option.HasLongOpt() && option.Flags&(Flag|Usage|Help) == 0 {
shortOptString = shortOptString + " " + option.LongOpt()
}
if option.Flags&(Flag|NoLongOpt) == Flag|NoLongOpt {
shortOptString = shortOptString + " "
}
}
return
}
func (option Option) Usage() (usageString string) {
switch {
case option.Flags&(IsArg|IsPassThrough|IsSubCommand) > 0:
usageString = valueIze(option.Key())
case option.HasShortOpt():
usageString = "-" + option.ShortOpt()
default:
usageString = "--" + option.LongOpt()
}
if option.Flags&(Flag|IsArg|IsSubCommand|IsPassThrough|Usage|Help) == 0 {
var separator string
if option.HasShortOpt() {
separator = " "
} else {
separator = "="
}
usageString = usageString + separator + valueIze(option.Key())
}
if option.Flags&Optional > 0 || option.Flags&Help > 0 || option.Flags&Usage > 0 || option.Flags&ExampleIsDefault > 0 {
usageString = "[" + usageString + "]"
}
return
}
func (option Option) DescriptionText() (description string) {
description = option.Description
defaultValue := fmt.Sprintf("%v", option.DefaultValue)
// %v for arrays prints something like [3 4 5] ... let's transform that to 3,4,5:
defaultValue = strings.Replace(strings.Replace(strings.Replace(defaultValue, "[", "", -1), "]", "", -1), " ", ",", -1)
if defaultValue != "" && option.DefaultValue != nil {
switch {
case option.Flags&(Optional|IsConfigFile) > 0 && option.Flags&ExampleIsDefault > 0:
description = description + " (default: " + defaultValue + ")"
case option.Flags&Required > 0 || option.Flags&Optional > 0 || option.Flags&IsArg > 0:
description = description + " (e.g. " + defaultValue + ")"
}
}
if option.HasEnvVar() && option.Flags&NoEnvHelp == 0 {
description = description + "; setable via $" + option.EnvVar()
}
return
}