GetoptGo/getopt.go

144 lines
3.7 KiB
Go

package getopt
import (
"fmt"
"os"
"strconv"
)
type flag struct {
name string
shortname string
description string
required bool
value interface{}
}
type GetoptInterface interface {
StringLong(name *string, shortname *rune, required bool, description string) **string
Usage()
BoolLong(name *string, shortname *rune, description string) *bool
NoOptionArgs() []string
Parse([]string)
ParseArgv()
UInt64Long(name *string, shortname *rune, required bool, description string) **uint64
CheckRequiredOptions() (bool, string, string)
}
type Getopt struct {
array_flags []*flag
flags map[string]*flag
flagsByShortname map[string]*flag
commonArgs []string
}
func New() Getopt {
return Getopt{
array_flags: make([]*flag, 0),
commonArgs: make([]string, 0),
flags: make(map[string]*flag),
flagsByShortname: make(map[string]*flag),
}
}
func (self *Getopt) CheckRequiredOptions() (bool, string, string) {
for _, value := range self.array_flags {
switch value.value.(interface{}).(type) {
case *bool:
continue
case **string:
if value.required && *value.value.(**string) == nil {
return false, value.name, value.shortname
}
case **uint64:
if value.required && *value.value.(**uint64) == nil {
return false, value.name, value.shortname
}
}
}
return true, "", ""
}
func (self *Getopt) StringLong(name *string, shortname *rune, required bool, description string) **string {
var value *string = nil
flagToAssign := flag{self.nilToEmptyString(name), self.nilRuneToEmptyString(shortname), description, required, &value}
if name != nil {
self.flags[*name] = &flagToAssign
}
if shortname != nil {
self.flagsByShortname[string(*shortname)] = &flagToAssign
}
self.array_flags = append(self.array_flags, &flagToAssign)
return &value
}
func (self *Getopt) Usage() {
fmt.Print(self.UsageString())
}
func (self *Getopt) UsageString() string {
var return_string string
var usage [][]string
for _, value := range self.array_flags {
usage = append(usage, self.usageOption(*value))
}
max := self.maxLen(usage)
for _, value := range usage {
for j, j_value := range value {
return_string += fmt.Sprintf("\t%-"+strconv.FormatInt(max[j], 10)+"s", j_value)
}
return_string += fmt.Sprintf("\n")
}
return return_string
}
func (self *Getopt) BoolLong(name *string, shortname *rune, description string) *bool {
value := false
flagToAssign := flag{self.nilToEmptyString(name), self.nilRuneToEmptyString(shortname), description, true, &value}
if name != nil {
self.flags[*name] = &flagToAssign
}
if shortname != nil {
self.flagsByShortname[string(*shortname)] = &flagToAssign
}
self.array_flags = append(self.array_flags, &flagToAssign)
return &value
}
func (self *Getopt) UInt64Long(name *string, shortname *rune, required bool, description string) **uint64 {
var value *uint64 = nil
flagToAssign := flag{self.nilToEmptyString(name), self.nilRuneToEmptyString(shortname), description, required, &value}
if name != nil {
self.flags[*name] = &flagToAssign
}
if shortname != nil {
self.flagsByShortname[string(*shortname)] = &flagToAssign
}
self.array_flags = append(self.array_flags, &flagToAssign)
return &value
}
func (self *Getopt) ParseArgv() {
args := os.Args[1:]
self.Parse(args)
}
func (self *Getopt) Parse(args []string) {
expectingArgument := false
var expectingFor string
short := false
for _, value := range args {
if !expectingArgument {
self.parseNotOptionArgument(value, &expectingArgument, &expectingFor, &short)
} else {
self.parseOptionArgument(value, expectingFor, short)
expectingArgument = false
}
}
}
func (self *Getopt) NoOptionArgs() []string {
return self.commonArgs
}