javascript - 我将如何配置 yargs 以获取单个输入文件和可选的输出文件?
问题描述
我正在使用yargs来配置以下命令行选项:
Usage: myapp <source file> [destination file]
没有选项,没有命令等。只需读取一个文件路径和一个可选的写入路径。什么是 yarg 配置让我得到它并给我所有好的文档?
似乎我需要使用选项或命令。我不能自己传递文件参数吗?
也看不到指挥官如何做到这一点。试图从 开始交易process.argv
,但在第一关卡住了。
解决方案
以下是如何获取第一个和第二个参数的示例:
var argv=require('yargs').argv
var source=argv._[0]
var dest=argv._[1]
甚至更好的方法:
const argv = require("yargs").command(
"$0 <source file> [destination file]",
"the default command",
() => {},
function({ sourcefile, destinationfile }) {
console.log({ sourcefile, destinationfile })
},
).argv
$0
是默认命令。输出应该是:
myapp.js <source file> [destination file]
the default command
Options:
--help Show help [boolean]
--version Show version number [boolean]
Not enough non-option arguments: got 0, need at least 1
学到更多:
推荐阅读
- c - 为指向指针数组的指针赋值
- javascript - 如何从浏览器控制台更改 Facebook 帖子文本?
- python - 熊猫根据几列减去数据框中的行
- jmeter - 无需登录多次验证令牌
- javascript - 如何在 HTML 中使用数据属性名称和值 - 略
- javascript - 创建一个带有钳位和焦点的 React CSS 对象
- easyadmin - 如何使用 EasyAdmin 2 和 A2lix TranslationFormBundle 将文本编辑器类型用于翻译类型
- python - 从未等待协程 discord.py
- javascript - 反应原生:swiper,第一次显示时出现问题
- javascript - Bootstrap 5.1.3 导航栏点击后不折叠