49 Commits

Author SHA1 Message Date
S.Solodyagin 575a0cdf6e Merge remote-tracking branch 'upstream/master' 2025-07-02 20:54:46 +03: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
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
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
Alec Thomas 2ad9498bdf chore: bump go + golangci-lint 2024-08-20 08:16:24 +10:00
s.solodyagin 6795fa089a rename 2024-04-03 20:59:37 +03:00
Dennis Ploeger dc13080e39 feat: Add info about optional flags to usage (#416) 2024-02-27 19:39:21 +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
Denis Titusov 9610ed62d9 feat: support multiple env variables (#349) 2023-02-01 06:39:30 +11:00
Michal Kralik 3371bd6167 fix: go1.19 specific tests for text wrapping 2022-08-26 18:53:26 +10:00
Alec Thomas e75e1ca88a refactor: switch to alecthomas/assert 2022-06-21 20:58:10 +10:00
Alec Thomas 7a63525420 feat: AutoGroup option 2022-03-11 22:51:12 +11:00
Alec Thomas c5e464a367 Deprecate HelpFormatter. 2021-12-03 11:48:54 +11:00
Connor Hindley 5090305bcb Add help testcase for envprefix + no env. 2021-11-19 06:22:36 +11:00
Dan 2770a34ce6 Add envprefix tag 2021-10-22 16:25:13 +11:00
Nikita Semikov f547618963 Add aliases to ALL help output (#191) 2021-08-07 22:04:51 +10:00
Kartikaya Gupta 2d879d2037 Add an option to clamp the help wrap width
This is nice to have because on really wide screens the default
terminal-width wrapping can be a little unpleasant to read.
2021-06-16 23:06:43 +10:00
Alec Thomas 6cebaa7b85 Hermitise and bump golangci-lint. 2021-06-09 12:48:47 +10:00
Alec Thomas 93f73cf38c Display multiple optional args in a more expected way.
Previously:

    Usage: command [<one>] [<two>]

Now:

    Usage: command [<one> [<two>]]

Thanks to @juliaogris for the inspiration!
2021-03-13 20:30:28 +11:00
Dustin Spicuzza 09467435e1 Add option to not expand subcommand help 2021-03-13 16:41:08 +11:00
Joe Schmitt d48f4e59c3 Add negatable output when using short flag names 2021-03-02 10:36:13 +11:00
Joe Schmitt 2fdddc4f51 Add negatable tag to set a bool to be negatable.
If negatable, add `--[no-]` prefix to help.
2021-03-02 10:36:13 +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
Joe Schmitt f306ae1529 Add aliases to tree help output 2021-02-13 06:37:48 +11:00
Jaco Coetzee 405b2f4fd9 Help Option to position flags in help output
When FlagsLast is set to true, flags are printed after commands.

- Added HelpOptions.FlagsLast bool
- Added test TestFlagsLast
2021-02-09 19:25:17 +11:00
Alec Thomas abbc2dfc2c More group formatting tweaks. 2021-02-09 15:23:05 +11:00
Alec Thomas 294d1d1717 Add a bit of extra spacing if group description is provided.
See #135.
2021-02-09 15:17:29 +11:00
Alec Thomas 7e27c8ecf5 Support a simplified kong.Groups{<key>: <title>} way of specifiying groups.
See #135.
2021-02-09 15:08:22 +11:00
Mickaël Menu b68e1aba63 Add support for groups in the default HelpPrinter (#135) 2021-02-09 06:58:43 +11:00
Cam Hutchison b11ebc500b Fix help tests with the introduction of -h
Commit c77deac080 in PR #83 added `h` as
the short flag for help, but the help tests were not updated. Update the
tests with the `-h` in the help output.
2020-06-01 22:25:13 +10:00
Rene Zbinden 7b000bd775 Add HelpValueFormatter as discussed in #60 (#61)
Fixes #60 .
2020-02-21 20:24:19 +11:00
Alec Thomas 42ea64b1b4 Factor out command help into functions. 2019-01-24 17:14:31 +11:00
Matthias Fax 2a90ca2ad3 feat: add a command tree help view (#32)
Add a command tree help view.

Following up #29 and #30, I noticed that the default help looks quite duplicative since only the help text of the last command in chain is viewed. I needed an option to also show the help text of the command nodes in an appropriate way.  This is what I came up with.
2019-01-22 00:14:26 +11:00
Alec Thomas 886c2d7b01 Better help for maps. 2019-01-12 20:59:24 +11:00
Alec Thomas 6406edf15f Add Visitor function for walking the model. 2018-09-21 09:47:55 +10:00
Alec Thomas f0bd1294a7 Support detailed help.
Any command/arg implementing the HelpProvider interface will be used to
provide arbitrarily detailed help.
2018-08-14 14:43:55 +10:00
Alec Thomas 6408010083 Clean up disparity between Context and Kong.
Previously, there was a confusing mix of functionality shared between
the two wherein you would need to use the Kong type for printing errors,
etc. but it did not have access to the context in order to print
context-sensitive usage information. This has been fixed.

Additionally, there are now fuzzy correction suggestions for flags and
commands

Also added a server example which shows how Kong can be used for parsing
in interactive shells. Run with:

    $ go run ./_examples/server/*.go

Then interact with:

    $ ssh -p 6740 127.0.0.1
2018-06-27 09:11:11 +10:00
Alec Thomas a2ec050947 Implement a robust Context.Run().
This helps when composing large applications from separate command
structs.
2018-06-21 21:50:30 +10:00
Alec Thomas 212ea2a356 Add path, existingfile and existingdir types.
- Document custom types.
- Add docker example.
2018-06-21 16:19:08 +10:00
Alec Thomas 653531d6bc Start making help slightly configurable. 2018-06-20 21:55:39 +10:00
Alec Thomas ecf21e4cc9 Add support for maps. 2018-06-13 22:17:28 +10:00
Alec Thomas 29fe92f286 Only reset grammar when Apply()ing.
Also add a Kong.Help() function for writing context-sensitive help.
2018-06-13 12:10:04 +10:00
Alec Thomas e9d88d6528 Implement flag "resolvers". (#24)
* Propagate errors.
* Use junit test output.
* Expand role of DecodeContext to include Scanner.
* Inject resolved flags as Path elements in the Context.
  This allows all existing logic to apply seamlessly: hooks, required
flags, etc.
* Clarify that hooks can be called multiple times.
2018-06-12 07:20:55 +10:00
Alec Thomas 73064a687f Add <command> to help when a sub-command is required. 2018-06-08 15:20:58 +10:00
Alec Thomas d30421d2b1 Get rid of troublesome [<flags>]. 2018-06-08 15:02:14 +10:00
Alec Thomas 96fa9c43d5 Improved documentation and help. 2018-06-05 11:36:51 +10:00
Alec Thomas 2afd4ba47b Help! 2018-06-04 13:55:38 +10:00
Alec Thomas fdc7230e22 Separate validation into a distinct step.
This allows help to be called even when the parse trace is invalid.
Without this, the command-line would have to be valid in order to use
help at all, which defeats the purpose.
2018-05-31 15:20:45 +10:00