-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Factory routine to create a new unattached argument #1497
Merged
shadowspawn
merged 6 commits into
tj:release/8.x
from
cravler:feature/factory-routine-for-argument
Apr 12, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
14c9686
Factory routine to create a new unattached argument
cravler 28be5d2
ESM export improvements
cravler 7e3814e
createArgument ESM test
cravler 6e086b2
Add typings for createArgument
shadowspawn 4eb8b35
Add tests for createArgument
shadowspawn 2e97568
Merge pull request #1 from shadowspawn/cravler-feature/factory-routin…
cravler 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 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 |
---|---|---|
@@ -1,4 +1,15 @@ | ||
import commander from './index.js'; | ||
|
||
// wrapper to provide named exports for ESM. | ||
export const { program, Option, Command, Argument, CommanderError, InvalidOptionArgumentError, Help, createCommand, createOption } = commander; | ||
export const { | ||
program, | ||
createCommand, | ||
createArgument, | ||
createOption, | ||
CommanderError, | ||
InvalidOptionArgumentError, | ||
Command, | ||
Argument, | ||
Option, | ||
Help | ||
} = commander; |
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,40 @@ | ||
const commander = require('../'); | ||
|
||
class MyArgument extends commander.Argument { | ||
constructor(name, description) { | ||
super(name, description); | ||
this.myProperty = 'MyArgument'; | ||
}; | ||
} | ||
|
||
class MyCommand extends commander.Command { | ||
createArgument(name, description) { | ||
return new MyArgument(name, description); | ||
}; | ||
|
||
// createCommand for testing .command('sub <file>') | ||
createCommand(name) { | ||
return new MyCommand(name); | ||
} | ||
} | ||
|
||
test('when override createArgument then used for argument()', () => { | ||
const program = new MyCommand(); | ||
program.argument('<file>'); | ||
expect(program._args.length).toEqual(1); | ||
expect(program._args[0].myProperty).toEqual('MyArgument'); | ||
}); | ||
|
||
test('when override createArgument then used for arguments()', () => { | ||
const program = new MyCommand(); | ||
program.arguments('<file>'); | ||
expect(program._args.length).toEqual(1); | ||
expect(program._args[0].myProperty).toEqual('MyArgument'); | ||
}); | ||
|
||
test('when override createArgument and createCommand then used for argument of command()', () => { | ||
const program = new MyCommand(); | ||
const sub = program.command('sub <file>'); | ||
expect(sub._args.length).toEqual(1); | ||
expect(sub._args[0].myProperty).toEqual('MyArgument'); | ||
}); |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the benefits of adding this factory when you can use
new Argument()
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main purpose is so you can override the method and return a custom subclass of Argument, which will be used to create the arguments added by Command methods
.argument()
and.arguments()
and.command('sub <file>')
.This factory pattern was first offered with
.createCommand()
where it is more likely to be used, but offering the same pattern for Option and now Argument.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why will you want to override the createArgument method when you can just pass any custom Argument class to addArgument?
Also, in order to create custom argument, you need to somewhat know the internals of the Argument class (in both the factory solution and the direct solution), so it sounds like a bad practice and a doc bloater, for probably a really rare use case. Instead of adding this factory to keep the the api aligned, I would consider removing all other factories...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.createCommand()
was the first one added in #1191 to make Command more subclass friendly..createHelp()
was added in major help refactor in #1365..createOption()
was added in #1380 to follow the pattern. We had established a tidy pattern forCommand
which also worked forHelp
, so following through and offering same support forOption
.Yes, that is implicit in writing a subclass.
I did consider whether to directly support subclassing at all for
Command
, but it a useful approach for extending functionality especially in custom cases which are not common use cases and would not be integrated into Commander itself.