"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/cli/handler.go" (28 May 2021, 2272 Bytes) of package /linux/misc/old/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package cli
    2 
    3 import (
    4     "regexp"
    5     "strings"
    6 )
    7 
    8 func NewFlagGroup(name string, flags ...Flag) FlagGroup {
    9     return FlagGroup{
   10         Name:  name,
   11         Flags: flags,
   12     }
   13 }
   14 
   15 type FlagGroup struct {
   16     Name  string
   17     Flags []Flag
   18 }
   19 
   20 type FlagGroups []FlagGroup
   21 
   22 func (groups FlagGroups) getFlags(name string) []Flag {
   23     for _, group := range groups {
   24         if group.Name == name {
   25             return group.Flags
   26         }
   27     }
   28 
   29     return nil
   30 }
   31 
   32 var handlers []*Handler
   33 
   34 type Handler struct {
   35     Pattern     string
   36     FlagGroups  FlagGroups
   37     Callback    func(Context)
   38     Description string
   39 }
   40 
   41 func (self *Handler) getParser() Parser {
   42     var parsers []Parser
   43 
   44     for _, pattern := range self.SplitPattern() {
   45         if isFlagGroup(pattern) {
   46             groupName := flagGroupName(pattern)
   47             flags := self.FlagGroups.getFlags(groupName)
   48             parsers = append(parsers, getFlagParser(flags))
   49         } else if isCaptureGroup(pattern) {
   50             parsers = append(parsers, CaptureGroupParser{pattern})
   51         } else {
   52             parsers = append(parsers, EqualParser{pattern})
   53         }
   54     }
   55 
   56     return CompleteParser{parsers}
   57 }
   58 
   59 // Split on spaces but ignore spaces inside <...> and [...]
   60 func (self *Handler) SplitPattern() []string {
   61     re := regexp.MustCompile(`(<[^>]+>|\[[^\]]+]|\S+)`)
   62     matches := []string{}
   63 
   64     for _, value := range re.FindAllStringSubmatch(self.Pattern, -1) {
   65         matches = append(matches, value[1])
   66     }
   67 
   68     return matches
   69 }
   70 
   71 func SetHandlers(h []*Handler) {
   72     handlers = h
   73 }
   74 
   75 func AddHandler(pattern string, groups FlagGroups, callback func(Context), desc string) {
   76     handlers = append(handlers, &Handler{
   77         Pattern:     pattern,
   78         FlagGroups:  groups,
   79         Callback:    callback,
   80         Description: desc,
   81     })
   82 }
   83 
   84 func findHandler(args []string) *Handler {
   85     for _, h := range handlers {
   86         if _, ok := h.getParser().Match(args); ok {
   87             return h
   88         }
   89     }
   90     return nil
   91 }
   92 
   93 func Handle(args []string) bool {
   94     h := findHandler(args)
   95     if h == nil {
   96         return false
   97     }
   98 
   99     _, data := h.getParser().Capture(args)
  100     ctx := Context{
  101         args:     data,
  102         handlers: handlers,
  103     }
  104     h.Callback(ctx)
  105     return true
  106 }
  107 
  108 func isCaptureGroup(arg string) bool {
  109     return strings.HasPrefix(arg, "<") && strings.HasSuffix(arg, ">")
  110 }
  111 
  112 func isFlagGroup(arg string) bool {
  113     return strings.HasPrefix(arg, "[") && strings.HasSuffix(arg, "]")
  114 }
  115 
  116 func flagGroupName(s string) string {
  117     return s[1 : len(s)-1]
  118 }