-
-
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
Add construct option to customise creation of a command #1185
Conversation
Initial reaction: probably no. However, I'll consider it some more before posting a longer reply. |
After consideration...
I suggest two possible approaches which are new in v5 (and do not require messy override of a) A partial solution is to use b) Better, you could wrap the Fair warning, |
Yeah, i understand, most likely will rewrite my own stuff with composition in mind instead of inheritance. Here's counter-arguments though:
class CustomCommand extends Command {
command(name, opts) { // for simplicity args without stand-alones
return super.command(name, { construct: (name) => new CustomCommand(name), ...opts });
}
} So after you redefined
So in summary, from my point of view, after this PR there should not be any other one connected directly to subclassing (as it will be fully supported) and it is not as work-aroundy as you think and it is only two-three lines of code (1.5 in code + 1 in types, not counting tests and possible documentation though...). But i understand if you don't want even to think about subclassing and close this PR (feel free to do so, no hard feelings). As i said i will rewrite my stuff in composition then. |
Good comments, thanks, and thanks for being understanding too.
Oh, that is tidier than I had in mind! I'll take another look in case that changes my thinking. The potential
I was thinking these would go in factory function if necessary. (i.e. using public functions to configure)
I was not expecting custom classes used with an executable.
Oops, that might be an omission with |
Out of these settings:
Only child.storeOptionsAsProperties(parent.opts() !== parent.opts()) if |
I did some experimenting with a factory routine: #1191 |
Pull Request
Problem
Want a way to customise creation of a subcommand through
command
method, mainly to use my subclass as a constructor. Without this option i would have to overridecommand
method and copy-paste code from originalcommand
method which is definitely not an optimal way.Solution
Add
construct
option which if present is responsible for creating a command instance.Can also make types more specific with generics, changing these lines:
But wanted to keep changes small, so not included these changes to types.