Skip to content

Commit 47b090f

Browse files
committed
fix: add an xorprefix:"..." option for prefixing xor/and groups
Fixes #343
1 parent cacaace commit 47b090f

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ Both can coexist with standard Tag parsing.
550550
| `and:"X,Y,..."` | AND groups for flags. All flags in the group must be used in the same command. When combined with `required`, all flags in the group will be required. |
551551
| `prefix:"X"` | Prefix for all sub-flags. |
552552
| `envprefix:"X"` | Envar prefix for all sub-flags. |
553+
| `xorprefix:"X"` | Prefix for all sub-flags in XOR/AND groups. |
553554
| `set:"K=V"` | Set a variable for expansion by child elements. Multiples can occur. |
554555
| `embed:""` | If present, this field's children will be embedded in the parent. Useful for composition. |
555556
| `passthrough:"<mode>"`[^1] | If present on a positional argument, it stops flag parsing when encountered, as if `--` was processed before. Useful for external command wrappers, like `exec`. On a command it requires that the command contains only one argument of type `[]string` which is then filled with everything following the command, unparsed. |

build.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func flattenedFields(v reflect.Value, ptag *Tag) (out []flattenedField, err erro
7171
// Accumulate prefixes.
7272
tag.Prefix = ptag.Prefix + tag.Prefix
7373
tag.EnvPrefix = ptag.EnvPrefix + tag.EnvPrefix
74+
tag.XorPrefix = ptag.XorPrefix + tag.XorPrefix
7475
// Combine parent vars.
7576
tag.Vars = ptag.Vars.CloneWith(tag.Vars)
7677
// Command and embedded structs can be pointers, so we hydrate them now.
@@ -111,7 +112,7 @@ func flattenedFields(v reflect.Value, ptag *Tag) (out []flattenedField, err erro
111112
// Build a Node in the Kong data model.
112113
//
113114
// "v" is the value to create the node from, "typ" is the output Node type.
114-
func buildNode(k *Kong, v reflect.Value, typ NodeType, tag *Tag, seenFlags map[string]bool) (*Node, error) {
115+
func buildNode(k *Kong, v reflect.Value, typ NodeType, tag *Tag, seenFlags map[string]bool) (*Node, error) { //nolint:gocyclo
115116
node := &Node{
116117
Type: typ,
117118
Target: v,
@@ -147,6 +148,12 @@ MAIN:
147148
}
148149
}
149150

151+
if len(tag.Xor) != 0 {
152+
for i := range tag.Xor {
153+
tag.Xor[i] = tag.XorPrefix + tag.Xor[i]
154+
}
155+
}
156+
150157
// Nested structs are either commands or args, unless they implement the Mapper interface.
151158
if field.value.Kind() == reflect.Struct && (tag.Cmd || tag.Arg) && k.registry.ForValue(fv) == nil {
152159
typ := CommandNode

kong_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -2475,3 +2475,23 @@ func TestCustomTypeNoEllipsis(t *testing.T) {
24752475
help := w.String()
24762476
assert.NotContains(t, help, "...")
24772477
}
2478+
2479+
func TestPrefixXorIssue343(t *testing.T) {
2480+
type DBConfig struct {
2481+
Password string `help:"Password" xor:"password" optional:""`
2482+
PasswordFile string `help:"File which content will be used for a password" xor:"password" optional:""`
2483+
PasswordCommand string `help:"Command to run to retrieve password" xor:"password" optional:""`
2484+
}
2485+
2486+
type SourceTargetConfig struct {
2487+
Source DBConfig `help:"Database config of source to be copied from" prefix:"source-" xorprefix:"source-" embed:""`
2488+
Target DBConfig `help:"Database config of source to be copied from" prefix:"target-" xorprefix:"target-" embed:""`
2489+
}
2490+
2491+
cli := SourceTargetConfig{}
2492+
kctx := mustNew(t, &cli)
2493+
_, err := kctx.Parse([]string{"--source-password=foo", "--target-password=bar"})
2494+
assert.NoError(t, err)
2495+
_, err = kctx.Parse([]string{"--source-password-file=foo", "--source-password=bar"})
2496+
assert.Error(t, err)
2497+
}

tag.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Tag struct {
4848
Vars Vars
4949
Prefix string // Optional prefix on anonymous structs. All sub-flags will have this prefix.
5050
EnvPrefix string
51+
XorPrefix string // Optional prefix on XOR/AND groups.
5152
Embed bool
5253
Aliases []string
5354
Negatable string
@@ -268,6 +269,7 @@ func hydrateTag(t *Tag, typ reflect.Type) error { //nolint: gocyclo
268269
}
269270
t.Prefix = t.Get("prefix")
270271
t.EnvPrefix = t.Get("envprefix")
272+
t.XorPrefix = t.Get("xorprefix")
271273
t.Embed = t.Has("embed")
272274
if t.Has("negatable") {
273275
if !isBool && !isBoolPtr {

0 commit comments

Comments
 (0)