我试了又试,但就是不能消化Yargs的docs。
我需要创建一组命令/子命令:
~$framework generate routed-module ModuleOne ModuleTwo ModuleThree --animation-style=bounce-up
//would call a handler with:
{ modules: string[], options: {animationStyle?: AnimationStyle}}
type AnimationStyle = 'bounce-up' | 'slide-left'或
~$framework generate stateful-ui ModuleOne ModuleTwo ModuleThree
//would call a handler with:
{ modules: string[]}或
~$framework init
//would just call a handler我想要别名:g代表generate,r代表routed-module,s代表stateful-ui。
自动补全会很好。
这是我试过的,不知道该怎么做:
yargs
.scriptName('elm-framework')
.command({
command: 'generate [moduleType] [moduleNames]',
aliases: ['g'],
describe: 'Generates a resource',
handler: config.handleModuleGeneration,
builder: {
moduleType: {
demand: true,
choices: ['routed', 'stateful'] as const,
default: 'routed',
},
moduleNames: {
demand: true,
array: true,
},
},
})谢谢!
(不需要使用typescript执行此操作。我主要想了解如何使用该库的api。)
发布于 2020-01-24 23:02:29
使用this crucial piece of documentation解决了这个问题
yargs
.scriptName('framework')
.command({
command: 'generate [moduleType] [moduleNames...]',
aliases: ['g'],
describe: 'Generates a resource',
handler: parsed => console.log('your handler goes here', parsed),
builder: {
moduleType: {
demand: true,
choices: ['routed', 'stateful'] as const,
default: 'routed',
},
moduleNames: {
demand: true,
array: true,
},
},
}).parse(process.argv.slice(2))发布于 2021-05-29 03:52:15
对我来说,实现这一点的最好方法是关注this Github问题,特别是在this comment中提出的建议。(嵌套在命令builder中的另一个builder )。
使用上面的建议('generate [moduleType] [moduleNames...]')的问题是,您不能对不同的子命令使用不同的标志。
https://stackoverflow.com/questions/59894389
复制相似问题