You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
951 B

4 years ago
package argument
4 years ago
4 years ago
import (
"gitoa.ru/go-4devs/console/input/value"
"gitoa.ru/go-4devs/console/input/value/flag"
4 years ago
)
func New(name, description string, opts ...func(*Argument)) Argument {
4 years ago
a := Argument{
Name: name,
Description: description,
}
for _, opt := range opts {
opt(&a)
}
return a
}
type Argument struct {
Name string
Description string
4 years ago
Default value.Value
Flag flag.Flag
Valid []func(value.Value) error
4 years ago
}
func (a Argument) HasDefault() bool {
return a.Default != nil
}
func (a Argument) IsBool() bool {
return a.Flag.IsBool()
}
func (a Argument) IsRequired() bool {
return a.Flag.IsRequired()
}
func (a Argument) IsArray() bool {
return a.Flag.IsArray()
}
4 years ago
func (a Argument) Validate(v value.Value) error {
4 years ago
for _, valid := range a.Valid {
if err := valid(v); err != nil {
4 years ago
return Error(a.Name, err)
4 years ago
}
}
return nil
}
4 years ago
func Error(name string, err error) error {
return err
}