美文网首页nodejsNode
nodejs命令行执行时带参数

nodejs命令行执行时带参数

作者: 深思海数_willschang | 来源:发表于2016-09-26 11:47 被阅读13911次

    今天项目里突然想在初始化时跑一些数据,于是想起以前在python时可以在命令行里带参数运行命令的,经过百度后确实也是有的。

    ** process.argv**

    //想获得命令行后面的几个参数值
    /*
    //node arg.js arg1 arg2 arg3, 想取得这三个参数
    //即可以程序中用:
    var args = process.argv.splice(2)
    //process是一个全局对象,argv返回的是一组包含命令行参数的数组。
    //第一项为”node”,第二项为执行的js的完整路径,后面是附加在命令行后的参数
    */
    
    nodejs-arguments.png

    代码如下:

    var arguments = process.argv.splice(2);
    console.log('所传递的参数是:', arguments);
    
    //////////////////////////
    // print process.argv
    process.argv.forEach(function (val, index, array) {
      console.log(index + ': ' + val);
    });
    
    

    process.argv is an array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.

    来自stackoverflow

    相关文章

      网友评论

        本文标题:nodejs命令行执行时带参数

        本文链接:https://www.haomeiwen.com/subject/fzlqyttx.html