-
Notifications
You must be signed in to change notification settings - Fork 26
/
flag.go
44 lines (36 loc) · 918 Bytes
/
flag.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
package macaddr
import (
"encoding"
"flag"
"net"
)
// Flag is a wrapper of net.HardwareAddr compatible with flag and json packages.
type Flag struct {
net.HardwareAddr
}
var (
_ flag.Getter = (*Flag)(nil)
_ encoding.TextMarshaler = Flag{}
_ encoding.TextUnmarshaler = (*Flag)(nil)
)
// Empty returns true if the HardwareAddr is unset.
func (f Flag) Empty() bool {
return len(f.HardwareAddr) == 0
}
// Get implements flag.Getter.
func (f *Flag) Get() any {
return f.HardwareAddr
}
// Set implements flag.Value.
func (f *Flag) Set(s string) (e error) {
f.HardwareAddr, e = net.ParseMAC(s)
return
}
// MarshalText implements encoding.TextMarshaler.
func (f Flag) MarshalText() (text []byte, e error) {
return []byte(f.HardwareAddr.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (f *Flag) UnmarshalText(text []byte) (e error) {
return f.Set(string(text))
}