Skip to content

fix: correct usage display to show [command] instead of [flags]#4733

Merged
ChengyuZhu6 merged 1 commit into
containerd:mainfrom
niveshdandyan:fix/usage-display
Feb 5, 2026
Merged

fix: correct usage display to show [command] instead of [flags]#4733
ChengyuZhu6 merged 1 commit into
containerd:mainfrom
niveshdandyan:fix/usage-display

Conversation

@niveshdandyan
Copy link
Copy Markdown
Contributor

Summary

Fix the usage display when running nerdctl without any command.

Fixes #3185

Problem

When running nerdctl without any arguments, the usage text showed:

Usage: nerdctl [flags]

But it should show:

Usage: nerdctl [command]

Root Cause

The usage() function used c.Runnable() to decide whether to show [flags] or [command]. However, the root command has RunE set to helpers.UnknownSubcommandAction (for handling unknown subcommands), which makes it "runnable" from cobra's perspective.

Solution

Changed the logic to prioritize checking HasSubCommands() first. Commands with subcommands should always show [command] in their usage, regardless of whether they have a Run function.

// Before
if c.Runnable() {
    s += c.UseLine() + "\n"
} else {
    s += c.CommandPath() + " [command]\n"
}

// After
if c.HasSubCommands() {
    s += c.CommandPath() + " [command]\n"
} else if c.Runnable() {
    s += c.UseLine() + "\n"
} else {
    s += c.CommandPath() + " [command]\n"
}

Testing

  • Code compiles successfully
  • Logic is straightforward and follows the expected behavior

When running nerdctl without any command, the usage text incorrectly
showed "Usage: nerdctl [flags]" instead of "Usage: nerdctl [command]".

This happened because the usage function checked c.Runnable() to
decide the format, but the root command has RunE set (for handling
unknown subcommands), making it "runnable" even though it semantically
requires a subcommand.

The fix prioritizes checking HasSubCommands() first, which correctly
displays "[command]" for commands that have subcommands, regardless
of whether they also have a Run function defined.

Fixes #3185

Signed-off-by: Nivesh Dandyan <niveshdandyan@gmail.com>
@AkihiroSuda AkihiroSuda added this to the v2.3.0 milestone Feb 4, 2026
Copy link
Copy Markdown
Member

@ChengyuZhu6 ChengyuZhu6 left a comment

Choose a reason for hiding this comment

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

LGTM

@ChengyuZhu6 ChengyuZhu6 merged commit 7d12ec8 into containerd:main Feb 5, 2026
142 of 156 checks passed
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.

Incorrect usage is displayed when running nerdctl without any command

3 participants