2024-10-29 07:22:04 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2024-12-01 10:22:47 +01:00
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/item"
|
2024-10-29 07:22:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ArgSet struct {
|
|
|
|
Main string
|
|
|
|
Flags map[string]Flag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *ArgSet) Set(name, val string) error {
|
|
|
|
f, ok := as.Flags[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown flag %s", name)
|
|
|
|
}
|
|
|
|
return f.Set(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *ArgSet) IsSet(name string) bool {
|
|
|
|
f, ok := as.Flags[name]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return f.IsSet()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *ArgSet) GetString(name string) string {
|
|
|
|
flag, ok := as.Flags[name]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
val, ok := flag.Get().(string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (as *ArgSet) GetDate(name string) item.Date {
|
2024-10-29 07:22:04 +01:00
|
|
|
flag, ok := as.Flags[name]
|
|
|
|
if !ok {
|
2024-12-19 12:06:03 +01:00
|
|
|
return item.Date{}
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-12-19 12:06:03 +01:00
|
|
|
val, ok := flag.Get().(item.Date)
|
2024-10-29 07:22:04 +01:00
|
|
|
if !ok {
|
2024-12-19 12:06:03 +01:00
|
|
|
return item.Date{}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *ArgSet) GetTime(name string) item.Time {
|
|
|
|
flag, ok := as.Flags[name]
|
|
|
|
if !ok {
|
|
|
|
return item.Time{}
|
|
|
|
}
|
|
|
|
val, ok := flag.Get().(item.Time)
|
|
|
|
if !ok {
|
|
|
|
return item.Time{}
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *ArgSet) GetDuration(name string) time.Duration {
|
|
|
|
flag, ok := as.Flags[name]
|
|
|
|
if !ok {
|
|
|
|
return time.Duration(0)
|
|
|
|
}
|
|
|
|
val, ok := flag.Get().(time.Duration)
|
|
|
|
if !ok {
|
|
|
|
return time.Duration(0)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (as *ArgSet) GetRecurrer(name string) item.Recurrer {
|
|
|
|
flag, ok := as.Flags[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
val, ok := flag.Get().(item.Recurrer)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *ArgSet) GetInt(name string) int {
|
2024-12-01 10:22:47 +01:00
|
|
|
flag, ok := as.Flags[name]
|
|
|
|
if !ok {
|
2024-12-19 12:06:03 +01:00
|
|
|
return 0
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
2024-12-19 12:06:03 +01:00
|
|
|
val, ok := flag.Get().(int)
|
2024-12-01 10:22:47 +01:00
|
|
|
if !ok {
|
2024-12-19 12:06:03 +01:00
|
|
|
return 0
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|