112 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
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 0c495e4936 feat: add IgnoreDefault optional interface 2025-02-26 14:37:56 +11: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
Alec Thomas a32b94b705 chore: interface{} -> any 2024-12-29 08:10:34 +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 88e13d750a chore: fix all golangci-lint warnings 2024-12-01 11:12:48 +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
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
Mitar 5f9c5cc822 Remove extra newline when message contains trailing newlines. (#387) 2024-07-05 22:52:45 +10:00
s.solodyagin 6795fa089a rename 2024-04-03 20:59:37 +03: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
Jan Heuermann 93d31e17f4 Drop automatic message prefix from command errors (#384) 2023-10-07 18:15:33 +11:00
Denis Titusov 9610ed62d9 feat: support multiple env variables (#349) 2023-02-01 06:39:30 +11:00
Alec Thomas 95a465b4b5 feat: add FlagNamer option
Fixes #347
2023-01-13 09:34:44 -05:00
Alec Thomas bf0cbf5d7c feat: Embed() option and Context.Call()
The former allows arbitrary structs to be embedded in the root of the
CLI, with optional tags.

The latter allows an arbitrary function to be called using Kong's
binding functionality.
2022-11-22 23:34:56 +11: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
pyq-lsa 5538b7f045 change help hook and call Reset later
change to BeforeResolve hook to catch calls to '--help' earlier;
call reset later in Kong.Parse() to allow help hook to get hooked;
add test;
2022-05-03 00:10:19 -07:00
Denis Titusov 89b2806f6a Switch to the standard errors API, that was introduced in 1.13 (#273)
* Switch to the standard errors API, that was introduced in 1.13

* Fix linter errors 🤦‍

* Remove withStackError

* fix: freeze go version to 1.17, since 1.18 introduced generics, which are not supported by linters yet

* fix: freeze go version to 1.17, since 1.18 introduced generics, which are not supported by linters yet
2022-03-18 18:54:29 +11:00
Alec Thomas 7533b11d06 Differentiate between omitted and empty default.
Fixes #250.
2021-12-13 19:03:23 +11:00
Mitar deebf0b09b Do not add environment variable to help it is already present.
Fixes #246.
2021-12-05 06:23:18 +11:00
Colin ff839dc6db Fix env var interpolation (#236) 2021-11-24 13:57:08 +11:00
Colin 79f896f0c0 feat: add support for variable interpolation into env tag key (#234) 2021-11-19 08:56:39 +11:00
Alec Thomas 5c7b038540 Rename IgnoreFieldsRegex -> IgnoreFields. 2021-09-27 16:31:54 +10:00
Alec Thomas 1d48b6f720 Allow Mappers to contribute Vars to interpolation.
See #218.
2021-09-27 16:07:48 +10:00
Daniel Selans 07faa2e0d0 Add ability to ignore fields that match given regex pattern(s) (#199) 2021-08-28 15:41:08 +10:00
Simon Frei 5a9c9c7864 Bubble errors instead of panicking (#194) 2021-08-19 18:00:53 +10:00
Alec Thomas d1a818b5a1 Allow DynamicCommand to specify arbitrary tags.
Fixes #185.
2021-07-13 13:55:01 +10:00
Alec Thomas 247574041d Enum fields must be required or have a default.
This is a breaking change, but the previous behaviour was broken so I'm
not concerned.

Also made most programmer errors more useful by giving type.field
context information.

Fixes #179.
2021-06-21 20:35:41 +09:30
Alec Thomas 6cebaa7b85 Hermitise and bump golangci-lint. 2021-06-09 12:48:47 +10:00
Alec Thomas 49417fe966 Add support for dynamically registered commands. 2021-04-02 11:38:24 +11:00
Julia Ogris 1300b2a3bd options: Add kong.ShortUsageOnError() option
Add kong.ShortUsageOnError() option similar to kong.UsageOnError().
Add tests for UsageOnError and ShortUsageOnError.

Remove the HelpOption summary reset at the beginning of DefaultHelpPrinter:

	 func DefaultHelpPrinter(options HelpOptions, ctx *Context) error {
	-       if ctx.Empty() {
	-               options.Summary = false
	-       }

⚠️ I'm not really sure what the implications of this are, tests still
seem to pass, but maybe this has unintended side effects.
2021-02-16 16:25:07 +11:00
Mickaël Menu b68e1aba63 Add support for groups in the default HelpPrinter (#135) 2021-02-09 06:58:43 +11:00
Alec Thomas 097bba54ec Revert UsageOnMissing.
This was breaking UsageOnMissing() when combined with validation of
positional arguments, in that it was allowing positional arguments to be
omitted. It also broke UsageOnMissing() somehow so that it did not work.
2020-10-27 17:33:46 +11:00
hasheddan a062611ecf Do not run parent node on missing child err with usageOnMissing option
Refactors usageOnMissing option to not run parent commands after
printing usage when a child node is missing.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
2020-10-20 09:44:46 +11:00
Sven Nebel 5b3fd5476a Refactor k.FatalIfErrorf to use k.Fatalf 2020-10-11 07:44:14 +11:00
hasheddan d1821a0666 Add UsageOnMissing option to Kong
Adds an UsageOnMissing option which configures Kong to omit errors and
display usage when a command is invalid due to a missing argument. This
can be useful if a user wishes for the top-level command to print usage
when no args are supplied, or if they would prefer child commands which
have their own subcommands to print usage specific to that command.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
2020-09-11 17:10:18 -07:00
Dima Kotik c77deac080 Added -h as default short parameter for the automatic --help flag. 2020-06-01 21:22:47 +10:00
Will Roden 7bdb319cf1 move CloneWith() call into interpolate() 2020-05-16 09:35:03 +10:00
Will Roden f19cb8c69d factor CloneWith out of most interpolate cases 2020-05-16 09:35:03 +10:00
Alec Thomas ed7caf6841 Revert "Convert env management to a resolver."
This reverts commit fbe8d48e4b.
2020-04-30 17:03:21 +10:00
Alec Thomas fbe8d48e4b Convert env management to a resolver.
This ensures that hooks correctly detect when values are set via
environment variables. Fixes #73.
2020-04-16 22:20:43 +10:00
Alec Thomas 109dce7ea5 Expand variables in config paths. 2020-03-05 11:27:53 +11:00
Rene Zbinden 7b000bd775 Add HelpValueFormatter as discussed in #60 (#61)
Fixes #60 .
2020-02-21 20:24:19 +11:00