457 Commits

Author SHA1 Message Date
S.Solodyagin 5e4be084f3 Merge tag 'v1.12.1' v1.12.1 2025-10-15 20:52:08 +03:00
Alec Thomas efa36914bb fix: don't require a Run() method on dynamic commands
Commands can be dispatched via string, so this is not necessary to constitute a command.
2025-07-21 13:22:05 +10:00
renovate[bot] 385f9b6d5a chore(deps): update all non-major dependencies (#542)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-07-14 11:53:16 +10:00
renovate[bot] f8bb69aa66 chore(deps): update all non-major dependencies (#540)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-07-04 18:08:25 +10:00
S.Solodyagin 575a0cdf6e Merge remote-tracking branch 'upstream/master' 2025-07-02 20:54:46 +03:00
Alec Thomas 8469b5cc3c chore: minor tweak for tinygo
There are still reflection issues, but it's a step.
2025-06-30 10:56:36 +10:00
Sam Xie fcd23468fe Update license of levenshtein method (#539) 2025-06-18 07:45:17 +10:00
renovate[bot] a521b46eaf chore(deps): update all non-major dependencies (#533)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-06-07 13:22:41 +10:00
Alec Thomas 9bc3bf9925 chore: optionally allow parsing of hyphen-prefixied flag parameters
This allows for eg. `foo --number -10`, `foo --flag -bar`.

Fixes #478, #315.
2025-05-15 19:31:29 +10:00
educhastenier 8e03dbeaf6 fix: broken internal link (#532)
Fix of minor broken link
2025-05-12 19:20:28 +10:00
renovate[bot] ebf6b70ab9 chore(deps): update all non-major dependencies (#531)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-05-09 18:01:36 +10:00
renovate[bot] e923ecc9ff chore(deps): update dependency lefthook to v1.11.11 (#530)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-04-28 19:55:25 +10:00
Sam Xie 66d5762b66 Support parsing integer literals (#529) 2025-04-24 08:24:47 +10:00
renovate[bot] f0b321097e chore(deps): update dependency lefthook to v1.11.10 (#527)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-04-21 13:02:03 +10:00
renovate[bot] 95c04ac28c chore(deps): update all non-major dependencies (#515)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-04-12 15:49:32 +10:00
Bob Lail 78d4066dab feat: Allow configuring global hooks via Kong's functional options (#511)
Lets you pass `kong.WithBeforeApply` along with a function that supports dynamic bindings to register a `BeforeApply` hook without tying it directly to a node in the schema.

Co-authored-by: Sutina Wipawiwat <swipawiwat@squareup.com>
2025-03-22 14:04:20 +11:00
renovate[bot] 1edf069f4a chore(deps): update all non-major dependencies (#506)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-03-17 08:28:07 +11:00
Chris Gunn 44be791798 feat: Placeholder string interpolation. (#510)
Add support string interpolation in placeholder values.
2025-03-13 11:49:21 +11:00
Alec Thomas 3d03233b16 refactor: ParseError can carry an exit code
Reinstated use of ParseError for all error paths so as to retain existing semantics.
2025-03-11 17:32:24 +11:00
Bob Lail a86adbbb25 feat: Allow Kong to exit with semantic exit codes (#507)
* feat: Allow Kong to exit with semantic exit codes

At Block, we've instrumented a number of commandline tools and set SLOs on some tools' reliability. To do that effectively, we had to partition usage errors from reliability issues. We looked at [prior art](https://github.com/square/exit?tab=readme-ov-file#reserved-codes-and-prior-art) and, taking inspiration from HTTP, defined [a set of semantic exit codes](https://github.com/square/exit?tab=readme-ov-file#about) in ranges: 80-99 for user errors, 100-119 for system errors.

We've been wrapping errors in `exit.Error` at whatever level of the stack can tell which class an error is and unwrapping them at exit (`os.Exit(exit.FromError(err))`).

This adds support for semantic exit codes to Kong, to `FatalIfErrorf`, which is used internally by `kong.Parse` and often used in Kong applications.

* feat: Exit 80 (Usage Error) when usage is syntactically or semantically invalid

* refactor: Always exit 80 (Usage Error) on a `ParseError` but don't wrap errors from hooks in `ParseError`
2025-03-11 17:21:09 +11:00
Alec Thomas 73db2e86a5 fix: ignore --help flag for determining optional flag usage
Fixes #508
2025-03-08 21:11:13 +11:00
Bob Lail 5b36573738 feat: Allow kong.Path to describe remaining unparsed args (#472)
As Kong traces a sequence of command line arguments, it parses them and appends them to the parsed `Path` sequence. For each element in `Path`, these is a corresponding sequence of unparsed arguments. This change enables `Path` to yield these.

I have a package that uses Kong's hooks to instrument Kong applications (to monitor usage, reliability, etc of internal tools). I would like to instrument the commandline arguments as well.

This change would enable it to work roughly as follows:
```golang
func (Foo) BeforeApply(app *kong.Kong, ctx *kong.Context, t *Tracker) error {
	command := []string{ctx.Model.Name}
	var args  []string

	for _, path := range ctx.Path {
		if path.Command != nil {
			command = append(command, path.Command.Name)
			args = path.Remainder()
		}
	}

	app.Exit = t.exit(app.Exit)

	t.WithCommand(strings.Join(command, " ")).WithArgs(args)
	return nil
}
```
2025-03-08 09:28:09 +11:00
Alec Thomas 0c495e4936 feat: add IgnoreDefault optional interface 2025-02-26 14:37:56 +11:00
Cam Hutchison 300cba8c27 feat: Allow ignoring fields from embedded structs (#499)
If a field in a struct is ignored with `kong:"-"`, any embedded fields
with the same name are also ignored. This allows an outer struct to
remove flags from an embedded struct by redefining it and adding a kong
ignore tag.
2025-02-17 20:06:02 +11:00
Abhinav Gupta 3b9af5bdce feat: Support singleton providers (#501)
* feat: Support singleton providers

This change adds support for provider functions that are
not reinvoked even if requested by multiple other providers.
Instead, their value is cached and reused between invocations.

To make this possible, we change how bindings are stored:
instead of just a function reference, we now store a binding object
which records whether the binding is a singleton,
and records the resolved singleton value (if any).

Resolves #500

* refac(bindings): hide singleton status

Don't require callAnyFunction to be aware of
whether a binding is a singleton or not.
2025-02-17 17:44:03 +11:00
renovate[bot] 7f94c902b9 chore(deps): update module github.com/alecthomas/kong to v1.8.1 (#503)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-02-17 15:41:24 +11:00
Abhinav Gupta 9f71a49767 ci: Test with Go 1.23 and 1.24 (#502)
In CI, test with Go 1.23 and 1.24,
and upgrade the Hermit-managed Go and golangci-lint to latest versions.

The new golangci-lint had a number of warnings and minor issues
that were either fixed or opted-out of.
2025-02-17 14:10:57 +11:00
Alec Thomas cab639ab83 chore: add test for decoding 2025-02-13 11:22:03 -08:00
Alec Thomas 7747b4146b Revert "Load environment variables as a resolver (#480)"
This reverts commit 3cedc44821.

Fixes #497, #498
2025-02-13 11:17:47 -08:00
Maxime Vidori 3cedc44821 Load environment variables as a resolver (#480) 2025-02-10 09:53:27 +11:00
renovate[bot] 6590294c3d chore(deps): update all non-major dependencies (#486)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-02-08 22:24:06 +11:00
Alec Thomas 705e259cd6 chore: reduce lefthook output 2025-01-30 14:11:42 +11:00
Alec Thomas a6efd403cc chore: add lefthook 2025-01-30 14:10:48 +11:00
Alec Thomas 5765c11522 refactor: minor simplification of getMethods 2025-01-30 14:08:44 +11:00
Abhinav Gupta 4be6ae6168 hooks: Recursively search embedded fields for methods (#494)
* hooks: Recursively search embedded fields for methods

Follow up to #493 and 840220c

Kong currently supports hooks on embedded fields of a parsed node,
but only at the first level of embedding:

```
type mainCmd struct {
    FooOptions
}

type FooOptions struct {
    BarOptions
}

func (f *FooOptions) BeforeApply() error {
    // this will be called
}

type BarOptions struct {
}

func (b *BarOptions) BeforeApply() error {
    // this will not be called
}
```

This change adds support for hooks to be defined
on embedded fields of embedded fields so that the above
example would work as expected.

Per #493, the definition of "embedded" field is adjusted to mean:

- Any anonymous (Go-embedded) field that is exported
- Any non-anonymous field that is tagged with `embed:""`

*Testing*:
Includes a test case for embedding an anonymous field in an `embed:""`
and an `embed:""` field in an anonymous field.

* Use recursion to build up the list of receivers

The 'receivers' parameter helps avoid constant memory allocation
as the backing storage for the slice is reused across recursive calls.
2025-01-30 13:43:10 +11:00
Alec Thomas 4e1757c0e8 feat: allow use of providers that don't return errors 2025-01-30 13:41:09 +11:00
Abhinav Gupta 9c08a58eb2 Support hooks on embed:"" fields (#493)
Relates to 840220c (#90)

This change adds support for hooks to be called on fields
that are tagged with `embed:""`.

### Use case

If a command has several subcommands,
many (but not all) of which need the same external resource,
this allows defining the flag-level inputs for that resource centrally,
and then using `embed:""` in any command that needs that resource.

For example, imagine:

```go
type githubClientProvider struct {
    Token string `name:"github-token" env:"GITHUB_TOKEN"`
    URL   string `name:"github-url" env:"GITHUB_URL"`
}

func (g *githubClientProvider) BeforeApply(kctx *kong.Context) error {
  return kctx.BindToProvider(func() (*github.Client, error) {
    return github.NewClient(...), nil
  })
}
```

Then, any command that needs GitHub client will add this field,
any other resource providers it needs,
and add parameters to its `Run` method to accept those resources:

```go
type listUsersCmd struct {
    GitHub githubClientProvider `embed:""`
    S3     s3ClientProvider     `embed:""`
}

func (l *listUsersCmd) Run(gh *github.Client, s3 *s3.Client) error {
    ...
}
```

### Alternatives

It is possible to do the same today if the `*Provider` struct above
is actually a Go embed instead of a Kong embed, *and* it is exported.

```
type GitHubClientProvider struct{ ... }

type listUsersCmd struct {
    GithubClientProvider
    S3ClientProvider
}
```

The difference is whether the struct defining the flags
is required to be exported or not.
2025-01-29 16:04:52 +11:00
Hritik Vijay 042a3258ec Document about binds with example (#491) 2025-01-25 10:51:46 +11:00
renovate[bot] 8d238c88fa chore(deps): update all non-major dependencies (#485)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-01-13 22:13:14 +11:00
Abhinav Gupta 7ca846736c fix(Context.Run): Don't panic on unselected root node (#484) 2025-01-04 11:13:14 +09:00
Alec Thomas b811e32243 fix: whoops, forgot to prefix "and" 2024-12-29 18:02:02 +09:00
Alec Thomas 47b090f2f4 fix: add an xorprefix:"..." option for prefixing xor/and groups
Fixes #343
2024-12-29 17:55:06 +09:00
Alec Thomas cacaace969 fix: don't append ... for fields with an explicit type
Fixes #346
2024-12-29 17:42:20 +09:00
Alec Thomas a32b94b705 chore: interface{} -> any 2024-12-29 08:10:34 +09:00
Alec Thomas 36257680f1 refactor(test): replace os.Setenv() with t.Setenv() 2024-12-29 08:02:45 +09:00
Alec Thomas a14bb2072c fix: don't call Apply() twice
For some reason this was called by `Run()`. All tests pass without it,
so I'm not sure why it was there.

Fixes #481
2024-12-29 07:43:05 +09:00
Alec Thomas 840220c2ed feat: allow hooks to be declared on embedded fields
Specifically, on Go embedded fields, not on fields tagged with `embed`.

Fixes #90.
2024-12-27 22:29:45 +09:00
renovate[bot] 565ae9b740 chore(deps): update all non-major dependencies (#476)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-12-12 22:32:41 +11:00
Alec Thomas f2fcd34abf docs: fix README TOC 2024-12-07 19:43:01 +11:00
Alec Thomas 388ba35f1a feat: add support for Provide*() (<type>, error) methods on commands 2024-12-07 17:09:03 +11:00