Skip to content

feat: infer version of sub from parents (#1860)#2404

Open
tionichm wants to merge 2 commits into
spf13:mainfrom
tionichm:main
Open

feat: infer version of sub from parents (#1860)#2404
tionichm wants to merge 2 commits into
spf13:mainfrom
tionichm:main

Conversation

@tionichm

Copy link
Copy Markdown

What

  • Added transparent subcommand version inference from nearest versioned parent.
  • Added tests for nested subcommands deriving versions from direct parents.
  • Altered existing tests expecting errors when subcommand version is requested without being explicitly set.
  • Resolves issues from some of @brianpursley's examples, including issue title.

Example 1: Base case, works as expected for the root command

No change.

$ go run mycmd.go --version
root version 1.0.0

Example 2: Pass sub command after the flag. This is what the unit test does.

No change.

$ go run mycmd.go --version sub
root version 1.0.0

Example 3: Pass sub command before the flag. Now you can see it's not really working for sub commands.

Change: sub derives version from root.

$ go run mycmd.go sub --version
sub version 1.0.0

Example 4: Pass an argument to the sub command. This shows that cobra must be interpreting "sub" as a flag value because it considers foo to be the sub command

No change: Probably not related to versioning mentioned in issue. I suggest a new issue is raised for this.

$ go run mycmd.go --version sub foo
Error: unknown command "foo" for "root"
Run 'root --help' for usage.

Example 5: To further show that it is skipping over the sub, I can pass sub twice and it then says --version is an unknown flag for sub:

Change: The version of sub is returned rather than throwing an error. We expect this command to return the version of root. Possibly related to the bug from above.

$ go run mycmd.go --version sub sub
sub version 1.0.0

Why

`--version flag does not exist` is only thrown if version cannot be
resolved
@CLAassistant

CLAassistant commented May 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread command.go
Comment on lines +1234 to +1252
// inferVersion attempts to determine the version of an unversioned
// subcommand by querying the version of the parent. If the parent's
// version is not set, then the process continues until the root command
// is reached. If the root command is also unversioned, then the subcommand
// remains unversioned.
func (c *Command) inferVersion() string {
if c.Version != "" {
return c.Version
}
current := c
for current.HasParent() {
current = current.Parent()
if current.Version != "" {
return current.Version
}
}
return ""
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also alter Command.Version in place. I'll leave it up to the reviewer to decide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants