1

I created node app that should be run through terminal with some parameters like:

node index.js --param1="test" --body=<div style="font-family: Lato;">

Problem is that when I run this in args I have:

[ '--param1=test',
  '--body=<div style=font-family:',
  'Lato' ]

I need to pass HTML content inside through parameter but quotes are always stripped off. Is there any way to get it without putting \" before every quote?

(function(args){
    console.log(process.argv.slice(2));
}(process.argv.slice(2)))
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1110
  • 7,829
  • 55
  • 176
  • 334
  • _“Problem is that when I run this in args I have:”_ - yeah, not really though, right? `Lato` probably did not magically turn into `Arial`, and I’d rather expect the value of the body parameter to be just `
    – CBroe Mar 19 '20 at 12:47
  • Arial was a typo sorry. I just realise that even `--body="
    ` is not solution. I convert this node to exe file and execute from sql server. But can't figure why it stripe quotes always.
    – 1110 Mar 19 '20 at 12:51
  • 1
    thats generally upto your shell on how it parses the string. – Daniel A. White Mar 19 '20 at 12:51
  • This exe is executed in windows env from ms sql – 1110 Mar 19 '20 at 12:52
  • how are you invoking it from mssql exactly? – Daniel A. White Mar 19 '20 at 12:53
  • 1
    For Windows command line, you might need to involve `^` for escaping, or double the quotes, see https://stackoverflow.com/a/55939478/1427878 – CBroe Mar 19 '20 at 12:54
  • `SET @qry = 'C:\PROGRA~1\SyncData\index.exe ' + @args + ''; EXEC master..xp_CMDShell @qry;` It produces a string like: `C:\P...\index.exe --body="
    "`
    – 1110 Mar 19 '20 at 12:56
  • alright i would try what @CBroe suggested as this calls a member `ShellExecute` function. – Daniel A. White Mar 19 '20 at 12:58
  • I just tried `--body="
    "` this gives me `The syntax of the command is incorrect.`
    – 1110 Mar 19 '20 at 13:01
  • just a suggestion - why not write the html to a file on disk and have your node utility read it and write it back to a file? – Daniel A. White Mar 19 '20 at 13:02
  • Impossible as everything must be dynamically generated :( we used sp_SendMail from sql server to send mails but now we need to make outside of sql in a form of exe to pass HTML to it and send email. – 1110 Mar 19 '20 at 13:04
  • but can you dynamically create the file? https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/xp-cmdshell-transact-sql?view=sql-server-ver15#e-capturing-the-result-of-a-command-to-a-file (i.e. do this but the opposite) – Daniel A. White Mar 19 '20 at 13:07
  • No can’t save file content anywhere – 1110 Mar 19 '20 at 13:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209925/discussion-between-daniel-a-white-and-1110). – Daniel A. White Mar 19 '20 at 13:12

1 Answers1

0

Escape the quotes in the parameter value:

node index.js --param1="test" --body="<div style=""font-family: Lato;"">"

process.argv.slice(2).forEach(param => {
    let equalPos = param.indexOf("=");

    if (equalPos > 0) {
        let name = param.substr(0, equalPos).replace(/^-+/, "");
        let value = param.substr(equalPos + 1);
        console.log(`Arg: ${name} = ${value}`);
    }
});

Result:

Arg: param1 = test
Arg: body = <div style="font-family: Lato;">
Mike D Sutton
  • 716
  • 3
  • 9