如何使用Yargs实现多个子命令?

问题描述 投票:0回答:1

我一直在尝试,但是无法消化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

我想要别名:ggeneraterrouted-modules代表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,
        },
      },
    })

谢谢!

(不必使用打字稿进行此操作。我主要是想了解如何使用库的api。)

javascript node.js typescript command-line-interface yargs
1个回答
0
投票

使用this crucial piece of documentation找出来:

  yargs
    .scriptName('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,
        },
      },
    }).parse(process.argv)
© www.soinside.com 2019 - 2024. All rights reserved.