-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
createCommand factory routine #1191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b99c4b4
Add factory method
shadowspawn 57883f5
Fix return doc
shadowspawn 116405b
Can not use "this" for return type of createCommand
shadowspawn 0918596
Merge branch 'develop' into feature/factory
shadowspawn 86a8ae5
Use return type of createCommand for subcommand
shadowspawn 045b011
Add mention of .command from .createCommand
shadowspawn af3a2ee
Remove trailing space
shadowspawn fa202c4
Add examples for createCommand
shadowspawn 3940a5d
Explain example and make a little more realistic
shadowspawn 052e309
Add comments pointing from .addCommand to .command
shadowspawn 841740d
Add createCommand to README
shadowspawn cb44c62
Shift command/subcommand contrast
shadowspawn 18e967c
Use single quotes in ts like in js, and clean up whitespace in new code
shadowspawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.