nodejs传递参数
2020-12-29 14:28
标签:receive min let lis trying RoCE imp cli pass Passing in arguments via the command line is an extremely basic programming task, and a necessity for anyone trying to write a simple Command-Line Interface (CLI). In Node.js, as in C and many related environments, all command-line arguments received by the shell are given to the process in an array called Node.js exposes this array for every running process in the form of Now save it, and try the following in your shell: There you have it - an array containing any arguments you passed in. Notice the first two elements - Where everyday CLI arguments are concerned, you‘ll want to skip the first two. Now try this in This yields: Now let‘s actually do something with the args: JS PRO TIP: Remember to nodejs传递参数 标签:receive min let lis trying RoCE imp cli pass 原文地址:https://www.cnblogs.com/chucklu/p/14180166.htmlHow to parse command line arguments
argv
(short for ‘argument values‘).process.argv
- let‘s take a look at an example. Make a file called argv.js
and add this line:console.log(process.argv);
$ node argv.js one two three four five
[ ‘node‘,
‘/home/avian/argvdemo/argv.js‘,
‘one‘,
‘two‘,
‘three‘,
‘four‘,
‘five‘ ]
node
and the path to your script. These will always be present - even if your program takes no arguments of its own, your script‘s interpreter and path are still considered arguments to the shell you‘re using.argv.js
:var myArgs = process.argv.slice(2);
console.log(‘myArgs: ‘, myArgs);
$ node argv.js one two three four five
myArgs: [ ‘one‘, ‘two‘, ‘three‘, ‘four‘, ‘five‘ ]
var myArgs = process.argv.slice(2);
console.log(‘myArgs: ‘, myArgs);
switch (myArgs[0]) {
case ‘insult‘:
console.log(myArgs[1], ‘smells quite badly.‘);
break;
case ‘compliment‘:
console.log(myArgs[1], ‘is really cool.‘);
break;
default:
console.log(‘Sorry, that is not something I know how to do.‘);
}
break
after each case
- otherwise you‘ll run the next case too!//console.log(process.argv);
var myArgs = process.argv.slice(2);
//console.log(‘myArgs: ‘, myArgs);
const dateTime = require(‘date-time‘);
var currentTime = dateTime({local: true, showTimeZone: true, showMilliseconds: true});
console.log(`${myArgs[0]} at ${currentTime}`)
上一篇:js判断 早上晚上
下一篇:js:表单校验(获取元素、事件)