简体   繁体   中英

When run node file arguments cut off double quotes

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)))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM