Skip to content
Myr

createCommand factory routine (#1191)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add factory method * Fix return doc * Can not use "this" for return type of createCommand * Use return type of createCommand for subcommand * Add mention of .command from .createCommand * Remove trailing space * Add examples for createCommand * Explain example and make a little more realistic * Add comments pointing from .addCommand to .command One of the downsides of extra ways of adding and creating commands is confusion with the more common way. * Add createCommand to README * Shift command/subcommand contrast * Use single quotes in ts like in js, and clean up whitespace in new code

authored by John Gee · committed by GitHub · parent 3c9f33f · Browse files · 7 files+168 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+168 added−3 removed

  • tests:+36 added−0 removed
  • other:+132 added−3 removed

7 files+168 added−3 removed

added:examples/custom-command-class.js+36 added
added:examples/custom-command-function.js+36 added
added:tests/createCommand.test.js+36 added
modified:typings/commander-tests.ts+16 added
183 unmodified lines
184const helpOptionThis3: commander.Command = program.helpOption(undefined, 'custom description');185186// on187const onThis: commander.Command = program.on('--help', () => { })188189// createCommand190191const createInstance1: commander.Command = program.createCommand();192const createInstance2: commander.Command = program.createCommand('name');193194class MyCommand extends commander.Command {195 createCommand(name?: string) {196 return new MyCommand(name);197 }198 myFunction() {}199}200const myProgram = new MyCommand();201myProgram.myFunction();202const mySub = myProgram.command('sub');203mySub.myFunction();
modified:typings/index.d.ts+12 added-2 removed
22 unmodified lines
23 }24 type OptionConstructor = { new (flags: string, description?: string): Option };2526 interface ParseOptions {27 from: "node" | "electron" | "user";27 from: 'node' | 'electron' | 'user';28 }2930 interface Command {31 [key: string]: any; // options as properties
31 unmodified lines
63 * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`64 * @param opts - configuration options65 * @returns new command66 */67 command(nameAndArgs: string, opts?: CommandOptions): Command;67 command(nameAndArgs: string, opts?: CommandOptions): ReturnType<this['createCommand']>;68 /**69 * Define a command, implemented in a separate executable file.70 * 71 * @remarks
13 unmodified lines
85 */86 command(nameAndArgs: string, description: string, opts?: commander.CommandOptions): this;8788 /**89 * Factory routine to create a new unattached command.90 *91 * See .command() for creating an attached subcommand, which uses this routine to92 * create the command. You can override createCommand to customise subcommands.93 */94 createCommand(name?: string): Command;95 96 /**97 * Add a prepared subcommand.98 * 99 * See .command() for creating an attached subcommand which inherits settings from its parent.100 * 101 * @returns parent command for chaining102 */103 addCommand(cmd: Command): this;104
264 unmodified lines
modified:index.js+18 added-1 removed
164 unmodified lines
165 desc = null;166 }167 opts = opts || {};168 const args = nameAndArgs.split(/ +/);169 const cmd = new Command(args.shift());169 const cmd = this.createCommand(args.shift());170171 if (desc) {172 cmd.description(desc);173 cmd._executableHandler = true;
21 unmodified lines
195 return cmd;196 };197198 /**199 * Factory routine to create a new unattached command.200 *201 * See .command() for creating an attached subcommand, which uses this routine to202 * create the command. You can override createCommand to customise subcommands.203 *204 * @param {string} [name]205 * @return {Command} new command206 * @api public207 */208209 createCommand(name) {210 return new Command(name);211 };212213 /**214 * Add a prepared subcommand.215 *216 * See .command() for creating an attached subcommand which inherits settings from its parent.217 *218 * @param {Command} cmd - new subcommand219 * @return {Command} parent command for chaining220 * @api public221 */
1491 unmodified lines
modified:Readme.md+14 added
34 unmodified lines
35 - [Bits and pieces](#bits-and-pieces)36 - [.parse() and .parseAsync()](#parse-and-parseasync)37 - [Avoiding option name clashes](#avoiding-option-name-clashes)38 - [TypeScript](#typescript)39 - [createCommand()](#createcommand)40 - [Node options such as `--harmony`](#node-options-such-as---harmony)41 - [Debugging stand-alone executable subcommands](#debugging-stand-alone-executable-subcommands)42 - [Override exit handling](#override-exit-handling)43 - [Examples](#examples)
586 unmodified lines
630```bash631node -r ts-node/register pm.ts632```633634### createCommand()635636This factory function creates a new command. It is exported and may be used instead of using `new`, like:637638```js639const { createCommand } = require('commander');640const program = createCommand();641```642643`createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally644when creating subcommands using `.command()`, and you may override it to645customise the new subcommand (examples using [subclass](./examples/custom-command-class.js) and [function](./examples/custom-command-function.js)).646647### Node options such as `--harmony`648649You can enable `--harmony` option in two ways:650
87 unmodified lines