chore: interface{} -> any
This commit is contained in:
@@ -648,7 +648,7 @@ normal validation.
|
|||||||
|
|
||||||
## Modifying Kong's behaviour
|
## Modifying Kong's behaviour
|
||||||
|
|
||||||
Each Kong parser can be configured via functional options passed to `New(cli interface{}, options...Option)`.
|
Each Kong parser can be configured via functional options passed to `New(cli any, options...Option)`.
|
||||||
|
|
||||||
The full set of options can be found [here](https://godoc.org/github.com/alecthomas/kong#Option).
|
The full set of options can be found [here](https://godoc.org/github.com/alecthomas/kong#Option).
|
||||||
|
|
||||||
@@ -706,7 +706,7 @@ All builtin Go types (as well as a bunch of useful stdlib types like `time.Time`
|
|||||||
1. `NamedMapper(string, Mapper)` and using the tag key `type:"<name>"`.
|
1. `NamedMapper(string, Mapper)` and using the tag key `type:"<name>"`.
|
||||||
2. `KindMapper(reflect.Kind, Mapper)`.
|
2. `KindMapper(reflect.Kind, Mapper)`.
|
||||||
3. `TypeMapper(reflect.Type, Mapper)`.
|
3. `TypeMapper(reflect.Type, Mapper)`.
|
||||||
4. `ValueMapper(interface{}, Mapper)`, passing in a pointer to a field of the grammar.
|
4. `ValueMapper(any, Mapper)`, passing in a pointer to a field of the grammar.
|
||||||
|
|
||||||
### `ConfigureHelp(HelpOptions)` and `Help(HelpFunc)` - customising help
|
### `ConfigureHelp(HelpOptions)` and `Help(HelpFunc)` - customising help
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
// Plugins are dynamically embedded command-line structures.
|
// Plugins are dynamically embedded command-line structures.
|
||||||
//
|
//
|
||||||
// Each element in the Plugins list *must* be a pointer to a structure.
|
// Each element in the Plugins list *must* be a pointer to a structure.
|
||||||
type Plugins []interface{}
|
type Plugins []any
|
||||||
|
|
||||||
func build(k *Kong, ast interface{}) (app *Application, err error) {
|
func build(k *Kong, ast any) (app *Application, err error) {
|
||||||
v := reflect.ValueOf(ast)
|
v := reflect.ValueOf(ast)
|
||||||
iv := reflect.Indirect(v)
|
iv := reflect.Indirect(v)
|
||||||
if v.Kind() != reflect.Ptr || iv.Kind() != reflect.Struct {
|
if v.Kind() != reflect.Ptr || iv.Kind() != reflect.Struct {
|
||||||
|
|||||||
+3
-3
@@ -19,7 +19,7 @@ func (b bindings) String() string {
|
|||||||
return "bindings{" + strings.Join(out, ", ") + "}"
|
return "bindings{" + strings.Join(out, ", ") + "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b bindings) add(values ...interface{}) bindings {
|
func (b bindings) add(values ...any) bindings {
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
v := v
|
v := v
|
||||||
b[reflect.TypeOf(v)] = func() (any, error) { return v, nil }
|
b[reflect.TypeOf(v)] = func() (any, error) { return v, nil }
|
||||||
@@ -27,11 +27,11 @@ func (b bindings) add(values ...interface{}) bindings {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b bindings) addTo(impl, iface interface{}) {
|
func (b bindings) addTo(impl, iface any) {
|
||||||
b[reflect.TypeOf(iface).Elem()] = func() (any, error) { return impl, nil }
|
b[reflect.TypeOf(iface).Elem()] = func() (any, error) { return impl, nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b bindings) addProvider(provider interface{}) error {
|
func (b bindings) addProvider(provider any) error {
|
||||||
pv := reflect.ValueOf(provider)
|
pv := reflect.ValueOf(provider)
|
||||||
t := pv.Type()
|
t := pv.Type()
|
||||||
if t.Kind() != reflect.Func || t.NumOut() != 2 || t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
|
if t.Kind() != reflect.Func || t.NumOut() != 2 || t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
|
||||||
|
|||||||
+1
-1
@@ -42,7 +42,7 @@ func TestConfigValidation(t *testing.T) {
|
|||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeConfig(t *testing.T, config interface{}) (path string, cleanup func()) {
|
func makeConfig(t *testing.T, config any) (path string, cleanup func()) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
w, err := os.CreateTemp("", "")
|
w, err := os.CreateTemp("", "")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
+9
-9
@@ -102,7 +102,7 @@ func Trace(k *Kong, args []string) (*Context, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bind adds bindings to the Context.
|
// Bind adds bindings to the Context.
|
||||||
func (c *Context) Bind(args ...interface{}) {
|
func (c *Context) Bind(args ...any) {
|
||||||
c.bindings.add(args...)
|
c.bindings.add(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ func (c *Context) Bind(args ...interface{}) {
|
|||||||
// This will typically have to be called like so:
|
// This will typically have to be called like so:
|
||||||
//
|
//
|
||||||
// BindTo(impl, (*MyInterface)(nil))
|
// BindTo(impl, (*MyInterface)(nil))
|
||||||
func (c *Context) BindTo(impl, iface interface{}) {
|
func (c *Context) BindTo(impl, iface any) {
|
||||||
c.bindings.addTo(impl, iface)
|
c.bindings.addTo(impl, iface)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ func (c *Context) BindTo(impl, iface interface{}) {
|
|||||||
//
|
//
|
||||||
// This is useful when the Run() function of different commands require different values that may
|
// This is useful when the Run() function of different commands require different values that may
|
||||||
// not all be initialisable from the main() function.
|
// not all be initialisable from the main() function.
|
||||||
func (c *Context) BindToProvider(provider interface{}) error {
|
func (c *Context) BindToProvider(provider any) error {
|
||||||
return c.bindings.addProvider(provider)
|
return c.bindings.addProvider(provider)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,7 +306,7 @@ func (c *Context) AddResolver(resolver Resolver) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FlagValue returns the set value of a flag if it was encountered and exists, or its default value.
|
// FlagValue returns the set value of a flag if it was encountered and exists, or its default value.
|
||||||
func (c *Context) FlagValue(flag *Flag) interface{} {
|
func (c *Context) FlagValue(flag *Flag) any {
|
||||||
for _, trace := range c.Path {
|
for _, trace := range c.Path {
|
||||||
if trace.Flag == flag {
|
if trace.Flag == flag {
|
||||||
v, ok := c.values[trace.Flag.Value]
|
v, ok := c.values[trace.Flag.Value]
|
||||||
@@ -572,7 +572,7 @@ func (c *Context) Resolve() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pick the last resolved value.
|
// Pick the last resolved value.
|
||||||
var selected interface{}
|
var selected any
|
||||||
for _, resolver := range resolvers {
|
for _, resolver := range resolvers {
|
||||||
s, err := resolver.Resolve(c, path, flag)
|
s, err := resolver.Resolve(c, path, flag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -757,7 +757,7 @@ func (e *unknownFlagError) Unwrap() error { return e.Cause }
|
|||||||
func (e *unknownFlagError) Error() string { return e.Cause.Error() }
|
func (e *unknownFlagError) Error() string { return e.Cause.Error() }
|
||||||
|
|
||||||
// Call an arbitrary function filling arguments with bound values.
|
// Call an arbitrary function filling arguments with bound values.
|
||||||
func (c *Context) Call(fn any, binds ...interface{}) (out []interface{}, err error) {
|
func (c *Context) Call(fn any, binds ...any) (out []any, err error) {
|
||||||
fv := reflect.ValueOf(fn)
|
fv := reflect.ValueOf(fn)
|
||||||
bindings := c.Kong.bindings.clone().add(binds...).add(c).merge(c.bindings)
|
bindings := c.Kong.bindings.clone().add(binds...).add(c).merge(c.bindings)
|
||||||
return callAnyFunction(fv, bindings)
|
return callAnyFunction(fv, bindings)
|
||||||
@@ -769,7 +769,7 @@ func (c *Context) Call(fn any, binds ...interface{}) (out []interface{}, err err
|
|||||||
//
|
//
|
||||||
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
|
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
|
||||||
// all parent nodes in the command structure will be bound.
|
// all parent nodes in the command structure will be bound.
|
||||||
func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
|
func (c *Context) RunNode(node *Node, binds ...any) (err error) {
|
||||||
type targetMethod struct {
|
type targetMethod struct {
|
||||||
node *Node
|
node *Node
|
||||||
method reflect.Value
|
method reflect.Value
|
||||||
@@ -815,7 +815,7 @@ func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
|
|||||||
//
|
//
|
||||||
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
|
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
|
||||||
// all parent nodes in the command structure will be bound.
|
// all parent nodes in the command structure will be bound.
|
||||||
func (c *Context) Run(binds ...interface{}) (err error) {
|
func (c *Context) Run(binds ...any) (err error) {
|
||||||
node := c.Selected()
|
node := c.Selected()
|
||||||
if node == nil {
|
if node == nil {
|
||||||
if len(c.Path) == 0 {
|
if len(c.Path) == 0 {
|
||||||
@@ -1087,7 +1087,7 @@ func checkAndMissing(paths []*Path) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findPotentialCandidates(needle string, haystack []string, format string, args ...interface{}) error {
|
func findPotentialCandidates(needle string, haystack []string, format string, args ...any) error {
|
||||||
if len(haystack) == 0 {
|
if len(haystack) == 0 {
|
||||||
return fmt.Errorf(format, args...)
|
return fmt.Errorf(format, args...)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
package kong
|
package kong
|
||||||
|
|
||||||
// ApplyDefaults if they are not already set.
|
// ApplyDefaults if they are not already set.
|
||||||
func ApplyDefaults(target interface{}, options ...Option) error {
|
func ApplyDefaults(target any, options ...Option) error {
|
||||||
app, err := New(target, options...)
|
app, err := New(target, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Parse constructs a new parser and parses the default command-line.
|
// Parse constructs a new parser and parses the default command-line.
|
||||||
func Parse(cli interface{}, options ...Option) *Context {
|
func Parse(cli any, options ...Option) *Context {
|
||||||
parser, err := New(cli, options...)
|
parser, err := New(cli, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
@@ -386,7 +386,7 @@ func newHelpWriter(ctx *Context, options HelpOptions) *helpWriter {
|
|||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *helpWriter) Printf(format string, args ...interface{}) {
|
func (h *helpWriter) Printf(format string, args ...any) {
|
||||||
h.Print(fmt.Sprintf(format, args...))
|
h.Print(fmt.Sprintf(format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ var (
|
|||||||
callbackReturnSignature = reflect.TypeOf((*error)(nil)).Elem()
|
callbackReturnSignature = reflect.TypeOf((*error)(nil)).Elem()
|
||||||
)
|
)
|
||||||
|
|
||||||
func failField(parent reflect.Value, field reflect.StructField, format string, args ...interface{}) error {
|
func failField(parent reflect.Value, field reflect.StructField, format string, args ...any) error {
|
||||||
name := parent.Type().Name()
|
name := parent.Type().Name()
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = "<anonymous struct>"
|
name = "<anonymous struct>"
|
||||||
@@ -24,7 +24,7 @@ func failField(parent reflect.Value, field reflect.StructField, format string, a
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Must creates a new Parser or panics if there is an error.
|
// Must creates a new Parser or panics if there is an error.
|
||||||
func Must(ast interface{}, options ...Option) *Kong {
|
func Must(ast any, options ...Option) *Kong {
|
||||||
k, err := New(ast, options...)
|
k, err := New(ast, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -76,7 +76,7 @@ type Kong struct {
|
|||||||
// New creates a new Kong parser on grammar.
|
// New creates a new Kong parser on grammar.
|
||||||
//
|
//
|
||||||
// See the README (https://github.com/alecthomas/kong) for usage instructions.
|
// See the README (https://github.com/alecthomas/kong) for usage instructions.
|
||||||
func New(grammar interface{}, options ...Option) (*Kong, error) {
|
func New(grammar any, options ...Option) (*Kong, error) {
|
||||||
k := &Kong{
|
k := &Kong{
|
||||||
Exit: os.Exit,
|
Exit: os.Exit,
|
||||||
Stdout: os.Stdout,
|
Stdout: os.Stdout,
|
||||||
@@ -401,7 +401,7 @@ func (k *Kong) applyHookToDefaultFlags(ctx *Context, node *Node, name string) er
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...interface{}) {
|
func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...any) {
|
||||||
lines := strings.Split(strings.TrimRight(fmt.Sprintf(format, args...), "\n"), "\n")
|
lines := strings.Split(strings.TrimRight(fmt.Sprintf(format, args...), "\n"), "\n")
|
||||||
leader := ""
|
leader := ""
|
||||||
for _, l := range leaders {
|
for _, l := range leaders {
|
||||||
@@ -417,25 +417,25 @@ func formatMultilineMessage(w io.Writer, leaders []string, format string, args .
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Printf writes a message to Kong.Stdout with the application name prefixed.
|
// Printf writes a message to Kong.Stdout with the application name prefixed.
|
||||||
func (k *Kong) Printf(format string, args ...interface{}) *Kong {
|
func (k *Kong) Printf(format string, args ...any) *Kong {
|
||||||
formatMultilineMessage(k.Stdout, []string{k.Model.Name}, format, args...)
|
formatMultilineMessage(k.Stdout, []string{k.Model.Name}, format, args...)
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf writes a message to Kong.Stderr with the application name prefixed.
|
// Errorf writes a message to Kong.Stderr with the application name prefixed.
|
||||||
func (k *Kong) Errorf(format string, args ...interface{}) *Kong {
|
func (k *Kong) Errorf(format string, args ...any) *Kong {
|
||||||
formatMultilineMessage(k.Stderr, []string{k.Model.Name, "error"}, format, args...)
|
formatMultilineMessage(k.Stderr, []string{k.Model.Name, "error"}, format, args...)
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalf writes a message to Kong.Stderr with the application name prefixed then exits with a non-zero status.
|
// Fatalf writes a message to Kong.Stderr with the application name prefixed then exits with a non-zero status.
|
||||||
func (k *Kong) Fatalf(format string, args ...interface{}) {
|
func (k *Kong) Fatalf(format string, args ...any) {
|
||||||
k.Errorf(format, args...)
|
k.Errorf(format, args...)
|
||||||
k.Exit(1)
|
k.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FatalIfErrorf terminates with an error message if err != nil.
|
// FatalIfErrorf terminates with an error message if err != nil.
|
||||||
func (k *Kong) FatalIfErrorf(err error, args ...interface{}) {
|
func (k *Kong) FatalIfErrorf(err error, args ...any) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mustNew(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
|
func mustNew(t *testing.T, cli any, options ...kong.Option) *kong.Kong {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
options = append([]kong.Option{
|
options = append([]kong.Option{
|
||||||
kong.Name("test"),
|
kong.Name("test"),
|
||||||
@@ -1680,7 +1680,7 @@ func TestOptionReturnsErr(t *testing.T) {
|
|||||||
func TestEnumValidation(t *testing.T) {
|
func TestEnumValidation(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
cli interface{}
|
cli any
|
||||||
fail bool
|
fail bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -1954,7 +1954,7 @@ func TestVersionFlagShouldStillWork(t *testing.T) {
|
|||||||
func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
|
func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
cli interface{}
|
cli any
|
||||||
args []string
|
args []string
|
||||||
err string
|
err string
|
||||||
}{
|
}{
|
||||||
@@ -2004,7 +2004,7 @@ func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
|
|||||||
func TestMapDecoderHelpfulErrorMsg(t *testing.T) {
|
func TestMapDecoderHelpfulErrorMsg(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
cli interface{}
|
cli any
|
||||||
args []string
|
args []string
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
|
|||||||
@@ -251,7 +251,7 @@ func (r *Registry) RegisterType(typ reflect.Type, mapper Mapper) *Registry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RegisterValue registers a Mapper by pointer to the field value.
|
// RegisterValue registers a Mapper by pointer to the field value.
|
||||||
func (r *Registry) RegisterValue(ptr interface{}, mapper Mapper) *Registry {
|
func (r *Registry) RegisterValue(ptr any, mapper Mapper) *Registry {
|
||||||
key := reflect.ValueOf(ptr)
|
key := reflect.ValueOf(ptr)
|
||||||
if key.Kind() != reflect.Ptr {
|
if key.Kind() != reflect.Ptr {
|
||||||
panic("expected a pointer")
|
panic("expected a pointer")
|
||||||
@@ -473,7 +473,7 @@ func mapDecoder(r *Registry) MapperFunc {
|
|||||||
case string:
|
case string:
|
||||||
childScanner = ScanAsType(t.Type, SplitEscaped(v, mapsep)...)
|
childScanner = ScanAsType(t.Type, SplitEscaped(v, mapsep)...)
|
||||||
|
|
||||||
case []map[string]interface{}:
|
case []map[string]any:
|
||||||
for _, m := range v {
|
for _, m := range v {
|
||||||
err := jsonTranscode(m, target.Addr().Interface())
|
err := jsonTranscode(m, target.Addr().Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -482,7 +482,7 @@ func mapDecoder(r *Registry) MapperFunc {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case map[string]interface{}:
|
case map[string]any:
|
||||||
return jsonTranscode(v, target.Addr().Interface())
|
return jsonTranscode(v, target.Addr().Interface())
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -548,11 +548,11 @@ func sliceDecoder(r *Registry) MapperFunc {
|
|||||||
case string:
|
case string:
|
||||||
childScanner = ScanAsType(t.Type, SplitEscaped(v, sep)...)
|
childScanner = ScanAsType(t.Type, SplitEscaped(v, sep)...)
|
||||||
|
|
||||||
case []interface{}:
|
case []any:
|
||||||
return jsonTranscode(v, target.Addr().Interface())
|
return jsonTranscode(v, target.Addr().Interface())
|
||||||
|
|
||||||
default:
|
default:
|
||||||
v = []interface{}{v}
|
v = []any{v}
|
||||||
return jsonTranscode(v, target.Addr().Interface())
|
return jsonTranscode(v, target.Addr().Interface())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -922,7 +922,7 @@ func (f *FileContentFlag) Decode(ctx *DecodeContext) error { //nolint: revive
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func jsonTranscode(in, out interface{}) error {
|
func jsonTranscode(in, out any) error {
|
||||||
data, err := json.Marshal(in)
|
data, err := json.Marshal(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ func (n *Node) Leaf() bool {
|
|||||||
// Find a command/argument/flag by pointer to its field.
|
// Find a command/argument/flag by pointer to its field.
|
||||||
//
|
//
|
||||||
// Returns nil if not found. Panics if ptr is not a pointer.
|
// Returns nil if not found. Panics if ptr is not a pointer.
|
||||||
func (n *Node) Find(ptr interface{}) *Node {
|
func (n *Node) Find(ptr any) *Node {
|
||||||
key := reflect.ValueOf(ptr)
|
key := reflect.ValueOf(ptr)
|
||||||
if key.Kind() != reflect.Ptr {
|
if key.Kind() != reflect.Ptr {
|
||||||
panic("expected a pointer")
|
panic("expected a pointer")
|
||||||
|
|||||||
+7
-7
@@ -79,7 +79,7 @@ type dynamicCommand struct {
|
|||||||
help string
|
help string
|
||||||
group string
|
group string
|
||||||
tags []string
|
tags []string
|
||||||
cmd interface{}
|
cmd any
|
||||||
}
|
}
|
||||||
|
|
||||||
// DynamicCommand registers a dynamically constructed command with the root of the CLI.
|
// DynamicCommand registers a dynamically constructed command with the root of the CLI.
|
||||||
@@ -87,7 +87,7 @@ type dynamicCommand struct {
|
|||||||
// This is useful for command-line structures that are extensible via user-provided plugins.
|
// This is useful for command-line structures that are extensible via user-provided plugins.
|
||||||
//
|
//
|
||||||
// "tags" is a list of extra tag strings to parse, in the form <key>:"<value>".
|
// "tags" is a list of extra tag strings to parse, in the form <key>:"<value>".
|
||||||
func DynamicCommand(name, help, group string, cmd interface{}, tags ...string) Option {
|
func DynamicCommand(name, help, group string, cmd any, tags ...string) Option {
|
||||||
return OptionFunc(func(k *Kong) error {
|
return OptionFunc(func(k *Kong) error {
|
||||||
if run := getMethod(reflect.Indirect(reflect.ValueOf(cmd)), "Run"); !run.IsValid() {
|
if run := getMethod(reflect.Indirect(reflect.ValueOf(cmd)), "Run"); !run.IsValid() {
|
||||||
return fmt.Errorf("kong: DynamicCommand %q must be a type with a 'Run' method; got %T", name, cmd)
|
return fmt.Errorf("kong: DynamicCommand %q must be a type with a 'Run' method; got %T", name, cmd)
|
||||||
@@ -156,7 +156,7 @@ func KindMapper(kind reflect.Kind, mapper Mapper) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ValueMapper registers a mapper to a field value.
|
// ValueMapper registers a mapper to a field value.
|
||||||
func ValueMapper(ptr interface{}, mapper Mapper) Option {
|
func ValueMapper(ptr any, mapper Mapper) Option {
|
||||||
return OptionFunc(func(k *Kong) error {
|
return OptionFunc(func(k *Kong) error {
|
||||||
k.registry.RegisterValue(ptr, mapper)
|
k.registry.RegisterValue(ptr, mapper)
|
||||||
return nil
|
return nil
|
||||||
@@ -191,7 +191,7 @@ func Writers(stdout, stderr io.Writer) Option {
|
|||||||
// AfterApply(...) error
|
// AfterApply(...) error
|
||||||
//
|
//
|
||||||
// Called before validation/assignment, and immediately after validation/assignment, respectively.
|
// Called before validation/assignment, and immediately after validation/assignment, respectively.
|
||||||
func Bind(args ...interface{}) Option {
|
func Bind(args ...any) Option {
|
||||||
return OptionFunc(func(k *Kong) error {
|
return OptionFunc(func(k *Kong) error {
|
||||||
k.bindings.add(args...)
|
k.bindings.add(args...)
|
||||||
return nil
|
return nil
|
||||||
@@ -201,7 +201,7 @@ func Bind(args ...interface{}) Option {
|
|||||||
// BindTo allows binding of implementations to interfaces.
|
// BindTo allows binding of implementations to interfaces.
|
||||||
//
|
//
|
||||||
// BindTo(impl, (*iface)(nil))
|
// BindTo(impl, (*iface)(nil))
|
||||||
func BindTo(impl, iface interface{}) Option {
|
func BindTo(impl, iface any) Option {
|
||||||
return OptionFunc(func(k *Kong) error {
|
return OptionFunc(func(k *Kong) error {
|
||||||
k.bindings.addTo(impl, iface)
|
k.bindings.addTo(impl, iface)
|
||||||
return nil
|
return nil
|
||||||
@@ -212,11 +212,11 @@ func BindTo(impl, iface interface{}) Option {
|
|||||||
//
|
//
|
||||||
// The provider function must have the signature:
|
// The provider function must have the signature:
|
||||||
//
|
//
|
||||||
// func() (interface{}, error)
|
// func() (any, error)
|
||||||
//
|
//
|
||||||
// This is useful when the Run() function of different commands require different values that may
|
// This is useful when the Run() function of different commands require different values that may
|
||||||
// not all be initialisable from the main() function.
|
// not all be initialisable from the main() function.
|
||||||
func BindToProvider(provider interface{}) Option {
|
func BindToProvider(provider any) Option {
|
||||||
return OptionFunc(func(k *Kong) error {
|
return OptionFunc(func(k *Kong) error {
|
||||||
return k.bindings.addProvider(provider)
|
return k.bindings.addProvider(provider)
|
||||||
})
|
})
|
||||||
|
|||||||
+6
-6
@@ -14,15 +14,15 @@ type Resolver interface {
|
|||||||
Validate(app *Application) error
|
Validate(app *Application) error
|
||||||
|
|
||||||
// Resolve the value for a Flag.
|
// Resolve the value for a Flag.
|
||||||
Resolve(context *Context, parent *Path, flag *Flag) (interface{}, error)
|
Resolve(context *Context, parent *Path, flag *Flag) (any, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolverFunc is a convenience type for non-validating Resolvers.
|
// ResolverFunc is a convenience type for non-validating Resolvers.
|
||||||
type ResolverFunc func(context *Context, parent *Path, flag *Flag) (interface{}, error)
|
type ResolverFunc func(context *Context, parent *Path, flag *Flag) (any, error)
|
||||||
|
|
||||||
var _ Resolver = ResolverFunc(nil)
|
var _ Resolver = ResolverFunc(nil)
|
||||||
|
|
||||||
func (r ResolverFunc) Resolve(context *Context, parent *Path, flag *Flag) (interface{}, error) { //nolint: revive
|
func (r ResolverFunc) Resolve(context *Context, parent *Path, flag *Flag) (any, error) { //nolint: revive
|
||||||
return r(context, parent, flag)
|
return r(context, parent, flag)
|
||||||
}
|
}
|
||||||
func (r ResolverFunc) Validate(app *Application) error { return nil } //nolint: revive
|
func (r ResolverFunc) Validate(app *Application) error { return nil } //nolint: revive
|
||||||
@@ -31,12 +31,12 @@ func (r ResolverFunc) Validate(app *Application) error { return nil } //nolint:
|
|||||||
//
|
//
|
||||||
// Flag names are used as JSON keys indirectly, by tring snake_case and camelCase variants.
|
// Flag names are used as JSON keys indirectly, by tring snake_case and camelCase variants.
|
||||||
func JSON(r io.Reader) (Resolver, error) {
|
func JSON(r io.Reader) (Resolver, error) {
|
||||||
values := map[string]interface{}{}
|
values := map[string]any{}
|
||||||
err := json.NewDecoder(r).Decode(&values)
|
err := json.NewDecoder(r).Decode(&values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var f ResolverFunc = func(context *Context, parent *Path, flag *Flag) (interface{}, error) {
|
var f ResolverFunc = func(context *Context, parent *Path, flag *Flag) (any, error) {
|
||||||
name := strings.ReplaceAll(flag.Name, "-", "_")
|
name := strings.ReplaceAll(flag.Name, "-", "_")
|
||||||
snakeCaseName := snakeCase(flag.Name)
|
snakeCaseName := snakeCase(flag.Name)
|
||||||
raw, ok := values[name]
|
raw, ok := values[name]
|
||||||
@@ -47,7 +47,7 @@ func JSON(r io.Reader) (Resolver, error) {
|
|||||||
}
|
}
|
||||||
raw = values
|
raw = values
|
||||||
for _, part := range strings.Split(name, ".") {
|
for _, part := range strings.Split(name, ".") {
|
||||||
if values, ok := raw.(map[string]interface{}); ok {
|
if values, ok := raw.(map[string]any); ok {
|
||||||
raw, ok = values[part]
|
raw, ok = values[part]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
+7
-7
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
type envMap map[string]string
|
type envMap map[string]string
|
||||||
|
|
||||||
func newEnvParser(t *testing.T, cli interface{}, env envMap, options ...kong.Option) *kong.Kong {
|
func newEnvParser(t *testing.T, cli any, env envMap, options ...kong.Option) *kong.Kong {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
for name, value := range env {
|
for name, value := range env {
|
||||||
t.Setenv(name, value)
|
t.Setenv(name, value)
|
||||||
@@ -275,7 +275,7 @@ func TestResolverWithBool(t *testing.T) {
|
|||||||
Bool bool
|
Bool bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var resolver kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
|
var resolver kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (any, error) {
|
||||||
if flag.Name == "bool" {
|
if flag.Name == "bool" {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@@ -294,14 +294,14 @@ func TestLastResolverWins(t *testing.T) {
|
|||||||
Int []int
|
Int []int
|
||||||
}
|
}
|
||||||
|
|
||||||
var first kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
|
var first kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (any, error) {
|
||||||
if flag.Name == "int" {
|
if flag.Name == "int" {
|
||||||
return 1, nil
|
return 1, nil
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var second kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
|
var second kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (any, error) {
|
||||||
if flag.Name == "int" {
|
if flag.Name == "int" {
|
||||||
return 2, nil
|
return 2, nil
|
||||||
}
|
}
|
||||||
@@ -318,7 +318,7 @@ func TestResolverSatisfiesRequired(t *testing.T) {
|
|||||||
var cli struct {
|
var cli struct {
|
||||||
Int int `required`
|
Int int `required`
|
||||||
}
|
}
|
||||||
var resolver kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
|
var resolver kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (any, error) {
|
||||||
if flag.Name == "int" {
|
if flag.Name == "int" {
|
||||||
return 1, nil
|
return 1, nil
|
||||||
}
|
}
|
||||||
@@ -336,7 +336,7 @@ func TestResolverTriggersHooks(t *testing.T) {
|
|||||||
Flag hookValue
|
Flag hookValue
|
||||||
}
|
}
|
||||||
|
|
||||||
var first kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
|
var first kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (any, error) {
|
||||||
if flag.Name == "flag" {
|
if flag.Name == "flag" {
|
||||||
return "one", nil
|
return "one", nil
|
||||||
}
|
}
|
||||||
@@ -355,7 +355,7 @@ type validatingResolver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *validatingResolver) Validate(app *kong.Application) error { return v.err }
|
func (v *validatingResolver) Validate(app *kong.Application) error { return v.err }
|
||||||
func (v *validatingResolver) Resolve(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
|
func (v *validatingResolver) Resolve(context *kong.Context, parent *kong.Path, flag *kong.Flag) (any, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -41,7 +41,7 @@ func (t TokenType) String() string {
|
|||||||
|
|
||||||
// Token created by Scanner.
|
// Token created by Scanner.
|
||||||
type Token struct {
|
type Token struct {
|
||||||
Value interface{}
|
Value any
|
||||||
Type TokenType
|
Type TokenType
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +171,7 @@ func (s *Scanner) PopValue(context string) (Token, error) {
|
|||||||
// PopValueInto pops a value token into target or returns an error.
|
// PopValueInto pops a value token into target or returns an error.
|
||||||
//
|
//
|
||||||
// "context" is used to assist the user if the value can not be popped, eg. "expected <context> value but got <type>"
|
// "context" is used to assist the user if the value can not be popped, eg. "expected <context> value but got <type>"
|
||||||
func (s *Scanner) PopValueInto(context string, target interface{}) error {
|
func (s *Scanner) PopValueInto(context string, target any) error {
|
||||||
t, err := s.PopValue(context)
|
t, err := s.PopValue(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -204,13 +204,13 @@ func (s *Scanner) Peek() Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Push an untyped Token onto the front of the Scanner.
|
// Push an untyped Token onto the front of the Scanner.
|
||||||
func (s *Scanner) Push(arg interface{}) *Scanner {
|
func (s *Scanner) Push(arg any) *Scanner {
|
||||||
s.PushToken(Token{Value: arg})
|
s.PushToken(Token{Value: arg})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// PushTyped pushes a typed token onto the front of the Scanner.
|
// PushTyped pushes a typed token onto the front of the Scanner.
|
||||||
func (s *Scanner) PushTyped(arg interface{}, typ TokenType) *Scanner {
|
func (s *Scanner) PushTyped(arg any, typ TokenType) *Scanner {
|
||||||
s.PushToken(Token{Value: arg, Type: typ})
|
s.PushToken(Token{Value: arg, Type: typ})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user