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

Function parseShorts

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

Source from the content-addressed store, hash-verified

432}
433
434func parseShorts(tokens *tokenList, options *patternList) (patternList, error) {
435 // shorts ::= '-' ( chars )* [ [ ' ' ] chars ] ;
436 tok := tokens.move()
437 if !tok.hasPrefix("-") || tok.hasPrefix("--") {
438 return nil, newError("short option '%s' doesn't start with -", tok)
439 }
440 left := strings.TrimLeft(tok.String(), "-")
441 parsed := patternList{}
442 for left != "" {
443 var opt *pattern
444 short := "-" + left[0:1]
445 left = left[1:]
446 similar := patternList{}
447 for _, o := range *options {
448 if o.short == short {
449 similar = append(similar, o)
450 }
451 }
452 if len(similar) > 1 {
453 return nil, tokens.errorFunc("%s is specified ambiguously %d times", short, len(similar))
454 } else if len(similar) < 1 {
455 opt = newOption(short, "", 0, false)
456 *options = append(*options, opt)
457 if tokens.err == errorUser {
458 opt = newOption(short, "", 0, true)
459 }
460 } else { // why copying is necessary here?
461 opt = newOption(short, similar[0].long, similar[0].argcount, similar[0].value)
462 var value interface{}
463 if opt.argcount > 0 {
464 if left == "" {
465 if tokens.current().match(true, "--") {
466 return nil, tokens.errorFunc("%s requires argument", short)
467 }
468 value = tokens.move().String()
469 } else {
470 value = left
471 left = ""
472 }
473 }
474 if tokens.err == errorUser {
475 if value != nil {
476 opt.value = value
477 } else {
478 opt.value = true
479 }
480 }
481 }
482 parsed = append(parsed, opt)
483 }
484 return parsed, nil
485}
486
487func newTokenList(source []string, err errorType) *tokenList {
488 errorFunc := newError

Callers 2

parseArgvFunction · 0.85
parseAtomFunction · 0.85

Calls 6

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

Tested by

no test coverage detected