"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/cli/parser.go" (28 May 2021, 8246 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     "fmt"
    5     "strconv"
    6 )
    7 
    8 type Parser interface {
    9     Match([]string) ([]string, bool)
   10     Capture([]string) ([]string, map[string]interface{})
   11 }
   12 
   13 type EqualParser struct {
   14     value string
   15 }
   16 
   17 func (self EqualParser) Match(values []string) ([]string, bool) {
   18     if len(values) == 0 {
   19         return values, false
   20     }
   21 
   22     if self.value == values[0] {
   23         return values[1:], true
   24     }
   25 
   26     return values, false
   27 }
   28 
   29 func (self EqualParser) Capture(values []string) ([]string, map[string]interface{}) {
   30     remainingValues, _ := self.Match(values)
   31     return remainingValues, nil
   32 }
   33 
   34 func (self EqualParser) String() string {
   35     return fmt.Sprintf("EqualParser '%s'", self.value)
   36 }
   37 
   38 type CaptureGroupParser struct {
   39     value string
   40 }
   41 
   42 func (self CaptureGroupParser) Match(values []string) ([]string, bool) {
   43     if len(values) == 0 {
   44         return values, false
   45     }
   46 
   47     return values[1:], true
   48 }
   49 
   50 func (self CaptureGroupParser) key() string {
   51     return self.value[1 : len(self.value)-1]
   52 }
   53 
   54 func (self CaptureGroupParser) Capture(values []string) ([]string, map[string]interface{}) {
   55     if remainingValues, ok := self.Match(values); ok {
   56         return remainingValues, map[string]interface{}{self.key(): values[0]}
   57     }
   58 
   59     return values, nil
   60 }
   61 
   62 func (self CaptureGroupParser) String() string {
   63     return fmt.Sprintf("CaptureGroupParser '%s'", self.value)
   64 }
   65 
   66 type BoolFlagParser struct {
   67     pattern      string
   68     key          string
   69     omitValue    bool
   70     defaultValue bool
   71 }
   72 
   73 func (self BoolFlagParser) Match(values []string) ([]string, bool) {
   74     if self.omitValue {
   75         return flagKeyMatch(self.pattern, values, 0)
   76     }
   77 
   78     remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
   79     if !ok {
   80         return remaining, false
   81     }
   82 
   83     // Check that value is a valid boolean
   84     if _, err := strconv.ParseBool(value); err != nil {
   85         return remaining, false
   86     }
   87 
   88     return remaining, true
   89 }
   90 
   91 func (self BoolFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
   92     if self.omitValue {
   93         remaining, ok := flagKeyMatch(self.pattern, values, 0)
   94         return remaining, map[string]interface{}{self.key: ok}
   95     }
   96 
   97     remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
   98     if !ok {
   99         return remaining, map[string]interface{}{self.key: self.defaultValue}
  100     }
  101 
  102     b, _ := strconv.ParseBool(value)
  103     return remaining, map[string]interface{}{self.key: b}
  104 }
  105 
  106 func (self BoolFlagParser) String() string {
  107     return fmt.Sprintf("BoolFlagParser '%s'", self.pattern)
  108 }
  109 
  110 type StringFlagParser struct {
  111     pattern      string
  112     key          string
  113     defaultValue string
  114 }
  115 
  116 func (self StringFlagParser) Match(values []string) ([]string, bool) {
  117     remaining, _, ok := flagKeyValueMatch(self.pattern, values, 0)
  118     return remaining, ok
  119 }
  120 
  121 func (self StringFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
  122     remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
  123     if !ok {
  124         return remaining, map[string]interface{}{self.key: self.defaultValue}
  125     }
  126 
  127     return remaining, map[string]interface{}{self.key: value}
  128 }
  129 
  130 func (self StringFlagParser) String() string {
  131     return fmt.Sprintf("StringFlagParser '%s'", self.pattern)
  132 }
  133 
  134 type IntFlagParser struct {
  135     pattern      string
  136     key          string
  137     defaultValue int64
  138 }
  139 
  140 func (self IntFlagParser) Match(values []string) ([]string, bool) {
  141     remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
  142     if !ok {
  143         return remaining, false
  144     }
  145 
  146     // Check that value is a valid integer
  147     if _, err := strconv.ParseInt(value, 10, 64); err != nil {
  148         return remaining, false
  149     }
  150 
  151     return remaining, true
  152 }
  153 
  154 func (self IntFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
  155     remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
  156     if !ok {
  157         return remaining, map[string]interface{}{self.key: self.defaultValue}
  158     }
  159 
  160     n, _ := strconv.ParseInt(value, 10, 64)
  161     return remaining, map[string]interface{}{self.key: n}
  162 }
  163 
  164 func (self IntFlagParser) String() string {
  165     return fmt.Sprintf("IntFlagParser '%s'", self.pattern)
  166 }
  167 
  168 type StringSliceFlagParser struct {
  169     pattern      string
  170     key          string
  171     defaultValue []string
  172 }
  173 
  174 func (self StringSliceFlagParser) Match(values []string) ([]string, bool) {
  175     if len(values) < 2 {
  176         return values, false
  177     }
  178 
  179     var remainingValues []string
  180 
  181     for i := 0; i < len(values); i++ {
  182         if values[i] == self.pattern && i+1 < len(values) {
  183             i++
  184             continue
  185         }
  186         remainingValues = append(remainingValues, values[i])
  187     }
  188 
  189     return remainingValues, len(values) != len(remainingValues)
  190 }
  191 
  192 func (self StringSliceFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
  193     remainingValues, ok := self.Match(values)
  194     if !ok {
  195         return values, map[string]interface{}{self.key: self.defaultValue}
  196     }
  197 
  198     var captured []string
  199 
  200     for i := 0; i < len(values); i++ {
  201         if values[i] == self.pattern && i+1 < len(values) {
  202             captured = append(captured, values[i+1])
  203         }
  204     }
  205 
  206     return remainingValues, map[string]interface{}{self.key: captured}
  207 }
  208 
  209 func (self StringSliceFlagParser) String() string {
  210     return fmt.Sprintf("StringSliceFlagParser '%s'", self.pattern)
  211 }
  212 
  213 type FlagParser struct {
  214     parsers []Parser
  215 }
  216 
  217 func (self FlagParser) Match(values []string) ([]string, bool) {
  218     remainingValues := values
  219 
  220     for _, parser := range self.parsers {
  221         remainingValues, _ = parser.Match(remainingValues)
  222     }
  223     return remainingValues, true
  224 }
  225 
  226 func (self FlagParser) Capture(values []string) ([]string, map[string]interface{}) {
  227     captured := map[string]interface{}{}
  228     remainingValues := values
  229 
  230     for _, parser := range self.parsers {
  231         var data map[string]interface{}
  232         remainingValues, data = parser.Capture(remainingValues)
  233         for key, value := range data {
  234             captured[key] = value
  235         }
  236     }
  237 
  238     return remainingValues, captured
  239 }
  240 
  241 func (self FlagParser) String() string {
  242     return fmt.Sprintf("FlagParser %v", self.parsers)
  243 }
  244 
  245 type ShortCircuitParser struct {
  246     parsers []Parser
  247 }
  248 
  249 func (self ShortCircuitParser) Match(values []string) ([]string, bool) {
  250     remainingValues := values
  251 
  252     for _, parser := range self.parsers {
  253         var ok bool
  254         remainingValues, ok = parser.Match(remainingValues)
  255         if ok {
  256             return remainingValues, true
  257         }
  258     }
  259 
  260     return remainingValues, false
  261 }
  262 
  263 func (self ShortCircuitParser) Capture(values []string) ([]string, map[string]interface{}) {
  264     if len(self.parsers) == 0 {
  265         return values, nil
  266     }
  267 
  268     for _, parser := range self.parsers {
  269         if _, ok := parser.Match(values); ok {
  270             return parser.Capture(values)
  271         }
  272     }
  273 
  274     // No parsers matched at this point,
  275     // just return the capture value of the first one
  276     return self.parsers[0].Capture(values)
  277 }
  278 
  279 func (self ShortCircuitParser) String() string {
  280     return fmt.Sprintf("ShortCircuitParser %v", self.parsers)
  281 }
  282 
  283 type CompleteParser struct {
  284     parsers []Parser
  285 }
  286 
  287 func (self CompleteParser) Match(values []string) ([]string, bool) {
  288     remainingValues := copySlice(values)
  289 
  290     for _, parser := range self.parsers {
  291         var ok bool
  292         remainingValues, ok = parser.Match(remainingValues)
  293         if !ok {
  294             return remainingValues, false
  295         }
  296     }
  297 
  298     return remainingValues, len(remainingValues) == 0
  299 }
  300 
  301 func (self CompleteParser) Capture(values []string) ([]string, map[string]interface{}) {
  302     remainingValues := copySlice(values)
  303     data := map[string]interface{}{}
  304 
  305     for _, parser := range self.parsers {
  306         var captured map[string]interface{}
  307         remainingValues, captured = parser.Capture(remainingValues)
  308         for key, value := range captured {
  309             data[key] = value
  310         }
  311     }
  312 
  313     return remainingValues, data
  314 }
  315 
  316 func (self CompleteParser) String() string {
  317     return fmt.Sprintf("CompleteParser %v", self.parsers)
  318 }
  319 
  320 func flagKeyValueMatch(key string, values []string, index int) ([]string, string, bool) {
  321     if index > len(values)-2 {
  322         return values, "", false
  323     }
  324 
  325     if values[index] == key {
  326         value := values[index+1]
  327         remaining := append(copySlice(values[:index]), values[index+2:]...)
  328         return remaining, value, true
  329     }
  330 
  331     return flagKeyValueMatch(key, values, index+1)
  332 }
  333 
  334 func flagKeyMatch(key string, values []string, index int) ([]string, bool) {
  335     if index > len(values)-1 {
  336         return values, false
  337     }
  338 
  339     if values[index] == key {
  340         remaining := append(copySlice(values[:index]), values[index+1:]...)
  341         return remaining, true
  342     }
  343 
  344     return flagKeyMatch(key, values, index+1)
  345 }
  346 
  347 func copySlice(a []string) []string {
  348     b := make([]string, len(a))
  349     copy(b, a)
  350     return b
  351 }