Skip to content

Commit 4ecd2df

Browse files
committed
fix(commands): preserve ShorthandDeprecated in flag clones and dedupe via helper
Address review feedback from gemini-code-assist on pkg/commands/talosctl_wrapper.go:121: the manual pflag.Flag clone used for the -f -> -F shorthand rename was missing the ShorthandDeprecated field. Without it, an upstream flag that marks its short form as deprecated would have that deprecation lost on the wrapped clone, and pflag's MarkShorthandDeprecated check (flag.go:719) would re-emit the shorthand in help output. Extract a renameFlagShorthand helper that mirrors every pflag.Flag metadata field, and use it from both the local-flag and persistent-flag copy loops. Value is intentionally shared (not deep-copied) — the parser must write to upstream's variable so the wrapped RunE (assigned from upstream) can still read its own state via cmd.Flags().Changed/.Value lookups against the wrapped command instance cobra passes at runtime. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent c2f3017 commit 4ecd2df

1 file changed

Lines changed: 26 additions & 27 deletions

File tree

‎pkg/commands/talosctl_wrapper.go‎

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ var rootShadowedPersistentFlags = map[string]struct{}{
9292
"siderov1-keys-dir": {},
9393
}
9494

95+
// renameFlagShorthand clones an upstream flag with its shorthand
96+
// replaced by `newShorthand`. Every pflag.Flag metadata field —
97+
// including ShorthandDeprecated, which controls upstream's deprecation
98+
// warning behaviour — is mirrored so downstream consumers that
99+
// introspect the wrapped flag observe the same shape as the original.
100+
// Value is intentionally NOT deep-copied: the parser must write to
101+
// upstream's variable so the wrapped RunE (assigned from upstream)
102+
// can still read its own state.
103+
func renameFlagShorthand(flag *pflag.Flag, newShorthand string) *pflag.Flag {
104+
return &pflag.Flag{
105+
Name: flag.Name,
106+
Usage: flag.Usage,
107+
Value: flag.Value,
108+
DefValue: flag.DefValue,
109+
Changed: flag.Changed,
110+
NoOptDefVal: flag.NoOptDefVal,
111+
Deprecated: flag.Deprecated,
112+
Hidden: flag.Hidden,
113+
Shorthand: newShorthand,
114+
ShorthandDeprecated: flag.ShorthandDeprecated,
115+
Annotations: flag.Annotations,
116+
}
117+
}
118+
95119
// propagatePersistentFlags mirrors the upstream command's persistent
96120
// flags onto the wrapped command's PersistentFlags so cobra's
97121
// mergePersistentFlags walks them through to wrapped children at
@@ -107,19 +131,7 @@ func propagatePersistentFlags(cmd, wrappedCmd *cobra.Command) {
107131
}
108132

109133
if flag.Shorthand == "f" {
110-
renamed := &pflag.Flag{
111-
Name: flag.Name,
112-
Usage: flag.Usage,
113-
Value: flag.Value,
114-
DefValue: flag.DefValue,
115-
Changed: flag.Changed,
116-
NoOptDefVal: flag.NoOptDefVal,
117-
Deprecated: flag.Deprecated,
118-
Hidden: flag.Hidden,
119-
Shorthand: "F",
120-
Annotations: flag.Annotations,
121-
}
122-
wrappedCmd.PersistentFlags().AddFlag(renamed)
134+
wrappedCmd.PersistentFlags().AddFlag(renameFlagShorthand(flag, "F"))
123135

124136
return
125137
}
@@ -159,20 +171,7 @@ func wrapTalosCommand(cmd *cobra.Command, cmdName string) *cobra.Command {
159171

160172
// If this flag has shorthand 'f', we need to change it to 'F'
161173
if flag.Shorthand == "f" {
162-
// Create a copy with new shorthand
163-
newFlag := &pflag.Flag{
164-
Name: flag.Name,
165-
Usage: flag.Usage,
166-
Value: flag.Value,
167-
DefValue: flag.DefValue,
168-
Changed: flag.Changed,
169-
NoOptDefVal: flag.NoOptDefVal, // Important for bool flags to work without '='
170-
Deprecated: flag.Deprecated,
171-
Hidden: flag.Hidden,
172-
Shorthand: "F", // Change shorthand from 'f' to 'F'
173-
Annotations: flag.Annotations,
174-
}
175-
wrappedCmd.Flags().AddFlag(newFlag)
174+
wrappedCmd.Flags().AddFlag(renameFlagShorthand(flag, "F"))
176175
} else {
177176
wrappedCmd.Flags().AddFlag(flag)
178177
}

0 commit comments

Comments
 (0)