cmd/opt/set.go

119 lines
1.7 KiB
Go

package opt
type Set []Option
func NewSet() Set {
return Set{}
}
func (s Set) MaxWidth() int {
max := 0
for _, f := range s {
if w := len(Names(f)); w > max {
max = w
}
}
return max
}
func (s *Set) Add(f Option) {
*s = append(*s, f)
}
func (s Set) Get(name string) (Option, bool) {
for _, o := range s {
if o.Name() == name {
return o, true
}
if o.ShortName() == name {
return o, true
}
}
return nil, false
}
func (s Set) Names() []string {
names := []string{}
for _, o := range s {
names = append(names, "--"+o.Name())
names = append(names, "-"+o.ShortName())
}
return names
}
func (s Set) GetByLongName(longName string) (Option, bool) {
for _, o := range s {
if o.Name() == longName {
return o, true
}
}
return nil, false
}
func (s Set) GetByShortName(shortName string) (Option, bool) {
for _, o := range s {
if o.ShortName() == shortName {
return o, true
}
}
return nil, false
}
func (s Set) GetBool(name string) (*BoolOption, bool) {
o, ok := s.Get(name)
if !ok {
return nil, false
}
b, ok := o.(*BoolOption)
if !ok {
return nil, false
}
return b, true
}
func (s Set) GetString(name string) (*StringOption, bool) {
o, ok := s.Get(name)
if !ok {
return nil, false
}
b, ok := o.(*StringOption)
if !ok {
return nil, false
}
return b, true
}
func (s Set) GetInt(name string) (*IntOption, bool) {
o, ok := s.Get(name)
if !ok {
return nil, false
}
b, ok := o.(*IntOption)
if !ok {
return nil, false
}
return b, true
}
func (s Set) GetFloat(name string) (*FloatOption, bool) {
o, ok := s.Get(name)
if !ok {
return nil, false
}
b, ok := o.(*FloatOption)
if !ok {
return nil, false
}
return b, true
}