-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
createCommand factory routine (#1191)
* 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
- Loading branch information
1 parent
3c9f33f
commit 8c3dd6f
Showing
7 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env node | ||
|
||
// const commander = require('commander'); // (normal include) | ||
const commander = require('../'); // include commander in git clone of commander repo | ||
|
||
// Use a class override of createCommand to customise subcommands, | ||
// in this example by adding --debug option. | ||
|
||
class MyCommand extends commander.Command { | ||
createCommand(name) { | ||
const cmd = new MyCommand(name); | ||
cmd.option('-d,--debug', 'output options'); | ||
return cmd; | ||
} | ||
}; | ||
|
||
const program = new MyCommand(); | ||
program | ||
.command('serve') | ||
.option('--port <port-number>', 'specify port number', 80) | ||
.action((cmd) => { | ||
if (cmd.debug) { | ||
console.log('Options:'); | ||
console.log(cmd.opts()); | ||
console.log(); | ||
} | ||
|
||
console.log(`Start serve on port ${cmd.port}`); | ||
}); | ||
|
||
program.parse(); | ||
|
||
// Try the following: | ||
// node custom-command-class.js help serve | ||
// node custom-command-class.js serve --debug | ||
// node custom-command-class.js serve --debug --port 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env node | ||
|
||
// const commander = require('commander'); // (normal include) | ||
const commander = require('../'); // include commander in git clone of commander repo | ||
|
||
// Override createCommand directly to customise subcommands, | ||
// in this example by adding --debug option. | ||
|
||
const program = commander.createCommand(); | ||
|
||
// Customise subcommand creation | ||
program.createCommand = (name) => { | ||
const cmd = commander.createCommand(name); | ||
cmd.option('-d,--debug', 'output options'); | ||
return cmd; | ||
}; | ||
|
||
program | ||
.command('serve') | ||
.option('--port <port-number>', 'specify port number', 80) | ||
.action((cmd) => { | ||
if (cmd.debug) { | ||
console.log('Options:'); | ||
console.log(cmd.opts()); | ||
console.log(); | ||
} | ||
|
||
console.log(`Start serve on port ${cmd.port}`); | ||
}); | ||
|
||
program.parse(); | ||
|
||
// Try the following: | ||
// node custom-command-function.js help serve | ||
// node custom-command-function.js serve --debug | ||
// node custom-command-function.js serve --debug --port 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const commander = require('../'); | ||
|
||
test('when createCommand then unattached', () => { | ||
const program = new commander.Command(); | ||
const cmd = program.createCommand(); | ||
expect(program.commands.length).toBe(0); | ||
expect(cmd.parent).toBeFalsy(); // (actually null, but use weaker test for unattached) | ||
}); | ||
|
||
test('when subclass overrides createCommand then subcommand is subclass', () => { | ||
class MyClass extends commander.Command { | ||
constructor(name) { | ||
super(); | ||
this.myProperty = 'myClass'; | ||
}; | ||
|
||
createCommand(name) { | ||
return new MyClass(name); | ||
}; | ||
}; | ||
const program = new MyClass(); | ||
const sub = program.command('sub'); | ||
expect(sub.myProperty).toEqual('myClass'); | ||
}); | ||
|
||
test('when override createCommand then subcommand is custom', () => { | ||
function createCustom(name) { | ||
const cmd = new commander.Command(); | ||
cmd.myProperty = 'custom'; | ||
return cmd; | ||
} | ||
const program = createCustom(); | ||
program.createCommand = createCustom; | ||
const sub = program.command('sub'); | ||
expect(sub.myProperty).toEqual('custom'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters