驴子
Yargs 是一个节点.js 库 fer hearties 尝试' ter 解析选择字符串
说明
Yargs帮助您构建交互式命令行工具,方法是解析参数并生成优雅的用户界面.
它给你:
- 命令和(分组)选项(
my-program.js serve --port=5000) (中文(简体) ). - 基于您参数的动态生成的帮助菜单 :
mocha [spec..]
Run tests with Mocha
Commands
mocha inspect [spec..] Run tests with Mocha [default]
mocha init <path> create a client-side Mocha setup at <path>
Rules & Behavior
--allow-uncaught Allow uncaught errors to propagate [boolean]
--async-only, -A Require all tests to use a callback (async) or
return a Promise [boolean]
- 命令和选项的崩溃补全快捷键 .
- 财务报告和审定财务报表 吨多一点。 。 。 .
安装
稳定版本 :
npm i yargs
具有最新特性的出血边缘版本:
npm i yargs@next
使用量
简单示例
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv
if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!')
} else {
console.log('Retreat from the xupptumblers!')
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!
$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!
说明:
hideBin是一个缩写process.argv.slice(2)它的好处在于它考虑到某些环境中的差异,例如, 电子。 。 。 .
复杂示例
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
yargs(hideBin(process.argv))
.command('serve [port]', 'start the server', (yargs) => {
return yargs
.positional('port', {
describe: 'port to bind on',
default: 5000
})
}, (argv) => {
if (argv.verbose) console.info(`start server on :${argv.port}`)
serve(argv.port)
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging'
})
.parse()
运行上面的示例 --help 以查看应用程序的帮助.
支持的平台
类型脚本
yargs 有类型定义 @ types/ 亚尔格。 。 。 .
npm i @types/yargs --save-dev
参见使用示例 文档。 。 。 .
德诺
截止 v16, (中文(简体) ), yargs 支持 德诺数字 :
import yargs from 'https://deno.land/x/yargs/deno.ts'
import { Arguments } from 'https://deno.land/x/yargs/deno-types.ts'
yargs(Deno.args)
.command('download <files...>', 'download a list of files', (yargs: any) => {
return yargs.positional('files', {
describe: 'a list of files to do something with'
})
}, (argv: Arguments) => {
console.info(argv)
})
.strictCommands()
.demandCommand(1)
.parse()
无害环境管理
截止 v16, (中文(简体) ),yargs 支持无害环境管理进口:
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
yargs(hideBin(process.argv))
.command('curl <url>', 'fetch the contents of the URL', () => {}, (argv) => {
console.info(argv)
})
.demandCommand(1)
.parse()
浏览器中的用法
查看浏览器中使用 yargs 的例子 文档。 。 。 .
社区
有问题吗? 想贡献吗? 加入我们的行列 社区松懈。 。 。 .
文档
目录
支持的节点.js版本
这一生态系统的图书馆尽最大努力追踪 节点Js的发布时间表这里 (单位:千美元) 贴上我们为什么认为 这很重要。 。 。 .