Skip to content

Lazy command loading breaks when global options precede subcommand name #85

Description

@Anaethelion

Bug

When a global option (e.g. --use-context, --config-file, --json) is placed before the subcommand name, the lazy-loading logic in src/cli.ts fails to detect the correct subcommand and registers only the empty stub instead of the full command tree.

Steps to reproduce

elastic --use-context serverless cloud elasticsearch-projects list-elasticsearch-projects

Expected: command runs normally
Actual:

Error: unknown command: elasticsearch-projects

Root cause

cli.ts uses process.argv[2] to identify the first subcommand:

const firstArg = process.argv[2]

if (firstArg === 'es') { ... }
if (firstArg === 'cloud') { ... }

When global flags precede the subcommand, argv[2] is the flag (e.g. --use-context) rather than the subcommand name (cloud), so the full command tree is never loaded.

Suggested fix

Scan for the first non-flag argument instead of relying on a fixed index:

const firstArg = process.argv.slice(2).find(arg => !arg.startsWith('-'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions