github.com/suntong/cascadia@v1.3.0/prop_piece.go (about)

     1  ////////////////////////////////////////////////////////////////////////////
     2  // Program: cascadia
     3  // Purpose: go cascadia CSS selection from command line
     4  // Authors: Tong Sun (c) 2016-2023, All rights reserved
     5  ////////////////////////////////////////////////////////////////////////////
     6  
     7  package main
     8  
     9  import (
    10  	"errors"
    11  	"regexp"
    12  )
    13  
    14  type PieceStyle int
    15  
    16  const (
    17  	PieceStyleTEXT PieceStyle = iota
    18  	PieceStyleRAW
    19  	PieceStyleATTR
    20  )
    21  
    22  type PieceStyleMap struct {
    23  	Keys        []string
    24  	Values      map[string]string
    25  	PieceStyles map[string]PieceStyle
    26  }
    27  
    28  var pieceStyles = map[string]PieceStyle{
    29  	"RAW":  PieceStyleRAW,
    30  	"ATTR": PieceStyleATTR,
    31  }
    32  
    33  //==========================================================================
    34  // cli parameter handling
    35  
    36  // DecodeSlice implements cli.SliceDecoder
    37  // NOTE: if SliceDecoder not implemented, the Decode method would be only invoked once
    38  func (PieceStyleMap) DecodeSlice() {}
    39  
    40  // Decode implements cli.Decoder interface
    41  func (m *PieceStyleMap) Decode(s string) error {
    42  	if (m.Values) == nil {
    43  		m.Values = make(map[string]string)
    44  		m.PieceStyles = make(map[string]PieceStyle)
    45  	}
    46  	matches := regexp.MustCompile("(.*)=((.*?):)?(.*)").FindStringSubmatch(s)
    47  	if len(matches) < 4 {
    48  		return errors.New("format error. To get help, run: " + progname)
    49  	}
    50  	key := matches[1]
    51  	ptp := matches[3] // piece type
    52  	val := matches[4]
    53  	style := PieceStyleTEXT
    54  	style, ok := pieceStyles[ptp]
    55  	//fmt.Println("]", key, ptp, style, ok, val)
    56  	if len(ptp) != 0 && !ok {
    57  		return errors.New("Piece style specification error. To get help, run: " + progname)
    58  	}
    59  	m.Keys = append(m.Keys, key)
    60  	m.PieceStyles[key] = style
    61  	m.Values[key] = val
    62  	return nil
    63  }