How to implement multiple subcommands with Yargs?

Bir*_*sky 8 javascript command-line-interface node.js typescript yargs

I've been trying and trying, but just couldn't digest Yargs' docs.

I need to create a set of commands/subcommands:

~$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'

Run Code Online (Sandbox Code Playgroud)

or

~$framework generate stateful-ui ModuleOne ModuleTwo ModuleThree
//would call a handler with:
{ modules: string[]}

Run Code Online (Sandbox Code Playgroud)

or

~$framework init
//would just call a handler
Run Code Online (Sandbox Code Playgroud)

I'd like aliases: g for generate, r for routed-module, s for stateful-ui.

Autocompletion would be nice.

Here's what I've tried, don't know where to go from here:

  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,
        },
      },
    })
Run Code Online (Sandbox Code Playgroud)

Thanx!

(Doing this with typescript is not necessary. I primarily want to understand how to use the library's api.)

Bir*_*sky 3

使用这个重要的文档来解决这个问题:

  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))
Run Code Online (Sandbox Code Playgroud)