Getting raw command line arguments in node.js

How to get the original command line arguments in a node.js application, given this command:

node forwardwithssh.js echo "hello arguments"

process.argv will be [ "node", "/path/to/forwardwith.js", "echo", "hello arguments" ]

And there is no way to restore the original echo "hello arguments" from this (that is, join(" " will not return quotes). I want the entire original source line after the script name.

what i want is easy to get in bash scripts with "$*" , is there an equivalent way to get this in node.js?

Note. In particular, the goal is to get a command to execute elsewhere (for example, via ssh)

+6
source share
1 answer

Wrap each of the arguments in single quotes and highlight the single quotes in each argument '\'' :

 var cmd_string = process.argv.map( function(arg){ return "'" + arg.replace(/'/g, "'\\''") + "'"; }).join(' '); 

This will give you a cmd_string containing:

 'node' '/path/to/forwardwith.js' 'echo' 'hello arguments' 

which can be run directly in another shell.

+3
source

Source: https://habr.com/ru/post/972614/


All Articles