yargs-file-commands
Functions
defineCommand()
Call Signature
function defineCommand<T, U>(module): DefineCommandResult<T, U>;
Defines a command whose handler arguments are inferred from a builder function.
The module is returned unchanged; this helper only adds types. Export the result as command
(or as the default export) from a file in a command directory.
Type Parameters
| Type Parameter | Default type |
|---|---|
T | object |
U | object |
Parameters
| Parameter | Type | Description |
|---|---|---|
module | DefineCommandWithBuilder<T, U> | The command definition. builder receives the Yargs instance and returns it with options added. |
Returns
DefineCommandResult<T, U>
The same module, typed as a CommandModule whose handler receives the arguments declared by builder.
Example
export const command = defineCommand({
command: 'my-command',
builder: (yargs) => yargs.option('name', { type: 'string', demandOption: true }),
handler: (argv) => {
// argv.name is correctly typed as string
console.log(argv.name);
},
});
Call Signature
function defineCommand<O, T>(module): DefineCommandResult<T, InferredOptionTypes<O>>;
Defines a command whose handler arguments are inferred from an options object.
Type Parameters
| Type Parameter | Default type |
|---|---|
O extends object | - |
T | object |
Parameters
| Parameter | Type | Description |
|---|---|---|
module | DefineCommandWithOptions<O> | The command definition. builder is a Yargs options object keyed by option name. |
Returns
DefineCommandResult<T, InferredOptionTypes<O>>
The same module, typed as a CommandModule whose handler receives the arguments inferred from builder.
Example
export const command = defineCommand({
describe: 'Start the studio',
builder: { port: { type: 'number', default: 3000 } },
handler: (argv) => {
// argv.port is typed as number
console.log(argv.port);
},
});
fileCommands()
function fileCommands(options): Promise<CommandModule<{
}, {
}>[]>;
Builds a Yargs command tree from the files under commandDirs.
Directories and dotted file names define nested commands, and a command file names its
directory's group. Only root-level command modules are imported up front; each group
imports its own commands when Yargs enters it, so an invocation only loads the modules
along the command path it uses. Later directories override matching commands in earlier ones.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | FileCommandsOptions | Directories to scan and how to scan them. |
Returns
Promise<CommandModule<{
}, {
}>[]>
The root-level commands, each with its nested subcommands, ready to pass to yargs.command().
Throws
If commandDirs is empty or contains a relative path, an extension lacks its leading dot,
no commands are found, or two files in one directory map to the same command.
Example
await yargs(hideBin(process.argv))
.command(await fileCommands({ commandDirs: [path.join(import.meta.dirname, 'commands')] }))
.demandCommand(1)
.help()
.parseAsync();
validateCommands()
function validateCommands(options): Promise<void>;
Imports every command module under commandDirs and validates its positional arguments and
group definitions without running any handler. Intended for a CLI's unit tests, so the check
costs nothing at runtime. Module-level code and builder functions do run.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | FileCommandsOptions | The same options you pass to fileCommands; validation is always enabled. |
Returns
Promise<void>
A promise that resolves when every command is valid.
Throws
On the first problem found, including a module that fails to import.
Example
it('validates the command tree', async () => {
await validateCommands({ commandDirs: [path.join(import.meta.dirname, 'commands')] });
});
Interfaces
DefineCommandBase
Fields shared by every command definition passed to defineCommand.
Extended by
Properties
DefineCommandWithBuilder
A command definition whose builder is a function that adds options to the Yargs instance.
Extends
Type Parameters
| Type Parameter | Default type |
|---|---|
T | object |
U | object |
Properties
| Property | Type | Description | Inherited from |
|---|---|---|---|
command? | string | readonly string[] | Command string, optionally with positionals such as create <name>. Defaults to the name derived from the file path. | DefineCommandBase.command |
aliases? | string | readonly string[] | Alternative names for the command. | DefineCommandBase.aliases |
describe? | string | false | Help text for the command. Use false to hide it from help output. | DefineCommandBase.describe |
deprecated? | string | boolean | Marks the command as deprecated. A string is shown as the deprecation message. | DefineCommandBase.deprecated |
builder | (yargs) => Argv<U> | PromiseLike<Argv<U>> | Adds options and positionals to the Yargs instance; their types flow into handler. | - |
handler | (args) => void | Promise<void> | Runs the command. | - |
DefineCommandWithOptions
A command definition whose builder is a Yargs options object keyed by option name.
Extends
Type Parameters
| Type Parameter |
|---|
O extends object |
Properties
| Property | Type | Description | Inherited from |
|---|---|---|---|
command? | string | readonly string[] | Command string, optionally with positionals such as create <name>. Defaults to the name derived from the file path. | DefineCommandBase.command |
aliases? | string | readonly string[] | Alternative names for the command. | DefineCommandBase.aliases |
describe? | string | false | Help text for the command. Use false to hide it from help output. | DefineCommandBase.describe |
deprecated? | string | boolean | Marks the command as deprecated. A string is shown as the deprecation message. | DefineCommandBase.deprecated |
builder | O | Yargs options keyed by option name; their types are inferred for handler. | - |
handler | (args) => void | Promise<void> | Runs the command. | - |
ScanDirectoryOptions
Options for directory scanning
Properties
Type Aliases
DefineCommandResult
type DefineCommandResult<T, U> = CommandModule<T, U>;
The Yargs CommandModule returned by defineCommand, with the builder's argument types preserved.
Type Parameters
| Type Parameter | Default type |
|---|---|
T | object |
U | object |
FileCommandsOptions
type FileCommandsOptions = ScanDirectoryOptions & object;
Configuration options for file-based command generation.
Type Declaration
| Name | Type | Description |
|---|---|---|
commandDirs | string[] | Absolute directory paths to scan. Later directories override matching commands in earlier ones. |
validation? | boolean | Validate builder positionals against the command string as modules load. Defaults to false; use validateCommands in tests for a full-tree check. |
demandCommandMessage? | string | Message used by every group when run without a subcommand. Defaults to You must specify a <group> subcommand with the group's name. |
Variables
DefaultFileCommandsOptions
const DefaultFileCommandsOptions: Required<Omit<FileCommandsOptions, "demandCommandMessage">>;
The defaults fileCommands and validateCommands apply to options you leave out.
Spread it to build on the defaults, for example to keep the default ignorePatterns when adding one.