87 Commits

Author SHA1 Message Date
Alec Thomas 73db2e86a5 fix: ignore --help flag for determining optional flag usage
Fixes #508
2025-03-08 21:11:13 +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 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 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
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
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 0ecd272e23 fix: use LookupEnv to check if envar is actually set
Fixes #389
2023-11-14 09:37:35 +11:00
Denis Titusov 9610ed62d9 feat: support multiple env variables (#349) 2023-02-01 06:39:30 +11:00
Alec Thomas 9b08b89396 fix: ensure pointers can be detected as bools
This required adding a BoolMapperExt interface.
2022-11-26 11:21:39 +11:00
pyqlsa a05a0c20ba Add Active member to Nodes and Values (#319) 2022-07-19 22:43:15 -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
Ali Eren Öztürk cc81f03a38 Support listing enums in specified order (#274) 2022-02-21 21:36:14 +11:00
Alec Thomas 72024dd44a fix: hydrate pointer fields
It is a mystery how this has been a bug for so long.
2022-02-16 13:26:29 +11:00
Mitar a7d3850e80 Cmd can be passthrough now, too.
Fixes #253.
2022-01-05 18:58:37 +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
Mitar c3703cda7e Fix that default should not override the placeholder.
Fixes #243.
2021-12-02 23:06:44 +11:00
Simon Frei 5a9c9c7864 Bubble errors instead of panicking (#194) 2021-08-19 18:00:53 +10:00
Nikita Semikov f547618963 Add aliases to ALL help output (#191) 2021-08-07 22:04:51 +10:00
Cam Hutchison 89315e74ad Allow default commands with cmds/args/flags (#188)
* Move default command validation to build

Move the validation of default commands - checking if a node has
multiple default commands or a default command has children - to the
build phase rather than tracing. These errors are with the structure of
the CLI ast and are detectable before parsing the command line.

Add a couple of tests for some of the default command error cases.

* Disallow positional args on a default command

Do not allow a default command to have positional arguments. The current
check is only for branching args, but the error message implies that
positional args are not allowed either. So add a check for positional
args too, and add a test case for it.

This is breaking change to the API but is unlikely to have ill-effect as
positional args on a default command cannot be used without explicitly
naming the command (i.e. not using it as a default).

* Allow default commands with cmds/args/flags

Allow default commands to have sub-commands, args and flags when tagged
with `default:"withargs"`. This makes specifying the name of the
command on the CLI completely optional as long as the args to that
command are not ambiguous with other commands.
2021-07-12 14:19:37 +10:00
Alec Thomas 54558f65e8 Allow multiple xor:"" groups to be defined.
Fixes #113.
2021-06-21 19:04:54 +09:30
Alec Thomas 6cebaa7b85 Hermitise and bump golangci-lint. 2021-06-09 12:48:47 +10:00
andrewbaxter b3bdeb18e2 Fixes #125: Support custom per-mapper placeholder text (#169) 2021-05-17 21:31:07 +10:00
Alec Thomas 2e611623a7 Don't display placeholder for counters.
Fixes #164.
2021-04-18 10:05:26 +10:00
Franklin "Snaipe" Mathieu d4dd709445 tag: add passthrough for positional arguments
This new tag tells the parser to stop processing flags after the
positional argument is encountered.

This is particularly useful for subcommands that forward their arguments
to an external program, and makes it possible to implement commands
like `kubectl exec` or `docker run`.

Fixes #80.
2021-04-06 07:53:16 +10:00
Alec Thomas 0c83d0f8dc Lint fix. 2021-03-13 21:13:23 +11: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
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
Joe Schmitt 2a0d7af9c5 Add support for negated boolean flags
Will automatically set a boolean Struct field to false if the flag starts with `--no-`, even when the default is `true`.

For example:
```
type Cmd struct {
  Output bool `default:"true"`
}
```

Calling
```
command
```
Will set `Output` to `true`, but
```
command --no-output
```
Will set `Output` to `false`.
2021-03-02 10:36:13 +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
Joe Schmitt 2479d83cc0 Add support for command aliases (#130) 2021-01-11 07:36:13 +11:00
Cam Hutchison f19f1031c0 Format help placeholders with slice/map separators
Use the specified separators for a flag in the placeholders printed in
the help message for slices and maps. Do not print the separator and
ellipsis if the separator is disabled (`"none"`).

Previously the default separator was printed with an ellipsis, ignoring
the `sep` and `mapsep` tags.

Add tests for `Flag.FormatPlaceHolder()`.
2020-06-02 17:19:11 +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
gak f830198fcd XOR groups for flags. (#35) 2019-07-20 20:31:04 +10:00
Alec Thomas 95de7d2f0d Produce a more useful error when flag-like values are used for flag values.
eg.  myapp: error: --log-level: expected string value but got "--foo" (long flag); perhaps try --log-level="--foo"?
2019-06-21 10:57:21 +10:00
Alec Thomas 7be398e79f ApplyDefaults() now only applies defaults if the value is not already otherwise set. 2019-06-12 13:26:36 +10:00
Alec Thomas 2ac3d43124 Make invalid value error messages more useful. 2019-04-01 10:07:10 +11:00
Alec Thomas 886c2d7b01 Better help for maps. 2019-01-12 20:59:24 +11:00
Alec Thomas aedca401da Hide sub-tree when parent is hidden. 2018-11-14 10:01:45 +11:00
Alec Thomas 010e3134ca Ensure flag defaults (or envars) aren't scanned in as untyped tokens.
This avoids treating default values starting with "-" as flags.
2018-11-06 14:40:02 +11:00
Alec Thomas e474873f84 Don't show ... in help for named slice types. 2018-11-06 11:15:35 +11:00
Alec Thomas fddfb973d6 Clean up error handling a bit. 2018-11-03 09:49:28 +11:00
Alec Thomas 5d7703774f Validate enums (finally). 2018-09-23 19:59:25 +10:00
Alec Thomas 3a832f8343 Treat envars as higher priority than resolvers. 2018-09-21 12:11:52 +10:00