-
-
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
Rework default program name, and searching for subcommands #1571
Merged
shadowspawn
merged 23 commits into
tj:release/9.x
from
shadowspawn:feature/discovering-context
Aug 6, 2021
Merged
Rework default program name, and searching for subcommands #1571
shadowspawn
merged 23 commits into
tj:release/9.x
from
shadowspawn:feature/discovering-context
Aug 6, 2021
Conversation
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
And some lint, remove trailing spaces et al
Add mention of .executableDir
abetomo
approved these changes
Aug 6, 2021
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.
LGTM 👍
Commander v9 has been released. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pull Request
The classic approach for scripts to find how they are called is to check the passed arguments, which often start with the interpreter and the script name. e.g.
['/usr/local/bin/node', '/Users/developer/test.js', '--print']
. However, this is not always available or consistent for Commander use cases.The implied script path found in
.parse()
affects the default program name, and searching for subcommands if stand-alone executable commands are used.Problem
require.main.filename
fallback has introduced different (arguably better) behaviours than script name, but can't be made primary behaviour because not available in es6executableFile
per commandNote: we do have
.name()
andexecutableFile
, so there is technically a way for user to get things working.Deep dive: #1569
Related (including some issues for which
executableFile
is a solution): #481 #521 (comment) #532 #714 #785 #786 #826 #830 #875 #890 #912 #1044 #1417 #1439Solution
Node.js itself does not offer universal solutions for finding a reference script name or a directory which we can use from inside Commander. There are some approaches that client can use. e.g.
__filename
,__dirname
,process.argv[1]
,require.main.filename
,import.meta.url
.Keep it simple. The script name is the classic mechanism, and Commander has always used it as the primary implicit context. Stick with it, and improve the documentation, and improve custom control.
Expand documentation about naming commands, and especially program. Rework documentation about finding subcommands.
Remove fallback to
require.main.filename
since it adds yet more behaviours, and only a partial solution as not available in es6.Use program (command) name rather than script name as prefix for looking for standalone executable subcommands. Include a backwards compatible fallback subcommand lookup using the script name.
Add
.executableDir()
for simple custom configuration, including when script name not supplied. (c.f..executableFile
)(Includes some additional minor markdown and example code fixes.)
ChangeLog
.executableDir()
for custom search for subcommandsexecutableFile
require.main.filename
when script not known from arguments passed to.parse()
(can supply details using.name()
, and.executableDir()
orexecutableFile
)executableFile