github.com/driusan/bug@v0.3.2-0.20190306121946-d7f4e7f33fea/bugapp/utils.go (about)

     1  package bugapp
     2  
     3  type ArgumentList []string
     4  
     5  func (args ArgumentList) HasArgument(arg string) bool {
     6  	for _, argCandidate := range args {
     7  		if arg == argCandidate {
     8  			return true
     9  		}
    10  	}
    11  	return false
    12  }
    13  
    14  func (args ArgumentList) GetArgument(argname, defaultVal string) string {
    15  	for idx, argCandidate := range args {
    16  		if argname == argCandidate {
    17  			// If it's the last argument, then return string
    18  			// "true" because we can't return idx+1, but it
    19  			// shouldn't be the default value when the argument
    20  			// isn't provided either..
    21  			if idx >= len(args)-1 {
    22  				return "true"
    23  			}
    24  			return args[idx+1]
    25  		}
    26  	}
    27  	return defaultVal
    28  }
    29  
    30  func (args ArgumentList) GetAndRemoveArguments(argnames []string) (ArgumentList, []string) {
    31  	var nextArgumentType int = -1
    32  	matches := make([]string, len(argnames))
    33  	var retArgs []string
    34  	for idx, argCandidate := range args {
    35  		// The last token was in argnames, so this one
    36  		// is the value. Set it in matches and reset
    37  		// nextArgumentType and continue to the next
    38  		// possible token
    39  		if nextArgumentType != -1 {
    40  			matches[nextArgumentType] = argCandidate
    41  			nextArgumentType = -1
    42  			continue
    43  		}
    44  
    45  		// Check if this is a argname we're looking for
    46  		for argidx, argname := range argnames {
    47  			if argname == argCandidate {
    48  				if idx >= len(args)-1 {
    49  					matches[argidx] = "true"
    50  				}
    51  				nextArgumentType = argidx
    52  				break
    53  			}
    54  		}
    55  
    56  		// It wasn't an argname, so add it to the return
    57  		if nextArgumentType == -1 {
    58  			retArgs = append(retArgs, argCandidate)
    59  		}
    60  	}
    61  	return retArgs, matches
    62  }