2023-04-26 18:16:42 +03:00
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
|
|
|
)
|
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
const (
|
|
|
|
// TagSource indicates that an Opt with this tag applies to source config
|
|
|
|
// (as opposed to applying only to base config). An opt with this tag
|
|
|
|
// typically applies to both base and source config.
|
|
|
|
TagSource = "source"
|
|
|
|
|
|
|
|
// TagTuning indicates that the Opt is related to tuning behavior.
|
|
|
|
TagTuning = "tuning"
|
|
|
|
|
|
|
|
// TagSQL indicates that the Opt is related to SQL interaction.
|
|
|
|
TagSQL = "sql"
|
|
|
|
|
|
|
|
// TagOutput indicates the Opt is related to output/formatting.
|
|
|
|
TagOutput = "output"
|
|
|
|
)
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// Opt is an option type. Concrete impls exist for various types,
|
|
|
|
// such as options.Int or options.Duration. Each concrete type must implement
|
2023-04-30 17:18:56 +03:00
|
|
|
// a "Get(o Options) T" method that returns the appropriate type T. The
|
|
|
|
// value returned by that Get method will be the same as that returned
|
|
|
|
// by the generic Opt.GetAny method. The impl should also provide a NewT(...) T
|
|
|
|
// constructor. The caller typically registers the new Opt in a options.Registry
|
|
|
|
// via Registry.Add.
|
2023-04-26 18:16:42 +03:00
|
|
|
//
|
2023-05-01 06:59:34 +03:00
|
|
|
// An impl should implement the Process method to appropriately munge the
|
|
|
|
// backing value. For example, options.Duration converts a
|
2023-04-26 18:16:42 +03:00
|
|
|
// string such as "1m30s" into a time.Duration.
|
|
|
|
type Opt interface {
|
|
|
|
// Key returns the Opt key, such as "ping.timeout".
|
|
|
|
Key() string
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// Flag is the long flag name to use, which is typically the same value
|
|
|
|
// as returned by Opt.Key. However, a distinct value can be supplied, such
|
|
|
|
// that flag usage and config usage have different keys. For example,
|
|
|
|
// an Opt might have a key "diff.num-lines", but a flag "lines".
|
|
|
|
Flag() string
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// Short is the short key. The zero value indicates no short key.
|
|
|
|
// For example, if the key is "json", the short key could be 'j'.
|
|
|
|
Short() rune
|
|
|
|
|
|
|
|
// Usage is a one-line description of the Opt. Additional detail can be
|
|
|
|
// found in Help.
|
|
|
|
Usage() string
|
|
|
|
|
|
|
|
// Help returns the Opt's help text, which typically provides more detail
|
|
|
|
// than Usage. The text must be plaintext (not markdown). Linebreaks are
|
|
|
|
// recommended at 100 chars.
|
|
|
|
Help() string
|
2023-05-03 15:36:10 +03:00
|
|
|
|
2023-06-18 04:28:11 +03:00
|
|
|
// String returns a log/debug-friendly representation.
|
2023-04-26 18:16:42 +03:00
|
|
|
String() string
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// IsSet returns true if this Opt is set in o.
|
|
|
|
IsSet(o Options) bool
|
|
|
|
|
|
|
|
// GetAny returns the value of this Opt in o. Generally, prefer
|
2023-05-01 06:59:34 +03:00
|
|
|
// use of the concrete strongly-typed Get method. If o is nil or
|
|
|
|
// empty, or the Opt is not in o, the Opt's default value is
|
|
|
|
// returned.
|
2023-04-30 17:18:56 +03:00
|
|
|
GetAny(o Options) any
|
2023-04-26 18:16:42 +03:00
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// DefaultAny returns the default value of this Opt. Generally, prefer
|
|
|
|
// use of the concrete strongly-typed Default method.
|
|
|
|
DefaultAny() any
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// Tags returns any tags on this Opt instance. For example, an Opt might
|
|
|
|
// have tags [source, csv].
|
|
|
|
Tags() []string
|
2023-05-01 06:59:34 +03:00
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
// HasTag returns true if the result of Opt.Tags contains tag.
|
|
|
|
HasTag(tag string) bool
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// Process processes o. The returned Options may be a new instance,
|
|
|
|
// with mutated values. This is typ
|
|
|
|
Process(o Options) (Options, error)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// BaseOpt is a partial implementation of options.Opt that concrete
|
|
|
|
// types can build on.
|
|
|
|
type BaseOpt struct {
|
|
|
|
key string
|
2023-05-22 18:08:14 +03:00
|
|
|
flag string
|
2023-05-07 05:36:34 +03:00
|
|
|
short rune
|
|
|
|
usage string
|
|
|
|
help string
|
|
|
|
tags []string
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// NewBaseOpt returns a new BaseOpt. If flag is empty string, key is
|
|
|
|
// used as the flag value.
|
|
|
|
func NewBaseOpt(key, flag string, short rune, usage, help string, tags ...string) BaseOpt {
|
|
|
|
if flag == "" {
|
|
|
|
flag = key
|
|
|
|
}
|
|
|
|
|
2023-07-27 07:19:11 +03:00
|
|
|
slices.Sort(tags)
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
return BaseOpt{
|
|
|
|
key: key,
|
2023-05-22 18:08:14 +03:00
|
|
|
flag: flag,
|
2023-05-07 05:36:34 +03:00
|
|
|
short: short,
|
|
|
|
usage: usage,
|
|
|
|
help: help,
|
|
|
|
tags: tags,
|
|
|
|
}
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Key implements options.Opt.
|
2023-05-07 05:36:34 +03:00
|
|
|
func (op BaseOpt) Key() string {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.key
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// Flag implements options.Opt.
|
|
|
|
func (op BaseOpt) Flag() string {
|
|
|
|
return op.flag
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// Short implements options.Opt.
|
|
|
|
func (op BaseOpt) Short() rune {
|
|
|
|
return op.short
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usage implements options.Opt.
|
|
|
|
func (op BaseOpt) Usage() string {
|
|
|
|
return op.usage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help implements options.Opt.
|
|
|
|
func (op BaseOpt) Help() string {
|
|
|
|
return op.help
|
2023-05-03 15:36:10 +03:00
|
|
|
}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// IsSet implements options.Opt.
|
2023-05-07 05:36:34 +03:00
|
|
|
func (op BaseOpt) IsSet(o Options) bool {
|
2023-04-30 17:18:56 +03:00
|
|
|
if o == nil {
|
2023-04-26 18:16:42 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
return o.IsSet(op)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAny is required by options.Opt. It needs to be implemented
|
|
|
|
// by the concrete type.
|
2023-05-07 05:36:34 +03:00
|
|
|
func (op BaseOpt) GetAny(_ Options) any {
|
|
|
|
panic(fmt.Sprintf("GetAny not implemented for: %s", op.key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultAny implements options.Opt.
|
|
|
|
func (op BaseOpt) DefaultAny() any {
|
|
|
|
panic(fmt.Sprintf("DefaultAny not implemented for: %s", op.key))
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements options.Opt.
|
2023-05-07 05:36:34 +03:00
|
|
|
func (op BaseOpt) String() string {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.key
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tags implements options.Opt.
|
2023-05-07 05:36:34 +03:00
|
|
|
func (op BaseOpt) Tags() []string {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.tags
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
// HasTag implements options.Opt.
|
|
|
|
func (op BaseOpt) HasTag(tag string) bool {
|
|
|
|
return slices.Contains(op.tags, tag)
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// Process implements options.Opt.
|
2023-05-07 05:36:34 +03:00
|
|
|
func (op BaseOpt) Process(o Options) (Options, error) {
|
2023-05-01 06:59:34 +03:00
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
var _ Opt = String{}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// NewString returns an options.String instance. If flag is empty, the
|
2023-07-03 18:34:19 +03:00
|
|
|
// value of key is used. If valid Fn is non-nil, it is called from
|
|
|
|
// the process function.
|
|
|
|
func NewString(key, flag string, short rune, defaultVal string,
|
|
|
|
validFn func(string) error, usage, help string, tags ...string,
|
|
|
|
) String {
|
2023-04-26 18:16:42 +03:00
|
|
|
return String{
|
2023-05-22 18:08:14 +03:00
|
|
|
BaseOpt: NewBaseOpt(key, flag, short, usage, help, tags...),
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal: defaultVal,
|
2023-07-03 18:34:19 +03:00
|
|
|
validFn: validFn,
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String is an options.Opt for type string.
|
|
|
|
type String struct {
|
2023-05-07 05:36:34 +03:00
|
|
|
BaseOpt
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal string
|
2023-07-03 18:34:19 +03:00
|
|
|
validFn func(string) error
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// GetAny implements options.Opt.
|
|
|
|
func (op String) GetAny(o Options) any {
|
|
|
|
return op.Get(o)
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// DefaultAny implements options.Opt.
|
|
|
|
func (op String) DefaultAny() any {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default returns the default value of op.
|
|
|
|
func (op String) Default() string {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// Get returns op's value in o. If o is nil, or no value
|
|
|
|
// is set, op's default value is returned.
|
|
|
|
func (op String) Get(o Options) string {
|
|
|
|
if o == nil {
|
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
v, ok := o[op.key]
|
2023-04-26 18:16:42 +03:00
|
|
|
if !ok {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var s string
|
|
|
|
s, ok = v.(string)
|
|
|
|
if !ok {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// Process implements options.Opt. If the String was constructed
|
|
|
|
// with validator function, it is invoked on the value of the Opt,
|
|
|
|
// if it is set. Otherwise the method is no-op.
|
|
|
|
func (op String) Process(o Options) (Options, error) {
|
|
|
|
if op.validFn == nil {
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := o[op.key]
|
|
|
|
if !ok || v == nil {
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var s string
|
|
|
|
if s, ok = v.(string); !ok {
|
|
|
|
return nil, errz.Errorf("expected string value for {%s} but got %T: %v", op.key, v, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := op.validFn(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
var _ Opt = Int{}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// NewInt returns an options.Int instance. If flag is empty, the
|
|
|
|
// value of key is used.
|
|
|
|
func NewInt(key, flag string, short rune, defaultVal int, usage, help string, tags ...string) Int {
|
2023-04-26 18:16:42 +03:00
|
|
|
return Int{
|
2023-05-22 18:08:14 +03:00
|
|
|
BaseOpt: NewBaseOpt(key, flag, short, usage, help, tags...),
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal: defaultVal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int is an options.Opt for type int.
|
|
|
|
type Int struct {
|
2023-05-07 05:36:34 +03:00
|
|
|
BaseOpt
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal int
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// Default returns the default value of op.
|
|
|
|
func (op Int) Default() int {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// GetAny implements options.Opt.
|
|
|
|
func (op Int) GetAny(o Options) any {
|
|
|
|
return op.Get(o)
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// DefaultAny implements options.Opt.
|
|
|
|
func (op Int) DefaultAny() any {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// Get returns op's value in o. If o is nil, or no value
|
|
|
|
// is set, op's default value is returned.
|
|
|
|
func (op Int) Get(o Options) int {
|
|
|
|
if o == nil {
|
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
v, ok := o[op.key]
|
2023-04-26 18:16:42 +03:00
|
|
|
if !ok || v == nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch i := v.(type) {
|
|
|
|
case int:
|
|
|
|
return i
|
|
|
|
case int64:
|
|
|
|
return int(i)
|
|
|
|
case uint64:
|
|
|
|
return int(i)
|
|
|
|
case uint:
|
|
|
|
return int(i)
|
|
|
|
default:
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// Process implements options.Opt. It converts matching
|
2023-04-30 17:18:56 +03:00
|
|
|
// values in o into bool. If no match found,
|
2023-04-26 18:16:42 +03:00
|
|
|
// the input arg is returned unchanged. Otherwise, a clone is
|
|
|
|
// returned.
|
2023-04-30 17:18:56 +03:00
|
|
|
func (op Int) Process(o Options) (Options, error) {
|
|
|
|
if o == nil {
|
2023-04-26 18:16:42 +03:00
|
|
|
return nil, nil //nolint:nilnil
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
v, ok := o[op.key]
|
2023-04-26 18:16:42 +03:00
|
|
|
if !ok || v == nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok = v.(int); ok {
|
|
|
|
// Happy path
|
2023-04-30 17:18:56 +03:00
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
o = o.Clone()
|
2023-04-26 18:16:42 +03:00
|
|
|
|
|
|
|
var i int
|
|
|
|
switch v := v.(type) {
|
|
|
|
case float32:
|
|
|
|
i = int(v)
|
|
|
|
case float64:
|
|
|
|
i = int(v)
|
|
|
|
case uint:
|
|
|
|
i = int(v)
|
|
|
|
case uint8:
|
|
|
|
i = int(v)
|
|
|
|
case uint16:
|
|
|
|
i = int(v)
|
|
|
|
case uint32:
|
|
|
|
i = int(v)
|
|
|
|
case uint64:
|
|
|
|
i = int(v)
|
|
|
|
case int8:
|
|
|
|
i = int(v)
|
|
|
|
case int16:
|
|
|
|
i = int(v)
|
|
|
|
case int32:
|
|
|
|
i = int(v)
|
|
|
|
case int64:
|
|
|
|
i = int(v)
|
|
|
|
case string:
|
|
|
|
if v == "" {
|
|
|
|
// Empty string is effectively nil
|
2023-04-30 17:18:56 +03:00
|
|
|
delete(o, op.key)
|
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if i, err = strconv.Atoi(v); err != nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return nil, errz.Wrapf(err, "invalid int value for {%s}: %v", op.key, v)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// This shouldn't happen, but it's a last-ditch effort.
|
|
|
|
// Print v as a string, and try to parse it.
|
|
|
|
s := fmt.Sprintf("%v", v)
|
|
|
|
var err error
|
|
|
|
if i, err = strconv.Atoi(s); err != nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return nil, errz.Wrapf(err, "invalid int value for {%s}: %v", op.key, v)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
o[op.key] = i
|
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Opt = Bool{}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// NewBool returns an options.Bool instance. If flag is empty, the value
|
|
|
|
// of key is used.
|
|
|
|
func NewBool(key, flag string, short rune, defaultVal bool, usage, help string, tags ...string) Bool {
|
2023-04-26 18:16:42 +03:00
|
|
|
return Bool{
|
2023-05-22 18:08:14 +03:00
|
|
|
BaseOpt: NewBaseOpt(key, flag, short, usage, help, tags...),
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal: defaultVal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bool is an options.Opt for type bool.
|
|
|
|
type Bool struct {
|
2023-05-07 05:36:34 +03:00
|
|
|
BaseOpt
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal bool
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// GetAny implements options.Opt.
|
|
|
|
func (op Bool) GetAny(opts Options) any {
|
|
|
|
return op.Get(opts)
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// DefaultAny implements options.Opt.
|
|
|
|
func (op Bool) DefaultAny() any {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// Get returns op's value in o. If o is nil, or no value
|
|
|
|
// is set, op's default value is returned.
|
|
|
|
func (op Bool) Get(o Options) bool {
|
|
|
|
if o == nil {
|
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
v, ok := o[op.key]
|
2023-04-26 18:16:42 +03:00
|
|
|
if !ok || v == nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bool
|
|
|
|
b, ok = v.(bool)
|
|
|
|
if !ok {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// Default returns the default value of op.
|
|
|
|
func (op Bool) Default() bool {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// Process implements options.Opt. It converts matching
|
2023-04-30 17:18:56 +03:00
|
|
|
// string values in o into bool. If no match found,
|
2023-04-26 18:16:42 +03:00
|
|
|
// the input arg is returned unchanged. Otherwise, a clone is
|
|
|
|
// returned.
|
2023-04-30 17:18:56 +03:00
|
|
|
func (op Bool) Process(o Options) (Options, error) {
|
|
|
|
if o == nil {
|
2023-04-26 18:16:42 +03:00
|
|
|
return nil, nil //nolint:nilnil
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
v, ok := o[op.key]
|
2023-04-26 18:16:42 +03:00
|
|
|
if !ok || v == nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok = v.(bool); ok {
|
|
|
|
// Happy path
|
2023-04-30 17:18:56 +03:00
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
o = o.Clone()
|
2023-04-26 18:16:42 +03:00
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
if v == "" {
|
|
|
|
// Empty string is effectively nil
|
2023-04-30 17:18:56 +03:00
|
|
|
delete(o, op.key)
|
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// It could be a string like "true"
|
|
|
|
b, err := stringz.ParseBool(v)
|
|
|
|
if err != nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return nil, errz.Wrapf(err, "invalid bool value for {%s}: %v", op.key, v)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
2023-04-30 17:18:56 +03:00
|
|
|
o[op.key] = b
|
2023-04-26 18:16:42 +03:00
|
|
|
default:
|
|
|
|
// Well, we don't know what this is... maybe a number like "1"?
|
|
|
|
// Last-ditch effort. Print the value to a string, and check
|
|
|
|
// if we can parse the string into a bool.
|
|
|
|
s := fmt.Sprintf("%v", v)
|
|
|
|
b, err := stringz.ParseBool(s)
|
|
|
|
if err != nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return nil, errz.Wrapf(err, "invalid bool value for {%s}: %v", op.key, v)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
2023-04-30 17:18:56 +03:00
|
|
|
o[op.key] = b
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Opt = Duration{}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// NewDuration returns an options.Duration instance. If flag is empty, the
|
|
|
|
// value of key is used.
|
|
|
|
func NewDuration(key, flag string, short rune, defaultVal time.Duration, usage, help string, tags ...string) Duration {
|
2023-04-26 18:16:42 +03:00
|
|
|
return Duration{
|
2023-05-22 18:08:14 +03:00
|
|
|
BaseOpt: NewBaseOpt(key, flag, short, usage, help, tags...),
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal: defaultVal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration is an options.Opt for time.Duration.
|
|
|
|
type Duration struct {
|
2023-05-07 05:36:34 +03:00
|
|
|
BaseOpt
|
2023-04-26 18:16:42 +03:00
|
|
|
defaultVal time.Duration
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// Process implements options.Opt. It converts matching
|
2023-04-30 17:18:56 +03:00
|
|
|
// string values in o into time.Duration. If no match found,
|
2023-04-26 18:16:42 +03:00
|
|
|
// the input arg is returned unchanged. Otherwise, a clone is
|
|
|
|
// returned.
|
2023-04-30 17:18:56 +03:00
|
|
|
func (op Duration) Process(o Options) (Options, error) {
|
|
|
|
if o == nil {
|
2023-04-26 18:16:42 +03:00
|
|
|
return nil, nil //nolint:nilnil
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
v, ok := o[op.key]
|
2023-04-26 18:16:42 +03:00
|
|
|
if !ok || v == nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return o, nil
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
if _, ok = v.(time.Duration); ok {
|
|
|
|
// v is already a duration, nothing to do here.
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// v should be a string
|
|
|
|
var s string
|
|
|
|
s, ok = v.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errz.Errorf("option {%s} should be {%T} but got {%T}: %v",
|
2023-04-30 17:18:56 +03:00
|
|
|
op.key, s, v, v)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
d, err := time.ParseDuration(s)
|
|
|
|
if err != nil {
|
2023-04-30 17:18:56 +03:00
|
|
|
return nil, errz.Wrapf(err, "options {%s} is not a valid duration", op.key)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
o = o.Clone()
|
|
|
|
o[op.key] = d
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAny implements options.Opt.
|
|
|
|
func (op Duration) GetAny(o Options) any {
|
|
|
|
return op.Get(o)
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// Default returns the default value of op.
|
|
|
|
func (op Duration) Default() time.Duration {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
// DefaultAny implements options.Opt.
|
|
|
|
func (op Duration) DefaultAny() any {
|
|
|
|
return op.defaultVal
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
// Get returns op's value in o. If o is nil, or no value
|
|
|
|
// is set, op's default value is returned.
|
|
|
|
func (op Duration) Get(o Options) time.Duration {
|
|
|
|
if o == nil {
|
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
v, ok := o[op.key]
|
2023-04-26 18:16:42 +03:00
|
|
|
if !ok {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var d time.Duration
|
|
|
|
d, ok = v.(time.Duration)
|
|
|
|
if !ok {
|
2023-04-30 17:18:56 +03:00
|
|
|
return op.defaultVal
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|