commander.js icon indicating copy to clipboard operation
commander.js copied to clipboard

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

Open atleta opened this issue 11 months ago • 3 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.)

atleta avatar Mar 01 '24 19:03 atleta

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:

  • https://github.com/pkgjs/parseargs/pull/20

shadowspawn avatar Mar 01 '24 22:03 shadowspawn

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.

atleta avatar Mar 04 '24 15:03 atleta

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.)

shadowspawn avatar Mar 05 '24 08:03 shadowspawn

Released in v12.1.0

shadowspawn avatar May 18 '24 23:05 shadowspawn