-
Notifications
You must be signed in to change notification settings - Fork 11
/
prop_piece.go
63 lines (54 loc) · 1.63 KB
/
prop_piece.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
////////////////////////////////////////////////////////////////////////////
// Program: cascadia
// Purpose: go cascadia CSS selection from command line
// Authors: Tong Sun (c) 2016-2023, All rights reserved
////////////////////////////////////////////////////////////////////////////
package main
import (
"errors"
"regexp"
)
type PieceStyle int
const (
PieceStyleTEXT PieceStyle = iota
PieceStyleRAW
PieceStyleATTR
)
type PieceStyleMap struct {
Keys []string
Values map[string]string
PieceStyles map[string]PieceStyle
}
var pieceStyles = map[string]PieceStyle{
"RAW": PieceStyleRAW,
"ATTR": PieceStyleATTR,
}
//==========================================================================
// cli parameter handling
// DecodeSlice implements cli.SliceDecoder
// NOTE: if SliceDecoder not implemented, the Decode method would be only invoked once
func (PieceStyleMap) DecodeSlice() {}
// Decode implements cli.Decoder interface
func (m *PieceStyleMap) Decode(s string) error {
if (m.Values) == nil {
m.Values = make(map[string]string)
m.PieceStyles = make(map[string]PieceStyle)
}
matches := regexp.MustCompile("(.*)=((.*?):)?(.*)").FindStringSubmatch(s)
if len(matches) < 4 {
return errors.New("format error. To get help, run: " + progname)
}
key := matches[1]
ptp := matches[3] // piece type
val := matches[4]
style := PieceStyleTEXT
style, ok := pieceStyles[ptp]
//fmt.Println("]", key, ptp, style, ok, val)
if len(ptp) != 0 && !ok {
return errors.New("Piece style specification error. To get help, run: " + progname)
}
m.Keys = append(m.Keys, key)
m.PieceStyles[key] = style
m.Values[key] = val
return nil
}