GetoptGo/private.go

157 lines
3.8 KiB
Go

package getopt
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
)
func (self *Getopt) usageOption(flagToStringify flag) []string {
var printArgument bool
switch flagToStringify.value.(interface{}).(type) {
case *bool:
printArgument = false
default:
printArgument = true
}
var returned []string
if flagToStringify.shortname != "" {
returned = append(returned, "-"+flagToStringify.shortname)
} else {
returned = append(returned, "")
}
if flagToStringify.name != "" {
returned = append(returned, "--"+flagToStringify.name)
} else {
returned = append(returned, "")
}
if printArgument {
returned = append(returned, "<argument>")
} else {
returned = append(returned, "")
}
if flagToStringify.required {
returned = append(returned, "(required)")
} else {
returned = append(returned, "")
}
returned = append(returned, flagToStringify.description)
return returned
}
func (self *Getopt) maxLen(lol [][]string) []int64 {
var returned []int64 = make([]int64, 5)
for _, i_val := range lol {
for j, j_val := range i_val {
if returned[j] < int64(len(j_val)) {
returned[j] = int64(len(j_val))
}
}
}
return returned
}
func (self *Getopt) parseOptionArgument(value string, expectingFor string, short bool) {
var gotFlag *flag
var ok bool
if short {
gotFlag, ok = self.flagsByShortname[expectingFor]
} else {
gotFlag, ok = self.flags[expectingFor]
}
if !ok {
fmt.Printf("Error retrieving the key, this IS a bug. %s\n", expectingFor)
}
self.assignToFlag(gotFlag, value)
}
func (self *Getopt) parseNotOptionArgument(currentIteration string, expectingArgument *bool, expectingFor *string, short *bool) {
matchName, err := regexp.MatchString("^--", currentIteration)
if err != nil {
log.Panic(err)
}
matchShortname, err := regexp.MatchString("^-", currentIteration)
if err != nil {
log.Panic(err)
}
if matchName {
*short = false
self.parseOption(currentIteration, expectingFor, expectingArgument, `^--([^=]*)(?:=(.*))?$`, self.flags)
} else if matchShortname {
*short = true
self.parseOption(currentIteration, expectingFor, expectingArgument, `^-(.)(.+)?$`, self.flagsByShortname)
} else {
self.commonArgs = append(self.commonArgs, currentIteration)
}
}
func (self *Getopt) parseOption(currentIteration string, expectingFor *string, expectingArgument *bool, option_regex string, flags map[string]*flag) {
re := regexp.MustCompile(option_regex)
listName := re.FindStringSubmatch(currentIteration)
name := listName[1]
gotFlag, ok := flags[name]
if !ok {
fmt.Printf("%s not in the option list.\n", name)
self.Usage()
os.Exit(1)
}
var nextParameter bool
switch gotFlag.value.(interface{}).(type) {
case *bool:
nextParameter = false
*(gotFlag.value.(*bool)) = true
default:
nextParameter = true
}
if listName[2] != "" {
if !nextParameter {
fmt.Printf("%s does not get any parameters.\n", name)
self.Usage()
os.Exit(1)
}
gotParameter := listName[2]
self.assignToFlag(gotFlag, gotParameter)
} else if nextParameter {
*expectingArgument = true
*expectingFor = name
}
}
func (self *Getopt) assignToFlag(toAssignFlag *flag, value string) {
switch toAssignFlag.value.(interface{}).(type) {
case **string:
*(toAssignFlag.value.(**string)) = &value
case **uint64:
value_uint, err := strconv.ParseUint(value, 10, 64)
if err != nil {
fmt.Printf("%s is not parseable into a unsigned interger of 64 bits.\n", value)
self.Usage()
os.Exit(1)
}
*(toAssignFlag.value.(**uint64)) = &value_uint
default:
fmt.Printf("Unsupported type %v.\n", toAssignFlag.value)
self.Usage()
os.Exit(1)
}
}
func (self *Getopt) nilRuneToEmptyString(input *rune) string {
if input == nil {
return ""
}
return string(*input)
}
func (self *Getopt) nilToEmptyString(input *string) string {
if input == nil {
return ""
}
return *input
}