49 Commits

Author SHA1 Message Date
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 81d0c29303 fix: regression where all args became passthrough
Fixes #475
2024-12-03 08:22:10 +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
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 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
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
Florian Loch 37e801405f fix: handle contents of tags properly by unquoting them when necessary 2023-01-26 03:52:22 +11: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
Jacob Hochstetler 0c6a9f3a3d Merging issue 280 with master changes (#296) 2022-09-20 05:55:05 -07: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
Alec Thomas 9c9b8ab50b More enum fixes. 2021-12-13 18:54:06 +11:00
Alec Thomas fa08e7027c fix: overly enthusiastic validation of tags 2021-12-13 16:48:21 +11:00
Mitar c3703cda7e Fix that default should not override the placeholder.
Fixes #243.
2021-12-02 23:06:44 +11:00
Dan 2770a34ce6 Add envprefix tag 2021-10-22 16:25:13 +11: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 54558f65e8 Allow multiple xor:"" groups to be defined.
Fixes #113.
2021-06-21 19:04:54 +09:30
Alec Thomas 8cbf5a6c03 Validate short flags are a single rune.
Fixes #175.
2021-06-09 13:11:49 +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
Joe Schmitt ed24960916 Implement code review feedback 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
Joe Schmitt 2479d83cc0 Add support for command aliases (#130) 2021-01-11 07:36:13 +11:00
Cam Hutchison d4822839a2 Add GetSep to simplify Tag initialisation
Factor out some repetition from `parseTag()` into a `GetSep()` method so
as to simplify `parseTag()` and formalise the semantics of a separator.

The existing behaviour in corner/error cases has been preserved, but an
appropriate error is returned (currently ignored) so a future breaking
change could be made to handle these conditions.

Tests have not been added as the test files are in a separate package to
the unit under test, making direct `Tag` construction difficult. Testing
through the exported APIs is already performed in the
`FormatPlaceHolder()` tests.
2020-06-02 17:19:11 +10:00
Cam Hutchison b89354adb0 Fix sep:"none" and mapsep:"none"
According to the README.md, a value of `"none"` for the `sep` tag (for
slices) and `mapsep` (for maps) is meant to disable the separator that
would allow multiple entries to be added from a single argument.
However, using `"none"` just set the separator to the rune `'n'`.

Fix the parsing of the tag, and also update `SplitEscaped` to not split
with a separator of `-1`.

Add a new test for the new cases.
2020-06-02 17:19:11 +10:00
Rene Zbinden 529703d8d1 add support to make map separator configurable 2020-02-14 21:25:58 +11:00
gak f830198fcd XOR groups for flags. (#35) 2019-07-20 20:31:04 +10:00
Alec Thomas 54338bd8b1 Add an "embed" tag. 2018-09-19 13:23:18 +10:00
Alec Thomas 2e29ff8981 Add support for setting variables via tag.
This provides much more convenient composition when reusing structs in
different parts of the command grammar.

eg.

	type Embedded struct {
		Key string `help:"A key from ${where}."`
	}

	var cli struct {
		Embedded `set:"where=somewhere"`
	}
2018-09-19 12:57:59 +10:00
Alec Thomas 467352418f Ignore fields tagged with kong:"-". 2018-09-17 10:27:38 +10:00
Alec Thomas fd197e5081 Support for flag prefixing. 2018-09-12 12:17:57 +10:00
Alec Thomas 1bb0c0b4b2 Support limited variable interpolation.
Kong supports limited variable interpolation into help strings, enum lists and
default values.

Variables are in the form:

    ${<name>}

Variables are set with the `Vars(map[string]string)` option. Undefined
variable references in the grammar will result in an error at construction
time.
2018-06-27 21:09:56 +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 cdcdf49f67 Display usage information on error. 2018-06-21 10:08:01 +10:00
Alec Thomas 9a68d32e72 Support for embedded structs + sep:"none".
Map key/value separator is now hardcoded to `=`. This allows the
`sep` tag to continue to be used for slice separation, cascading to
support maps with slice values eg. `--set a=1,2,3`.
2018-06-14 11:31:50 +10:00
Alec Thomas 662ace41c5 Move separator defaulting back into Tag. 2018-06-13 22:58:53 +10:00
Alec Thomas ecf21e4cc9 Add support for maps. 2018-06-13 22:17:28 +10:00
Alec Thomas 232faad0a0 Add configuration loading + docs + linter fixes. 2018-06-13 10:34:19 +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 96fa9c43d5 Improved documentation and help. 2018-06-05 11:36:51 +10:00
Gerald Kaszuba 254fa00e81 Better name for parseCSV -> parseTagItems 2018-06-01 12:26:26 +10:00
Gerald Kaszuba ed123d1c06 Allow "bare" tags and moved tag tests into tag_test.go. Fixes #19 2018-06-01 12:26:26 +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
Alec Thomas 5df3b26bab Use new Tag type for slice separator. 2018-05-23 12:20:47 -04:00
Gerald Kaszuba 886edb6f5a Extend Tag for non-core decoders (Fixes #14) 2018-05-23 11:50:46 -04:00
Gerald Kaszuba ecd369e622 Kong tag PR fixes (#12)
* Removed bubbling errors in favour of panic
* Added a test for Parse with a bad build and an arg.
* Removed is() function in favour of a switch with strings
* Collect key and value in parseCSV, reducing complexity.
* Fix in global to not use parser instance on error.
2018-05-22 09:18:08 -04:00
Gerald Kaszuba 3eb5e285ed Use "kong" as tag keys fixes #9 (#10) 2018-05-21 09:25:19 -04:00