command()
Define a command with typed options, positionals, and a handler function.
Import from maltty.
CommandDef
Yargs-native arg format
As an alternative to Zod, commands accept a yargs-native format for options and positionals. Both produce the same typed ctx.args.
YargsArgDef
Hidden and deprecated
Both accept a static value or a function (Resolvable<T>), resolved once at registration time.
Default commands
Mark a command default: true to make it run when no subcommand matches. Both invocation forms stay available, and ctx.meta.command still reports the command's own name. A nameless root index command is the exception -- it has no name to report, so it contributes no path segment and ctx.meta.command is [].
A known subcommand name always wins over the default command's positionals, so mygrep config cannot pass config as a pattern. Use the explicit form (mygrep search config) when you need that value.
At most one command per level may be marked default -- a second one is a startup error. The flag also works inside a subcommand group, where it makes the bare group invocation dispatch to that subcommand:
Via autoload
An index file at the root of the commands directory becomes the default command automatically, mirroring how an index file inside a subdirectory becomes that group's parent command.
Give it a name to keep a named invocation form alongside the default; without one it is reachable only as the default.
If that name collides with another file's command, autoload keeps the root index command and warns -- the later definition is discarded. Because only one survives, the collision is reported as a warning rather than the duplicate-default startup error, which sees the deduplicated map.
Subcommands
Commands can contain nested subcommands:
autoload()
Dynamically discover commands from a directory at runtime.