Skip to content

Shell completion commands trigger agent selection prompt #308

@sushichan044

Description

@sushichan044

Describe the bug

Problem

When running shell completion commands like nr --completion, nr --completion-zsh, or nr --completion-bash in a directory without a lockfile, the agent selection prompt is displayed instead of the expected completion output.

Current Behavior

$ pwd
/Users/sushichan044
$ nr --completion-zsh
? Choose the agent : - Use arrow-keys. Return to submit.
o   npm
    yarn
    pnpm
    bun
    deno

Expected Behavior

Shell completion commands should output their results directly without agent detection, as they don't require a package manager agent.

Proposed Solution

Add onBeforeCommand hook that allows commands to perform agent-independent operations before agent detection occurs.
This hook is expected to be called after -v/--help processing but before getCliCommand().

Expected interface:

export interface RunOptions {
  /**
   * Called before agent detection and command execution.
   *
   * Useful for performing concrete, agent-agnostic operations such as generating shell-completion scripts.
   *
   * Call `exit()` to skip a command execution entirely.
   */
  onBeforeCommand?: (args: string[], ctx: { cwd: string, exit: () => void }) => void | Promise<void>
}

Expected usage:

runCli((agent, args, ctx) => {
  // main logic (requires agent)
  return parseNr(agent, args, ctx)
}, {
  onBeforeCommand: (args, { cwd, exit }) => {
    if (args[0] === '--completion') {
      const suggestions = getCompletionSuggestions(args, { cwd })
      console.log(suggestions.join('\n'))
      return exit()
    }

    if (args[0] === '--completion-bash') {
      console.log(rawBashCompletionScript)
      return exit()
    }
  },
})

Reproduction

https://stackblitz.com/edit/antfu-ni-nr-completion-results-agent-picker?file=README.md

System Info

System:
    OS: macOS 26.1
    CPU: (8) arm64 Apple M2
    Memory: 238.20 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.11.1 - /Users/sushichan044/.local/share/mise/installs/node/24.11.1/bin/node
    Yarn: 1.22.22 - /Users/sushichan044/.local/share/mise/installs/npm-corepack/0.34.4/bin/yarn
    npm: 11.6.2 - /Users/sushichan044/.local/share/mise/installs/node/24.11.1/bin/npm
    pnpm: 10.21.0 - /Users/sushichan044/.local/share/mise/installs/npm-corepack/0.34.4/bin/pnpm
    bun: 1.3.2 - /Users/sushichan044/.local/share/mise/installs/bun/1.3.2/bin/bun
    Deno: 2.5.6 - /Users/sushichan044/.local/share/mise/installs/deno/2.5.6/bin/deno
  Browsers:
    Chrome: 142.0.7444.162
    Safari: 26.1

Used Package Manager

n/a

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Contributions

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions