Commander.js parses argv into options, arguments, and subcommands, then drives help text and action handlers for Node.js CLI tools.
Overview
Commander.js is a Node.js package for describing and running command-line interfaces in code. You declare options, positional arguments, and nested subcommands; the library parses process arguments, enforces unknown-option errors by default, and routes execution to handlers or external subcommand binaries.
Options support boolean and value flags, defaults, negated long names, optional values, variadic lists, and mandatory options. Custom processing hooks can coerce types or accumulate values. Commands can nest arbitrarily, carry aliases, and hide or mark default subcommands for help output.
Help is generated from the definitions you attach, with hooks to customize usage text, error display, and version flags. Programs can parse synchronously or asynchronously when handlers return promises. Stand-alone executable subcommands map names to separate files discovered beside the entry script.
The package exposes a global default object for quick scripts and a factory for tests or multi-command applications that need isolated Command instances.
Key features
- Declarative options and arguments
- Nested subcommands
- Auto-generated help and version
- Strict unknown-option checking
- Sync and async parsing
- Executable file subcommands
- Custom option and argument coercion
- TypeScript typings
Use cases
- Node.js developer CLIs
- Multi-subcommand tooling
- Scripts with nested commands
- Published npm command binaries