Commit Graph

71 Commits

Author SHA1 Message Date
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
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
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
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
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
Michal Kralik 9c8b401de0 Cumulative argument needs to be last (#331) 2022-09-20 05:47:41 -07:00
Michal Kralik f9bc630ef8 Panic on duplicate command names (#317) 2022-07-17 02:31:21 -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 fa08e7027c fix: overly enthusiastic validation of tags 2021-12-13 16:48:21 +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
Connor Hindley ea18c73f11 Only prepend EnvPrefix if .Env is not empty.
Before this change the EnvPrefix was applied to fields without env
specified, which resulted in strange and confusing help output.
2021-11-19 06:22:36 +11:00
Dan 2770a34ce6 Add envprefix tag 2021-10-22 16:25:13 +11:00
Alec Thomas 5c7b038540 Rename IgnoreFieldsRegex -> IgnoreFields. 2021-09-27 16:31:54 +10:00
Alec Thomas d0c0180cec Hydrate pointers to embedded structs. 2021-08-28 16:54:55 +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
Alec Thomas 43616c6a6d Hydrate sub-commands that are pointers.
Fixes #196.
2021-08-27 20:57:38 +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
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 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 ff93d67a8d Validate duplicate short flags.
Fixes #174.
2021-06-09 13:01:16 +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 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
Alec Thomas 259d19076d Add support for plugins. 2020-09-16 16:41:18 +10:00
Alec Thomas 067f0e835f More useful error message when finding an unexpected struct. 2020-09-12 16:32:10 +10:00
gak f830198fcd XOR groups for flags. (#35) 2019-07-20 20:31:04 +10:00
Alec Thomas 439c674f7a Use interface{} instead of string in tokens.
This allows the scanner and resolvers to pass Go types around rather
than having to serialise/deserialise to/from strings.
2019-04-24 23:25:13 +10:00
Matthias Fax fcf5f9bc1a fix: hidden tag is not passed to the flag object
Or is it not supposed to be passed?
2019-01-20 09:03:53 +11:00
Alec Thomas 6406edf15f Add Visitor function for walking the model. 2018-09-21 09:47:55 +10:00
Alec Thomas 6fa83bdc0e Apply hooks to default values. 2018-09-20 20:33:55 +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 37d57a878b Support for embedding interfaces. 2018-09-12 13:19:56 +10:00
Alec Thomas fd197e5081 Support for flag prefixing. 2018-09-12 12:17:57 +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 b2cab08684 Add support for a mapper interface directly on fields.
If a field implements the MapperValue interface that interface will be
used.
2018-06-29 12:33:50 +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 212ea2a356 Add path, existingfile and existingdir types.
- Document custom types.
- Add docker example.
2018-06-21 16:19:08 +10:00
Alec Thomas 3a2f3eebdd Indent multi-line messages/errors. 2018-06-20 12:45:57 +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 c7dca86dad Rename Value.Value to Value.Target to correctly reflect its purpose. 2018-06-13 21:12:56 +10:00
Alec Thomas 96fa9c43d5 Improved documentation and help. 2018-06-05 11:36:51 +10:00