-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathflag.go
More file actions
36 lines (32 loc) · 658 Bytes
/
flag.go
File metadata and controls
36 lines (32 loc) · 658 Bytes
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
package tl
import (
"fmt"
"strconv"
"strings"
)
// Flag describes conditional parameter.
type Flag struct {
// Name of the parameter.
Name string `json:"name"`
// Index represent bit index.
Index int `json:"index"`
}
func (f Flag) String() string {
return fmt.Sprintf("%s.%d", f.Name, f.Index)
}
func (f *Flag) Parse(s string) error {
pos := strings.Index(s, ".")
if pos < 1 {
return fmt.Errorf("bad flag %q", s)
}
f.Name = s[:pos]
if !isValidName(f.Name) {
return fmt.Errorf("name %q is invalid", f.Name)
}
idx, err := strconv.Atoi(s[pos+1:])
if err != nil {
return fmt.Errorf("bad index: %w", err)
}
f.Index = idx
return nil
}