150 Commits

Author SHA1 Message Date
S.Solodyagin 575a0cdf6e Merge remote-tracking branch 'upstream/master' 2025-07-02 20:54:46 +03: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
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
Chris Gunn 44be791798 feat: Placeholder string interpolation. (#510)
Add support string interpolation in placeholder values.
2025-03-13 11:49:21 +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
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 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
Abhinav Gupta 7ca846736c fix(Context.Run): Don't panic on unselected root node (#484) 2025-01-04 11:13:14 +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
Alec Thomas 388ba35f1a feat: add support for Provide*() (<type>, error) methods on commands 2024-12-07 17:09:03 +11:00
Alec Thomas 96647c30af feat: add old "passthrough" behaviour back in as an option
`passthrough:""` or `passthrough:"all"` (the default) will pass through
all further arguments including unrecognised flags.

`passthrough:"partial"` will validate flags up until the `--` or the
first positional argument, then pass through all subsequent flags and
arguments.
2024-12-01 20:02:03 +11:00
Alec Thomas 2544d3f008 feat: add AfterRun() hook
Fixes #288
2024-11-03 14:49:27 +11:00
Alec Thomas d0beaf7df3 fix: update enum+default vars after interpolation
This doesn't recursively apply interpolation, as discussed in the issue,
but that's a much bigger change.

Fixes #337
2024-11-03 14:28:27 +11:00
Alec Thomas 1b9d57eec1 feat: support optionally passing kong.Context to Validate()
Fixes #340
2024-11-03 14:14:55 +11:00
Alec Thomas 64229c9fe7 fix: format enum value
Fixed in #415
2024-11-03 14:02:03 +11:00
Alec Thomas 26c1c9ad05 fix: allow duplicate custom negated flags
Fixes #456
2024-09-12 12:05:09 +10:00
Bob Lail 9924ec4461 fix!: Include -- in passthrough args (#436)
Given a grammar like this:

```golang
var cli struct {
        Args []string `arg:"" optional:"" passthrough:""`
}
```

If Kong parses `cli foo -- bar`, it will populate `Args` with `[]string{"foo", "--", "bar"}` (including "`--`").
However, if Kong parses `cli -- foo bar`, will populate `Args` with `[]string{"foo", "bar"}` (leaving off `"--"`).

This differs from the behavior of a passthrough Command, where `"--"` is included with the args in both cases.

There are 3 places where `c.endParsing()` is called
1. When `node.Passthrough` is true: https://github.com/alecthomas/kong/blob/5f9c5cc822bdb888a3671c44d4688a6f602ecb90/context.go#L366-L368
2. When `arg.Passthrough` is true: https://github.com/alecthomas/kong/blob/5f9c5cc822bdb888a3671c44d4688a6f602ecb90/context.go#L451-L453
3. When `"--"` is encountered: https://github.com/alecthomas/kong/blob/5f9c5cc822bdb888a3671c44d4688a6f602ecb90/context.go#L384-L387

The first two do not also pop any tokens. The third one does.

This commit makes `c.scan.Pop()` conditional, skipping it when the next positional argument is passthrough.

I believe this will cause Kong to behave a little more consistently — and from my perspective, `--` is relevant for args intended to be passed through! — but it will change the behavior of existing projects that use `arg:"" passthrough:""`.
2024-09-11 10:51:37 +10:00
Cam Hutchison 4ecb53599b Make negatable flag name customisable (#439)
* fix: Check if negatable duplicates another flag

Add a check for flags with the `negatable` option if the negative flag
conflicts with another tag, such as:

    Flag   bool `negatable:""`
    NoFlag bool

The flag `--no-flag` is ambiguous in this scenario.

* feat: Make negatable flag name customisable

Allow a value on the `negatable` tag to specify a flag name to use for
negation instead of using `--no-<flag-name>` as the flag.

e.g.

    Approve bool `default:"true",negatable:"deny"`

This example will allow `--deny` to set the `Approve` field to false.
2024-09-10 21:02:37 +10:00
Camilla 7d84b95294 Feature: Add check for overlapping xor and and groups (#443)
* Docs: Clean and group description

* Feat: Add check for overlapping xor and and groups

Co-authored-by: inful <jone.marius@vign.es>

* Chore: Rewrite overlap err to avoid duplicated words

---------

Co-authored-by: inful <jone.marius@vign.es>
2024-09-10 21:01:12 +10:00
Alec Thomas 2ad9498bdf chore: bump go + golangci-lint 2024-08-20 08:16:24 +10:00
Camilla ff6d5ba7d5 Feature: Add xand tag (#442)
* Feat: Add xand group and check for missing

* Fix: Split and combine err in TestMultiand for consistency

* Feat: Check missing required flags in xand groups

* Feat: Handle combined xor and xand

* Docs: Add info about combined xand and required use

* Docs: Fix language error in xand description

Co-authored-by: Stautis <thkrst@gmail.com>

* Feat: Rename xand to and

* Refactor: Switch from fmt.Sprintf to err.Error

* Refactor: Get requiredAndGroup map in separate function

---------

Co-authored-by: Stautis <thkrst@gmail.com>
2024-08-08 16:58:22 +10:00
Mitar 5f9c5cc822 Remove extra newline when message contains trailing newlines. (#387) 2024-07-05 22:52:45 +10:00
Leo Antunes e864bb0220 feat: allow non-structs to be used as commands (#428)
* feat: allow non-structs to be used as commands

This small MR allows using the func-to-interface trick to implement a command (see commandFunc in kong_test.go).

This is useful e.g. for commands that have no flags or arguments of their own, but instead receive all required information via bound  parameters.

* fix: check DynamicCommand is runnable when adding
2024-07-05 22:51:38 +10:00
Bob Lail fcb5e05c07 fix: When a Grammar combines flags with passthrough args, see if an unrecognized flag may be treated as a positional argument (#435)
* ci: Add a test for positional args that are passthrough on a command that isn't passthrough

* fix: When a Grammar combines flags with passthrough args, see if an unrecognized flag may be treated as a positional argument

Given a grammar like this:
```golang
var cli struct {
	Args []string `arg:"" optional:"" passthrough:""`
}
```

The first positional argument implies that it was preceded by `--`, so subsequent flags are not parsed.

If Kong parses `cli 1 --unknown 3`, it will populate `Args` with `[]string{"1", "--unknown", "3"}`.
However, if Kong parses `cli --unknown 2 3`, it will fail saying that `--unknown` is an unrecognized flag.

This commit changes the parser so that if an unknown flag _could_ be treated as the first passthrough argument, it is.

After this change, if Kong parses `cli --unknown 2 3`, it will populate `Args` with `[]string{"--unknown", "2", "3"}`.

* ci: Skip the `maintidx` linter for `trace()`
2024-07-05 22:48:33 +10:00
s.solodyagin 6795fa089a rename 2024-04-03 20:59:37 +03:00
Adam Lesperance 958a344920 Aliases can be duplicated between different sub commands (#419)
Make sure we remove all the aliases from the seenFlags tracker just like we do with other flags/short flags
2024-03-15 06:17:27 +11:00
Dennis Ploeger dc13080e39 feat: Add info about optional flags to usage (#416) 2024-02-27 19:39:21 +11:00
Prashant Varanasi fa9b636997 Support aliases for flags (#409)
Aliases are currently only supported for sub-commands, but they're
useful for flags as well. E.g., when migrating from an old flag name
to a new flag name, while still supporting the old value.
2024-02-27 11:10:15 +11:00
Abhinav Gupta a86bda490b golangci-lint: Upgrade, fix issues (#397)
The golangci-lint being used was quite dated.
This change upgrades to the latest version.
Adds and updates exclusions based on new failures.

Note on revive:
I've included an opt-out for unused parameters for revive
because turning `newThing(required bool)` to `newThing(_ bool)`
is a loss of useful information.

The changes to the Go files are to fix the following issues:

```
camelcase.go:16: File is not `gofmt`-ed with `-s` (gofmt)
config_test.go:50:18: directive `//nolint: gosec` is unused for linter "gosec" (nolintlint)
defaults_test.go:28:25: G601: Implicit memory aliasing in for loop. (gosec)
doc.go:5: File is not `gofmt`-ed with `-s` (gofmt)
kong.go:446:18: directive `//nolint: gosec` is unused for linter "gosec" (nolintlint)
kong_test.go:503:20: G601: Implicit memory aliasing in for loop. (gosec)
model.go:493:10: composites: reflect.ValueError struct literal uses unkeyed fields (govet)
scanner.go:112: File is not `gofmt`-ed with `-s` (gofmt)
```

And to address broken nolint directives reported as follows by
golangci-lint.

```
[.. skipped .. ]
tag.go:65:1: directive `// nolint:gocyclo` should be written without leading space as `//nolint:gocyclo` (nolintlint)
tag.go:206:51: directive `// nolint: gocyclo` should be written without leading space as `//nolint: gocyclo` (nolintlint)
util_test.go:23:43: directive `// nolint: errcheck` should be written without leading space as `//nolint: errcheck` (nolintlint)
util_test.go:51:22: directive `// nolint: errcheck` should be written without leading space as `//nolint: errcheck` (nolintlint)
```
2023-12-11 09:37:07 +11:00
Alec Thomas 815ba68265 Revert "fix: set trailing positional arguments to active"
This reverts commit 575d5b1d34.
2023-12-10 08:21:36 +11:00
Alec Thomas 575d5b1d34 fix: set trailing positional arguments to active
This fixes an issue where existingfile et al would not correctly apply
to positional arguments with defaults.
2023-11-17 16:56:45 +11:00
Jan Heuermann 93d31e17f4 Drop automatic message prefix from command errors (#384) 2023-10-07 18:15:33 +11:00
Mitar 074ccd0906 fix: remove showing app name twice in errors (#368) 2023-07-21 22:08:56 +10:00
Denis Titusov 9610ed62d9 feat: support multiple env variables (#349) 2023-02-01 06:39:30 +11:00
Florian Loch 37e801405f fix: handle contents of tags properly by unquoting them when necessary 2023-01-26 03:52:22 +11:00
Alec Thomas 95a465b4b5 feat: add FlagNamer option
Fixes #347
2023-01-13 09:34:44 -05:00
Alec Thomas 8b826182d8 fix: tests 2022-09-21 07:33:27 +10:00
Jacob Hochstetler 0c6a9f3a3d Merging issue 280 with master changes (#296) 2022-09-20 05:55:05 -07:00
Michal Kralik 9c8b401de0 Cumulative argument needs to be last (#331) 2022-09-20 05:47:41 -07:00
pyqlsa f48da244f5 change VersionFlag to a BeforeReset hook 2022-07-28 16:15:07 +10:00
Michal Kralik f9bc630ef8 Panic on duplicate command names (#317) 2022-07-17 02:31:21 -07:00
Alec Thomas e75e1ca88a refactor: switch to alecthomas/assert 2022-06-21 20:58:10 +10:00
Alec Thomas 0066abb973 fix: Reset() should always be called before anything else 2022-06-15 20:45:10 +10:00