Node.js:如何将参数的值从终端传递到JS脚本 [英] Node.js : how to pass parameter's value from terminal to JS script

查看:712
本文介绍了Node.js:如何将参数的值从终端传递到JS脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个基于jsdomsvgcreator.node.js脚本文件:

Given a jsdom based svgcreator.node.js script file :

var jsdom = require('jsdom');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK
  [ 'http://d3js.org/d3.v3.min.js',    // JS DEPENDENCIES online ...
  'js/d3.v3.min.js' ],                 // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
  function (err, window) {
    var svg = window.d3.select("body")
        .append("svg")
        .attr("width", 100)
        .attr("height", 100);
    svg.append("rect")
        .attr("id", "rect1")
        .attr("x", 10)
        .attr("y", 10)
        .attr("width", 80)
        .attr("height", 80)
        .style("fill", "green");
    // END svg design

  //PRINTING OUT SELECTION
    console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

鉴于我使用NodeJS终端命令运行它并生成一个output.svg文件:

Given I use NodeJS terminal command to run it and generate a output.svg file :

node svgcreator.node.js > output.svg  # nodeJS + script command

如何将参数值从终端传递到NodeJS?

测试依赖项:

  • svgcreator.node.js github repository: git clone 'git@github.com:hugolpz/svgcreator.node.js.git'
  • jsdom required, use : sudo npm install -g jsdom (global).

使用的解决方案(@Matt_Harrison):我们依靠process.env.myVar

svgcreator.node.js JS代码:

svgcreator.node.js JS code :

var jsdom = require('jsdom');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK:
  [ 'http://d3js.org/d3.v3.min.js',    // JS DEPENDENCIES online ...
  'js/d3.v3.min.js' ],                 // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
  function (err, window) {

    var color = process.env.COLOR;     // <<################# IMPORTANT !!
    var svg = window.d3.select("body")
        .append("svg")
        .attr("width", 100)
        .attr("height", 100);
    svg.append("rect")
        .attr("id", "rect1")
        .attr("x", 10)
        .attr("y", 10)
        .attr("width", 80)
        .attr("height", 80)
        .style("fill", color);         // <<################# IMPORTANT !!
    // END svg design

  //PRINTING OUT SELECTION
    console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

终端NodeJS命令:

COLOR=#66AAFF node svgcreator.node.js > out.svg   # <<############# IMPORTANT !! setting the value.

+1 @Matt_Harrison的答案,问题得到赞赏!

+1 @Matt_Harrison answer and the question appreciated !

推荐答案

在您的终端中,您可以使用环境变量 :

In your terminal, you can use environment variables:

$ COLOR=#FFFFFF node jsdom.node.js

在您的JS中,执行以下操作:

In your JS, do:

var color = process.env.COLOR;


或者您可以在命令中添加其他参数:


Or you could add extra arguments to the command:

$ node jsdom.node.js '#FFFFFF'

和您的JS中:

var color = process.argv[2];


如果要使用库,请执行以下操作:我建议您查看 Minimist 库或


If you want to use a library; I would advise looking into the Minimist library, or Commander for a more fully-featured solution.

这篇关于Node.js:如何将参数的值从终端传递到JS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆