child_process.spawn doc fix, args must escape spaces to get passed correctly. #4157

Closed
wants to merge 1 commit into
from

Projects

None yet

8 participants

@Paretzky

Quoted strings as args do not work, you must escape the space.  Example myprog -o "Long option, that takes a string".  Args would be ["-o", "Long\ option,\ that\ takes\ a\ string"]

@Paretzky Paretzky Changed docs to suggest escaping spaces in args.
Quoted strings as args do not work, you must escape the space.  Example myprog -o "Long option, that takes a string".  Args would be ["-o", "Long\ option,\ that\ takes\ a\ string"]
ca71255

If array is provided, arguments should be escaped automatically, this is my opinion

I'd like to close this if possible, I described the wrong situation. I had an arg of the form -arg "String here". Using spawn's args as ["-arg","String here"] does the right thing; I was trying to use ["-arg 'String Here'] and variations on that.

isaacs commented Oct 20, 2012

Yes, it's an array for a reason :)

@isaacs isaacs closed this Oct 20, 2012
damianb commented Nov 9, 2012

Semi-relevant, is there anything else that should be escaped within a string being passed as args as well? Double-quotes? Single-quotes? I'm seeing some odd behavior in child_process.spawn and windows and I suspect it deals with quotes.

e: example of problem would be:

var ps = child.spawn('wmic', ['process', 'get', 'Caption,Processid,Commandline', '/format:"C:\\code\\evening\\propercsv.xsl"'])

Problem point is the /format arg - backslash escaping of the quotes used with the '/format' results in no change, and changing to single-quotes ends with a slightly different error. All errors are from wmic itself, but it seems node is butchering the args being passed along the way.

This does work however:

var ps = child.exec('wmic process get Caption,Processid,Commandline /format:"C:\\code\\evening\\propercsv.xsl"', function(error, stdout, stderr) {
// ...

...and it does give the right results.

Member

@damianb Please send support questions to the mailing list.

damianb commented Nov 9, 2012

@piscisaureus And documentation issues go to issue trackers. See my edit.

Member

@damianb Very witty, but this response was entirely reasonable given that your original post had only the first paragraph :-(

Member

@damianb Yes, node butchers the command line (using the conventions that the c runtime uses) to provide a reasonable emulation of the unixy "arguments array". In your case the arguments array would be translated to:

wmic process get Caption,Processid,Commandline "/format:\"C:\\code\\evening\\propercsv.xsl\""

In case of wmic this apparently does not work correctly. For this we have the windowsVerbatimArguments option (terrible name imho) which disables butchering. If you set it, arguments are simply separated by spaces. So if you do: var ps = child.spawn('wmic', ['process', 'get', 'Caption,Processid,Commandline', '/format:"C:\\code\\evening \\propercsv.xsl"'], { windowsVerbatimArguments: true }); it should would.

exec does not have this problem (but it has other problems) because it runs the command via the shell, e.g. it does something akin to cmd /c <yourcommandline>.

damianb commented Nov 9, 2012

So, windowsVerbatimArguments...isn't documented at all on the site from what I see in master. Should I open a separate issue for it then?

Owner

So, windowsVerbatimArguments...isn't documented at all on the site from what I see in master. Should I open a separate issue for it then?

Better yet, a documentation pull request.

damianb commented Nov 9, 2012

pr #4259 created. language could probably use improvement, open to criticism/suggested changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment