commander.js: Commander incorrectly assumes that process.argv[0] is 'nodejs' and argv[1] is script name
Command assumes that the first value in process.argv is the name of the nodejs executable being used, and the the second value is the name of the script which was passed to nodejs.
This is mostly fine except in the situation where a packaged electron app is used. For a packaged app a custom binary is used (typically with the name of the app) and it automatically calls the main.js script at start up. It’s process.argv starts with the name of the exe and immediately the first of the user supplied arguments.
A possible solution may be to add an extra method which only takes the list of arguments. The calling application would then be responsible for extracting the real arguments from process.argv. (The method could also just return the parsed results and not modify the commander object. See #183 )
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 8
- Comments: 15
Commits related to this issue
- fix(package): update commander to version 5.0.0 *** ☝️ **Important announcement:** Greenkeeper will be saying goodbye 👋 and passing the torch to Snyk on June 3rd, 2020! [Find out how to migrate to ... — committed to jaebradley/emoji-search-cli by greenkeeper[bot] 4 years ago
- fix(package): update commander to version 5.0.0 *** ☝️ **Important announcement:** Greenkeeper will be saying goodbye 👋 and passing the torch to Snyk on June 3rd, 2020! [Find out how to migrate to ... — committed to jaebradley/github-languages-cli by greenkeeper[bot] 4 years ago
- fix(package): update commander to version 5.0.0 * *** ☝️ **Important announcement:** Greenkeeper will be saying goodbye 👋 and passing the torch to Snyk on June 3rd, 2020! [Find out how to migrate t... — committed to jaebradley/npm-install-search-cli by greenkeeper[bot] 4 years ago
- fix(package): update commander to version 5.0.0 *** ☝️ **Important announcement:** Greenkeeper will be saying goodbye 👋 and passing the torch to Snyk on June 3rd, 2020! [Find out how to migrate to ... — committed to jaebradley/http-status-identifier-cli by greenkeeper[bot] 4 years ago
- fix(package): update commander to version 5.0.0 *** ☝️ **Important announcement:** Greenkeeper will be saying goodbye 👋 and passing the torch to Snyk on June 3rd, 2020! [Find out how to migrate to ... — committed to jaebradley/urban-dictionary-cli by greenkeeper[bot] 4 years ago
What about this:
It works for me using both electron and electron-packager generated executable, and it seems to be the “official” way to handle this.
Obviously implementing this on the library itself would break some existing workarounds, so I would keep the existing behaviour by default, and add a parameter to normalize the arguments. For example something like this:
For some reason the workarounds posted here didn’t really do it for me. So here is mine:
It just forces your process.argv to always start with
['<yourelectronexe>', '.'], so commander can properly parse it. No matter the enviroment you’re in (dev or packaged).Edit: You will run into problems when passing the current directory via
., e.g.meview .. As a temporary workaround pass./or precede the dot with other arguments.Edit 2 Better workaround if you use electron-builder or similar, which renames the electron executable:
In dev mode, your execPath will contain
electronand in production the execPath will have the name of your app as the executable name.it doesn’t work in all scenarios, but this worked when executing with
electron app.jsand executing the electron-builder generated binnary, in this caseapp:Possible API for an options parameter to
.parse()to specify different conventions for the passed arguments:node references: