12

How can I get the raw command line arguments in a node.js app, given this example command:

node forwardwithssh.js echo "hello arguments"

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

And there's no way to reconstruct the original echo "hello arguments" from that (ie. join(" " won't put quotes back). I want the whole original raw string after the script name.

what I want is easily obtained in bash scripts with "$*", is there any equivalent way to get that in node.js?

Note: the intention in particular is to receive a command to be executed somewhere else (eg. thru ssh)

8
  • This module should be able to do what you want: npmjs.org/package/minimistCommentedJul 21, 2014 at 20:30
  • 1
    There is no way to tell if the original argument was specified as "hello arguments", 'hello arguments' or hello\ arguments. Those are all the same in bash. Do you want to just always assume that an arg with a space was originally quoted with double quotes? It's easy to see that the space must have been escaped somehow, because you get one arg instead of two for "hello arguments".
    – Paul
    CommentedJul 21, 2014 at 20:31
  • You could quote all your args process.argv.map(function(arg){ return '"' + arg + '"'; });. Which will give you: "node" "forwardwithssh.js" "echo" "hello arguments", which is the exact same in bash as your original command .
    – Paul
    CommentedJul 21, 2014 at 20:36
  • @BrianGlaz minimist has the same problem, you can see after the -- the arguments are parsed too. @Paulpro I can just add quotes to everything, I want to forward the raw argument as a command to be executed somewhere else
    – Benja
    CommentedJul 21, 2014 at 20:36
  • @Paulpro "$*" in bash does gives u the exact raw string (which I need as it is another command), doesn't just put quotes everywhere
    – Benja
    CommentedJul 21, 2014 at 20:39

1 Answer 1

4

Wrap each of the args in single quotes, and escape and single quotes within each arg to '\'':

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

That would give you a cmd_string containing:

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

which can be run directly in another shell.

4
  • I was afraid adding single quotes wouldn't work on shell, but you're right, I would like to now have to deal with escaping, and be able to log the original command... but this seems to be the best solution available
    – Benja
    CommentedJul 22, 2014 at 22:34
  • It does not give original command arguments. 'node x.js y' and 'node x.js "y"' will give the same process.argv.
    – Kos
    CommentedFeb 28, 2017 at 18:14
  • @wandalen Those are evaluated identically in a shell before node is even invoked. If you want to pass quotes in from a shell you would need node x.js \'y\' or node x.js "'y'".
    – Paul
    CommentedFeb 28, 2017 at 18:55
  • 1
    @Paulpro goal is not passing quotes but getting original invocation which could be made not by shell. But you are write, I feel it's not possible on Unix-like systems. Windows has GetCommandLine.
    – Kos
    CommentedMar 1, 2017 at 0:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.