MCPcopy Create free account
hub / github.com/docopt/docopt.go / parseLong

Function parseLong

docopt.go:355–432  ·  view source on GitHub ↗
(tokens *tokenList, options *patternList)

Source from the content-addressed store, hash-verified

353}
354
355func parseLong(tokens *tokenList, options *patternList) (patternList, error) {
356 // long ::= '--' chars [ ( ' ' | '=' ) chars ] ;
357 long, eq, v := stringPartition(tokens.move().String(), "=")
358 var value interface{}
359 var opt *pattern
360 if eq == "" && v == "" {
361 value = nil
362 } else {
363 value = v
364 }
365
366 if !strings.HasPrefix(long, "--") {
367 return nil, newError("long option '%s' doesn't start with --", long)
368 }
369 similar := patternList{}
370 for _, o := range *options {
371 if o.long == long {
372 similar = append(similar, o)
373 }
374 }
375 if tokens.err == errorUser && len(similar) == 0 { // if no exact match
376 similar = patternList{}
377 for _, o := range *options {
378 if strings.HasPrefix(o.long, long) {
379 similar = append(similar, o)
380 }
381 }
382 }
383 if len(similar) > 1 { // might be simply specified ambiguously 2+ times?
384 similarLong := make([]string, len(similar))
385 for i, s := range similar {
386 similarLong[i] = s.long
387 }
388 return nil, tokens.errorFunc("%s is not a unique prefix: %s?", long, strings.Join(similarLong, ", "))
389 } else if len(similar) < 1 {
390 argcount := 0
391 if eq == "=" {
392 argcount = 1
393 }
394 opt = newOption("", long, argcount, false)
395 *options = append(*options, opt)
396 if tokens.err == errorUser {
397 var val interface{}
398 if argcount > 0 {
399 val = value
400 } else {
401 val = true
402 }
403 opt = newOption("", long, argcount, val)
404 }
405 } else {
406 opt = newOption(similar[0].short, similar[0].long, similar[0].argcount, similar[0].value)
407 if opt.argcount == 0 {
408 if value != nil {
409 return nil, tokens.errorFunc("%s must not have an argument", opt.long)
410 }
411 } else {
412 if value == nil {

Callers 2

parseArgvFunction · 0.85
parseAtomFunction · 0.85

Calls 6

stringPartitionFunction · 0.85
newOptionFunction · 0.85
moveMethod · 0.80
currentMethod · 0.80
StringMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected