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
Implicit program "name", deep dive #1569
Comments
I was liking References:
|
Why care so much about the name, which is arguably just cosmetic? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The user can set the program name. If they don't, Commander has a go at finding a name to use. This turns out to be quite hard and less consistent than you might expect! (In practice a user will only be using a subset of all the possibilities, but still some potential for getting caught out.)
constructor
.name()
Implicit, from args
A commonly used method for command-line scripts to find the name of the currently running scripts is to examine the passed arguments. The Node.js documentation for
process.argv
says:Commander uses the parsed arguments to determine the default name if they (should) include the script name. The problem is there are so many ways to launch a node application and not all pass the script name, and platform differences as well.
Passing script directly:
Running script as an executable:
For running via
package.json
in directorypackage-dir
:Running a package returns the name of the package directory (not the script name):
Running an installed binary on Mac and Linux gets the bin name (not the script name):
Running an installed binary on Windows gets the script name (not the bin name):
Implicit, not from args
Since Commander v5, we support parsing arguments which do not include the script name. In this case we fallback to using the main module, from Commander 8 using
require.main.filename
. This does give consistent behaviour for all the launch methods with CommonJs.There is a defect I discovered though, not available when main script is ECMAScript!
Summary
In Commander v8 the default program name may turn out to be the script name, the package directory name, the
package.json
"bin" name, or even null. The default program name from the arguments is different for an npm installed binary on Windows and non-Windows.An additional caveat is that default program name derived from the arguments may not be sensible when running unit tests or other launches when the normal entry script is not in the launch arguments. And the non-arguments approach uses some Node.js state which may not be defined by other applications (e.g. iisnode).
The safe approach is of course to set the name yourself! So Commander does not have to solve all the problems.
The text was updated successfully, but these errors were encountered: