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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
package mybase
import (
"fmt"
"os"
"runtime"
"sort"
"strings"
"unicode"
"github.com/mitchellh/go-wordwrap"
terminal "golang.org/x/term"
)
// OptionType is an enum for representing the type of an option.
type OptionType int
// Constants representing different OptionType enumerated values.
// Note that there intentionally aren't separate types for int, comma-separated
// list, regex, etc. From the perspective of the CLI or an option file, these
// are all strings; callers may *process* a string value as a different Golang
// type at runtime using Config.GetInt, Config.GetSlice, etc.
const (
OptionTypeString OptionType = iota // String-valued option
OptionTypeBool // Boolean-valued option
)
// Option represents a flag/setting for a Command. Any Option present for a
// parent Command will automatically be available to all of its descendent
// subcommands, although subcommands may choose to override the exact semantics
// by providing another conflicting Option of same Name.
type Option struct {
Name string
Shorthand rune
Type OptionType
Default string
Description string
RequireValue bool
HiddenOnCLI bool
Group string // Used in help information
}
// StringOption creates a string-type Option. By default, string options require
// a value, though this can be overridden via ValueOptional().
func StringOption(long string, short rune, defaultValue string, description string) *Option {
return &Option{
Name: strings.Replace(long, "_", "-", -1),
Shorthand: short,
Type: OptionTypeString,
Default: defaultValue,
Description: description,
RequireValue: true,
}
}
// BoolOption creates a boolean-type Option. By default, boolean options do not
// require a value, though this can be overridden via ValueRequired().
func BoolOption(long string, short rune, defaultValue bool, description string) *Option {
var defaultAsStr string
if defaultValue {
defaultAsStr = "1"
} else {
defaultAsStr = ""
}
return &Option{
Name: strings.Replace(long, "_", "-", -1),
Shorthand: short,
Type: OptionTypeBool,
Default: defaultAsStr,
Description: description,
RequireValue: false,
}
}
// Hidden prevents an Option from being displayed in a Command's help/usage
// text.
func (opt *Option) Hidden() *Option {
opt.HiddenOnCLI = true
return opt
}
// ValueRequired marks an Option as needing a value, so it will be an error if
// the option is supplied alone without any corresponding value.
func (opt *Option) ValueRequired() *Option {
if opt.Type == OptionTypeBool {
panic(fmt.Errorf("Option %s: boolean options cannot have required value", opt.Name))
}
opt.RequireValue = true
return opt
}
// ValueOptional marks an Option as not needing a value, allowing the Option to
// appear without any value associated.
func (opt *Option) ValueOptional() *Option {
opt.RequireValue = false
return opt
}
// Usage displays one-line help information on the Option.
func (opt *Option) Usage(maxNameLength int) string {
if opt.HiddenOnCLI {
return ""
}
lineLen := 10000
stdinFd := int(os.Stderr.Fd())
if terminal.IsTerminal(stdinFd) {
lineLen, _, _ = terminal.GetSize(stdinFd)
if lineLen < 80 {
lineLen = 80
}
// Avoid extra blank lines on Windows when output matches full line length
if runtime.GOOS == "windows" {
lineLen--
}
}
var shorthand string
if opt.Shorthand > 0 {
shorthand = fmt.Sprintf("-%c,", opt.Shorthand)
}
head := fmt.Sprintf(" %3s --%*s ", shorthand, -1*maxNameLength, opt.usageName())
desc := fmt.Sprintf("%s%s", opt.Description, opt.DefaultUsage())
if len(desc)+len(head) > lineLen {
desc = wordwrap.WrapString(desc, uint(lineLen-len(head)))
spacer := fmt.Sprintf("\n%s", strings.Repeat(" ", len(head)))
desc = strings.Replace(desc, "\n", spacer, -1)
}
return fmt.Sprintf("%s%s\n", head, desc)
}
// DefaultUsage returns usage information relating to the Option's default
// value.
func (opt *Option) DefaultUsage() string {
if opt.HiddenOnCLI || !opt.HasNonzeroDefault() {
return ""
} else if opt.Type == OptionTypeBool {
return fmt.Sprintf(" (enabled by default; disable with --skip-%s)", opt.Name)
}
return fmt.Sprintf(" (default %s)", opt.PrintableDefault())
}
// usageName returns the option's name, potentially modified/annotated for
// display on help screen.
func (opt *Option) usageName() string {
if opt.HiddenOnCLI {
return ""
} else if opt.Type == OptionTypeBool {
if opt.HasNonzeroDefault() {
return fmt.Sprintf("[skip-]%s", opt.Name)
}
return opt.Name
} else if opt.RequireValue {
return fmt.Sprintf("%s value", opt.Name)
}
return fmt.Sprintf("%s[=value]", opt.Name)
}
// HasNonzeroDefault returns true if the Option's default value differs from
// its type's zero/empty value.
func (opt *Option) HasNonzeroDefault() bool {
switch opt.Type {
case OptionTypeString:
return opt.Default != ""
case OptionTypeBool:
return BoolValue(opt.Default)
default:
return false
}
}
// PrintableDefault returns a human-friendly version of the Option's default
// value.
func (opt *Option) PrintableDefault() string {
switch opt.Type {
case OptionTypeBool:
if BoolValue(opt.Default) {
return "true"
}
return "false"
default:
return fmt.Sprintf(`"%s"`, opt.Default)
}
}
// OptionGroup is a group of related Options, used in generation of usage
// instructions for a Command.
type OptionGroup struct {
Name string
Options []*Option
}
func newOptionGroup(group string, options []*Option) *OptionGroup {
grp := &OptionGroup{Name: group}
lookup := make(map[string]*Option, len(options))
names := make([]string, 0, len(options))
for _, opt := range options {
lookup[opt.Name] = opt
names = append(names, opt.Name)
}
sort.Strings(names)
for _, name := range names {
grp.Options = append(grp.Options, lookup[name])
}
return grp
}
// NormalizeOptionToken takes a string of form "foo=bar" or just "foo", and
// parses it into separate key and value. It also returns whether the arg
// included a value (to tell "" vs no-value) and whether it had a "loose-"
// prefix, meaning that the calling parser shouldn't return an error if the key
// does not correspond to any existing option.
func NormalizeOptionToken(arg string) (key, value string, hasValue, loose bool) {
tokens := strings.SplitN(arg, "=", 2)
key = strings.TrimFunc(tokens[0], unicode.IsSpace)
if key == "" {
return
}
key = strings.ToLower(key)
key = strings.Replace(key, "_", "-", -1)
if strings.HasPrefix(key, "loose-") {
key = key[6:]
loose = true
}
var negated bool
if strings.HasPrefix(key, "skip-") {
key = key[5:]
negated = true
} else if strings.HasPrefix(key, "disable-") {
key = key[8:]
negated = true
} else if strings.HasPrefix(key, "enable-") {
key = key[7:]
}
if len(tokens) > 1 {
hasValue = true
value = strings.TrimFunc(tokens[1], unicode.IsSpace)
// negated and value supplied: set to falsey value of "" UNLESS the value is
// also falsey, in which case we have a double-negative, meaning enable
if negated {
if BoolValue(value) {
value = ""
} else {
value = "1"
}
}
} else if negated {
// No value supplied and negated: set to falsey value of ""
value = ""
// But negation still satisfies "having a value" for RequireValue options
hasValue = true
}
return
}
// BoolValue converts the supplied option value string to a boolean.
// The case-insensitive values "", "off", "false", and "0" are considered false;
// all other values are considered true.
func BoolValue(input string) bool {
switch strings.ToLower(input) {
case "", "off", "false", "0":
return false
default:
return true
}
}
// NormalizeOptionName is a convenience function that only returns the "key"
// portion of NormalizeOptionToken.
func NormalizeOptionName(name string) string {
ret, _, _, _ := NormalizeOptionToken(name)
return ret
}
// OptionNotDefinedError is an error returned when an unknown Option is used.
type OptionNotDefinedError struct {
Name string
Source string
}
// Error satisfies golang's error interface.
func (ond OptionNotDefinedError) Error() string {
var source string
if ond.Source != "" {
source = fmt.Sprintf("%s: ", ond.Source)
}
return fmt.Sprintf("%sUnknown option \"%s\"", source, ond.Name)
}
// OptionMissingValueError is an error returned when an Option requires a value,
// but no value was supplied.
type OptionMissingValueError struct {
Name string
Source string
}
// Error satisfies golang's error interface.
func (omv OptionMissingValueError) Error() string {
var source string
if omv.Source != "" {
source = fmt.Sprintf("%s: ", omv.Source)
}
return fmt.Sprintf("%sMissing required value for option %s", source, omv.Name)
}
|