[Node.js] Pass command line arguments to node.js

Command line arguments are often used to modify the behavior of an application or specify needed parameters for operation. In this lesson, you will learn how to access the command line arguments passed to your node.js application as well as different strategies for evaluating and accessing them.

To catch what arguement user passed in from command line, we can use ‘process.argv‘.

If we log out:

// server.js

console.log(process.argv)

Then we run the command:

node server.js foo bar soo

In the console, it log out this:

[ ‘/usr/local/bin/node‘,
  ‘/Users/zhentianwan/Documents/Programming/Node/nol/server‘,
  ‘foo‘,
  ‘bar‘,
  ‘soo‘ ]

Here the ‘foo bar soo‘ are meaningless, but in the real case, the argv user pass in might be something we need, but the order might be differet, for example, sometime you do:

npm i ramda --save

Somtime you might:

npm i --save ramda

The order of command can be different.

To make sure what user has pass in, we can do:

if (process.argv.indexOf(‘foo‘) != -1) { console.log(‘Yeah! We are going to foo!‘); }
时间: 2024-10-13 00:17:45

[Node.js] Pass command line arguments to node.js的相关文章

Pass command line arguments to Gnuplot script

Gnuplot is a light-weight and easy to use  scientific plotting tool. Its has enough functionalities to deal with most of my demands on visualizing experimental data. If heavy computation is needed, GNU Octave can be used, of which the plotting backen

msiexec command line arguments

Documented command line arguments Type MSIEXEC /? and you'll get the following on-screen help: Windows ® Installer. V 5.0.7601.17514 msiexec /Option <Required Parameter> [Optional Parameter] Install Options </package | /i> <Product.msi>

An annotation based command line parser

JCommander star1000+ This is an annotation based parameter parsing framework for Java 8. The full doc is available at http://jcommander.org. Java port of Python's famous argparse command-line argument parser. https://argparse4j.github.io/ Argparse4j

《Node.js入门》Windows 7下Node.js Web开发环境搭建笔记

近期想尝试一下在IBM Bluemix上使用Node.js创建Web应用程序.所以须要在本地搭建Node.js Web的开发測试环境. 这里讲的是Windows下的搭建方法,使用CentOS 的小伙伴请參考:<Node.js入门>CentOS 6.5下Node.js Web开发环境搭建笔记 Node.js是什么? 我们看看百科里怎么说的? JavaScript是一种执行在浏览器的脚本,它简单,轻巧.易于编辑,这样的脚本通经常使用于浏览器的前端编程.可是一位开发人员Ryan有一天发现这样的前端式

JS一般般的网页重构可以使用Node.js做些什么(转)

一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.js,且很有可能对Node.js都没有一个比较立体的认识——知道这玩意可以跑服务,构建很多前端工具,看上去很厉害的样子,但是,可能就仅限于此了. “那可否三言两语概括Node.js的林林总总呢?” “不可!” “那怎么办?” “那就六言四语!” 首先,要知道,Node.js一个JavaScript运行

[Selenium+Java] Selenium Grid Tutorial: Command Line and JSON Example

Original URL: https://www.guru99.com/introduction-to-selenium-grid.html What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in para

Command Line Skills

Part 1: Command Line Interface(CLI) The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer kn

How to build .apk file from command line(转)

How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want to install a number of programs for building your Android project, this article is for you. You will need only JDK, the Android SDK platform tools and

Phalcon下命令行应用(command line applications)

今天打算在phalcon项目下加入一个定时任务,来发送邮件. 看了官网的例子,配置和运行都少许杂乱,还有个bug,见备注, 我这边主要讲解如何将command line 的配置独立出来,如果初上手的同学,建议先运行官网第一个例子,成功后,再实践一下官网第二个例子,出现报错,就看我备注,然后再看我这如何将配置独立处理. 借鉴思想:主要借鉴Yii框架中,类似功能commands的配置console.php. 第一步:在app下的tasks文件夹下建三个文件: cl_config.php    //直