Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

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

Closed
wants to merge 1 commit into from

Conversation

Paretzky
Copy link

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"]

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"]
@nickpoorman
Copy link

+1

1 similar comment
@gatesphere
Copy link

+1

@langpavel
Copy link

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

@Paretzky
Copy link
Author

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
Copy link

isaacs commented Oct 20, 2012

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

@isaacs isaacs closed this Oct 20, 2012
@katanacrimson
Copy link

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.

@piscisaureus
Copy link

@damianb Please send support questions to the mailing list.

@katanacrimson
Copy link

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

@piscisaureus
Copy link

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

@piscisaureus
Copy link

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

@katanacrimson
Copy link

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?

@bnoordhuis
Copy link
Member

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.

@katanacrimson
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants