tj / commander.js

node.js command-line interfaces made easy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle the case when node called with the "-e" argument

atleta opened this issue · comments

When you call node with the -e argument and supply the script (commands) to execute on the command line then the script parameter will be missing from process.argv (at index 1) so automatic parsing ({from: 'node'}) will fail.

As far as I can see this case can be handled by looking into process.execArgv and checking for the presence of the -e or --eval option.

(One might use this option to run non-trivial scripts e.g. when calling a node process in a docker container.)

For interest, did you encounter this lack in use or noticed in code?

Yes, this is a case we could handle.

I wrote code for this in parseargs where node eval/print was a use case being considered:

I ran into this when I changed the way I called a script (running in a docker instance) from another app.

It took a bit of investigation to figure out what caused the problem as node is not my main development environment and I'd say, this definitely is a surprising behavior. Knowing what the problem is, the workaround is 2 lines of code but, I think, it's better to take care of it in a higher-level library, like this.

I have opened a PR which will autodetect node --eval and node --print when no arguments are specified to .parse(). To avoid breaking existing code or getting weird test results with custom argv depending on how node is launched, { from: 'node' } still assumes "normal" node conventions.

(And updated documentation to match.)

Released in v12.1.0