57 lines
927 B
Go
57 lines
927 B
Go
|
package opts
|
||
|
|
||
|
import "strconv"
|
||
|
|
||
|
type IntOption struct {
|
||
|
name string
|
||
|
shortName string
|
||
|
description string
|
||
|
value int
|
||
|
}
|
||
|
|
||
|
var _ Option = (*StringOption)(nil)
|
||
|
|
||
|
func Int(name, shortName string, defaultValue int, description string) *IntOption {
|
||
|
return &IntOption{
|
||
|
name: name,
|
||
|
shortName: shortName,
|
||
|
description: description,
|
||
|
value: defaultValue,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Description implements Option.
|
||
|
func (o *IntOption) Description() string {
|
||
|
return o.description
|
||
|
}
|
||
|
|
||
|
// Name implements Option.
|
||
|
func (o *IntOption) Name() string {
|
||
|
return o.name
|
||
|
}
|
||
|
|
||
|
// ShortName implements Option.
|
||
|
func (o *IntOption) ShortName() string {
|
||
|
return o.shortName
|
||
|
|
||
|
}
|
||
|
|
||
|
// Value implements Option.
|
||
|
func (o *IntOption) Parse(raw string) error {
|
||
|
if raw == "" {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
intVal, err := strconv.Atoi(raw)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
o.value = intVal
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *IntOption) Value() int {
|
||
|
return o.value
|
||
|
}
|