- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我找不到正确配置位置参数的方法。我有这个代码:
#!/usr/bin/env node
const create = (argv) => {
console.log('create component with name:', argv.name)
}
const createBuilder = (yargs) => {
yargs.positional('name', {
desc: 'Name of the new component',
})
}
/* eslint-disable no-unused-expressions */
require('yargs')
.command({
command: 'create <name>',
desc: 'Create a new component',
builder: createBuilder,
handler: create,
})
.demandCommand(1, 'A command is required')
.help()
.argv
并且我想提供自定义错误消息,以防用户在创建命令后未指定名称。
从文档中我不清楚如何做到这一点,在处理 github 问题时我遇到了这个评论 (#928):
I recommend instead using demandCommand and demandOption (each of which are documented).
These allow you to configure positional arguments and flag arguments separately
我尝试过各种组合
.demandCommand(1, 'You need to provide name for the new component')
或
.demandOption('name', 'You need to provide name for the new component')
但运气不好。有人知道该怎么做吗?
最佳答案
tl;dr - 尝试通过将字符串 *
或 $0
添加到命令名称的开头,使您的命令成为默认命令。
我发现,只有在 default command 中定义位置参数时,才会考虑位置参数(即:显示在帮助菜单中,并在未提供所需位置时抛出错误)。 .
以下是让它与您的代码一起使用的示例(请注意,您不再需要使用 .demandCommand()
):
require('yargs')
.scriptName('cli-app')
.command({
command: '$0 create <name>',
desc: 'Create a new component',
builder: yargs => {
yargs.positional('name', {
desc: 'Name of the new component'
});
},
handler: function(argv) {
console.log('this is the handler function!');
}
})
.help().argv;
输出(注意最后的“没有足够的非选项参数:有 1,需要至少 2”行):
➞ node cli-app.js create 1 ↵
cli-app create <name>
Create a new component
Positionals:
name Name of the new component
Options:
--version Show version number [boolean]
--help Show help [boolean]
Not enough non-option arguments: got 1, need at least 2
关于node.js - Yargs - 如何为省略的位置参数提供自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47304347/
有人非常了解 npm yargs 吗?我正在将此包用于命令行参数。但是我找不到“构建器”的解释。我在哪里可以买到这个? yargs.command({ command: 'test',
我正在使用 yargs 为传递给脚本的参数添加命令行选项。当我发出带有脚本名称的帮助命令时,它不显示添加 参数的帮助。 const yargs=require('yargs'); const arg
我一直在尝试,但就是无法消化 Yargs 的 docs . 我需要创建一组命令/子命令: ~$framework generate routed-module ModuleOne ModuleTwo
有人可以解释为什么下面的脚本没有显示所有命令的帮助吗? test.js [command] Commands: test.js list-spaces List spaces Options:
我正在尝试为我正在从事的项目开发一个模块。在将一些核心代码拆分到一个单独的目录并尝试使用 import Noxel from 'noxel' 包含该模块后,我收到了大量警告/错误。 node_modu
我目前在一个应用程序中使用 yargs,该应用程序可以不带参数运行,也可以不带任何参数运行: a '-o' option with exactly one argument a '-f' option
我正在使用 yargs 创建一个带有“build”、“link”、“clean”等子命令的构建工具。 我希望能够键入不带参数的 ./build.js 并默认调用“build”子命令处理程序。 我是这样
我正在玩 yargs如要配置以下命令行选项: Usage: myapp [destination file] 没有选项,没有命令等。只需要一个文件路径来读取和一个可选的文件路径来写入。是什么 yar
我正在构建一个 Node CLI 应用程序,需要将单个文件作为参数传递。 例如: myapp.js /Users/jdoe/my_file.txt 我知道我可以通过 yargs 中的 _ 对象引用 /
我正在教程中开发一个node.js命令行天气应用程序,我意识到当我输入一个字符串作为输入时,仅采用第一个单词,该字符串被拆分为一个单词数组,并且仅第一个单词返回 app.js const yargs
如何从命令的构建器对象中要求我的选项之一 require('yargs') .usage('Usage: $0 [options]') .command( 'read', '
我正在尝试使用 yargs 验证一些参数,如下所示: var args = require('yargs') .command('comando', 'comp
我找不到正确配置位置参数的方法。我有这个代码: #!/usr/bin/env node const create = (argv) => { console.log('create compone
我正在使用 yargs 获取 CLI 参数。我想知道命令和选项之间的区别。 const argv = yargs .command( 'add', 'Add a new note', {
我正在尝试使用 yargs 实现一个 restart 命令。我已经实现了 start 和 stop 命令,现在我想做的就是在 restart 命令中调用这些现有命令。 不幸的是,仅仅使用 yargs.
我正在使用yargs来验证用于数据加载助手库的cli参数。 我希望能够在允许脚本运行之前检查文件是否存在,这与fs.accessSync(filename, fs.R_OK);一样。但是,如果文件不存
我正在使用 yargs 在 gulp 命令中传递参数。我在检查参数的有效性以及标志是否存在时会发生什么情况时遇到了麻烦。 更具体地说,这是我的 gulpfile.js: 'use strict'; v
我正在尝试使用 yarg 添加命令,但是当我运行代码时,我的命令未添加。 这是我正在尝试的: const yargs = require('yargs') //create add command y
我正在使用 Node JS 创建脚本,并希望使用 yargs 启用异步命令行完成功能. completion section of the yargs documentation说:“将生成的脚本连接
我正在尝试运行 yargs.command 命令。我尝试运行此代码片段: import yargs from "yargs"; yargs.command({ command: "list",
我是一名优秀的程序员,十分优秀!