传送门:
# examplehttps://github.com/yargs/yargs/blob/master/docs/examples.md# 官网http://yargs.js.org/# githubhttps://github.com/yargs/yargs
Example1:
index.js
const yargs ??= require(‘yargs‘)const argv ???= yargs.alias(‘n‘, ‘name‘).alias(‘p‘, ‘path‘).argv
console.log(argv, argv.name, argv.n, argv.p, argv.path);
命令这样执行: node ./index.js --name 123 --path 456
当然也可以这样执行:node ./index.js -n 123
但建议还是使用 ‘--‘ 避免冲突。
Example2: 无参数的值如何获取? argv._
nonopt.js:
#!/usr/bin/env nodevar argv = require(‘yargs‘).argv;console.log(‘(%d,%d)‘, argv.x, argv.y);console.log(argv._);
$ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho(0.54,1.12)[ ‘me hearties‘, ‘yo‘, ‘ho‘ ]
Nodejs 命令行交互神奇 yargs
原文地址:https://www.cnblogs.com/CyLee/p/8496018.html