forked from jimmyfrasche/autoreadme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
defflag.go
44 lines (37 loc) · 878 Bytes
/
defflag.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
// Copyright 2021 John Papandriopoulos. All rights reserved.
// Use of this code is governed by a BSD-License found in the LICENSE file.
package main
import (
"errors"
"strings"
"unicode"
)
type def struct {
Name string
Value string
}
// UpperClone returns a clone of the receiver, with the first character of the
// name in upper case.
func (d def) UpperClone() def {
rs := []rune(d.Name)
return def{
Name: string(unicode.ToUpper(rs[0])) + string(rs[1:]),
Value: d.Value,
}
}
type defFlag []def
func (df *defFlag) String() string {
return "" // Not used
}
func (df *defFlag) Set(value string) error {
// Make sure it has the form $name=$value
i := strings.Index(value, "=")
if i <= 0 {
return errors.New("invalid def flag: missing equals token")
}
*df = append(*df, def{
Name: strings.ToLower(value[:i]),
Value: value[i+1:],
})
return nil
}