Skip to content

Commit 19b1113

Browse files
committed
Parse additional types.
1 parent 31404f6 commit 19b1113

1 file changed

Lines changed: 66 additions & 26 deletions

File tree

cmd/deviceparameter/deviceparameter.go

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,78 @@ import "github.com/thinnect/go-devparam"
1919

2020
const ApplicationVersionMajor = 0
2121
const ApplicationVersionMinor = 1
22-
const ApplicationVersionPatch = 0
22+
const ApplicationVersionPatch = 1
2323

2424
var ApplicationBuildDate string
2525
var ApplicationBuildDistro string
2626

27-
type HexString []byte
28-
29-
func (self *HexString) UnmarshalFlag(value string) error {
30-
data, err := hex.DecodeString(value)
31-
*self = data
32-
return err
33-
}
34-
35-
func (self HexString) MarshalFlag() (string, error) {
36-
return hex.EncodeToString(self), nil
37-
}
38-
3927
func parseValue(opts Options) ([]byte, error) {
40-
buf := new(bytes.Buffer)
28+
var t interface{}
4129
if opts.Uint8 {
42-
if v, err := strconv.ParseUint(opts.Value, 10, 8); err != nil {
43-
return nil, err
44-
} else if err = binary.Write(buf, binary.BigEndian, uint8(v)); err != nil {
30+
if v, err := strconv.ParseUint(opts.Value, 10, 8); err == nil {
31+
t = uint8(v)
32+
} else {
4533
return nil, err
4634
}
47-
return buf.Bytes(), nil
4835
} else if opts.Uint16 {
49-
if v, err := strconv.ParseUint(opts.Value, 10, 16); err != nil {
50-
return nil, err
51-
} else if err = binary.Write(buf, binary.BigEndian, uint16(v)); err != nil {
36+
if v, err := strconv.ParseUint(opts.Value, 10, 16); err == nil {
37+
t = uint16(v)
38+
} else {
5239
return nil, err
5340
}
5441
} else if opts.Uint32 {
55-
if v, err := strconv.ParseUint(opts.Value, 10, 32); err != nil {
42+
if v, err := strconv.ParseUint(opts.Value, 10, 32); err == nil {
43+
t = uint32(v)
44+
} else {
45+
return nil, err
46+
}
47+
} else if opts.Uint64 {
48+
if v, err := strconv.ParseUint(opts.Value, 10, 64); err == nil {
49+
t = uint64(v)
50+
} else {
51+
return nil, err
52+
}
53+
} else if opts.Int8 {
54+
if v, err := strconv.ParseInt(opts.Value, 10, 8); err == nil {
55+
t = int8(v)
56+
} else {
5657
return nil, err
57-
} else if err = binary.Write(buf, binary.BigEndian, uint32(v)); err != nil {
58+
}
59+
} else if opts.Int16 {
60+
if v, err := strconv.ParseInt(opts.Value, 10, 16); err == nil {
61+
t = int16(v)
62+
} else {
63+
return nil, err
64+
}
65+
} else if opts.Int32 {
66+
if v, err := strconv.ParseInt(opts.Value, 10, 32); err == nil {
67+
t = int32(v)
68+
} else {
69+
return nil, err
70+
}
71+
} else if opts.Int64 {
72+
if v, err := strconv.ParseInt(opts.Value, 10, 64); err == nil {
73+
t = int64(v)
74+
} else {
5875
return nil, err
5976
}
6077
}
78+
79+
if t != nil {
80+
switch t := t.(type) {
81+
case uint8, uint16, uint32, uint64, int8, int16, int32, int64:
82+
buf := new(bytes.Buffer)
83+
if err := binary.Write(buf, binary.BigEndian, t); err != nil {
84+
return nil, err
85+
}
86+
return buf.Bytes(), nil
87+
}
88+
}
89+
90+
if opts.String {
91+
return []byte(opts.Value), nil
92+
}
93+
6194
return hex.DecodeString(opts.Value)
6295
}
6396

@@ -70,9 +103,16 @@ type Options struct {
70103

71104
Parameter string `short:"p" long:"parameter" description:"Name of the parameter"`
72105
Value string `short:"v" long:"value" description:"Value to set"`
73-
Uint8 bool `long:"uint8" description:"Value is uint8"`
74-
Uint16 bool `long:"uint16" description:"Value is uint16"`
75-
Uint32 bool `long:"uint32" description:"Value is uint32"`
106+
107+
String bool `long:"string" description:"Value is string"`
108+
Uint8 bool `long:"uint8" description:"Value is uint8"`
109+
Uint16 bool `long:"uint16" description:"Value is uint16"`
110+
Uint32 bool `long:"uint32" description:"Value is uint32"`
111+
Uint64 bool `long:"uint64" description:"Value is uint64"`
112+
Int8 bool `long:"int8" description:"Value is int8"`
113+
Int16 bool `long:"int16" description:"Value is int16"`
114+
Int32 bool `long:"int32" description:"Value is int32"`
115+
Int64 bool `long:"int64" description:"Value is int64"`
76116

77117
Debug []bool `short:"D" long:"debug" description:"Debug mode, print raw packets"`
78118
ShowVersion func() `short:"V" long:"version" description:"Show application version"`

0 commit comments

Comments
 (0)