MCPcopy Create free account
hub / github.com/yargs/yargs / addHandler

Method addHandler

lib/command.ts:125–232  ·  view source on GitHub ↗
(
    cmd: string | CommandHandlerDefinition | DefinitionOrCommandName[],
    description?: CommandHandler['description'],
    builder?: CommandBuilderDefinition | CommandBuilder,
    handler?: CommandHandlerCallback,
    commandMiddleware?: Middleware[],
    deprecated?: boolean
  )

Source from the content-addressed store, hash-verified

123 }
124 }
125 addHandler(
126 cmd: string | CommandHandlerDefinition | DefinitionOrCommandName[],
127 description?: CommandHandler['description'],
128 builder?: CommandBuilderDefinition | CommandBuilder,
129 handler?: CommandHandlerCallback,
130 commandMiddleware?: Middleware[],
131 deprecated?: boolean
132 ): void {
133 let aliases: string[] = [];
134 const middlewares = commandMiddlewareFactory(commandMiddleware);
135 handler = handler || (() => {});
136
137 // If an array is provided that is all CommandHandlerDefinitions, add
138 // each handler individually:
139 if (Array.isArray(cmd)) {
140 if (isCommandAndAliases(cmd)) {
141 [cmd, ...aliases] = cmd;
142 } else {
143 for (const command of cmd) {
144 this.addHandler(command);
145 }
146 }
147 } else if (isCommandHandlerDefinition(cmd)) {
148 let command =
149 Array.isArray(cmd.command) || typeof cmd.command === 'string'
150 ? cmd.command
151 : null;
152 if (command === null) {
153 throw new Error(
154 `No command name given for module: ${this.shim.inspect(cmd)}`
155 );
156 }
157 if (cmd.aliases)
158 command = ([] as string[]).concat(command).concat(cmd.aliases);
159 this.addHandler(
160 command,
161 this.extractDesc(cmd),
162 cmd.builder,
163 cmd.handler,
164 cmd.middlewares,
165 cmd.deprecated
166 );
167 return;
168 } else if (isCommandBuilderDefinition(builder)) {
169 // Allow a module to be provided as builder, rather than function:
170 this.addHandler(
171 [cmd].concat(aliases),
172 description,
173 builder.builder,
174 builder.handler,
175 builder.middlewares,
176 builder.deprecated
177 );
178 return;
179 }
180
181 // The 'cmd' provided was a string, we apply the command DSL:
182 // https://github.com/yargs/yargs/blob/main/docs/advanced.md#advanced-topics

Callers 2

addDirectoryMethod · 0.95
commandMethod · 0.80

Calls 7

extractDescMethod · 0.95
commandMiddlewareFactoryFunction · 0.85
isCommandAndAliasesFunction · 0.85
parseCommandFunction · 0.85
commandMethod · 0.65

Tested by

no test coverage detected