Skip to content

Instantly share code, notes, and snippets.

@myas92
Last active December 4, 2021 05:29
Show Gist options
  • Save myas92/6f6951e660344ab0f9e6368064129849 to your computer and use it in GitHub Desktop.
Save myas92/6f6951e660344ab0f9e6368064129849 to your computer and use it in GitHub Desktop.

Multiple commands and nested commands in yargs

network publish add [options]
network publish delete [options]

network internet add[options]
exports = yargs.command({
  command: 'network',
  description: 'network command',
  builder: (yargs) => {
    console.log('----yargs----');
    yargs.command({
      command: 'publish',
      description: 'publish network DESC',
      builder: (yargs) => {
        console.log('----yargs----');
        yargs.command({
          command: 'add',
          description: 'Add network publish DESC',
          builder: {
            tag: {
              alias: 't',
              describe: 'VLAN Tag',
              demandOption: true,
              type: 'string',
              // choices: ['host', 'edge'],
            },
          },
          handler: (a) => {
            console.log('handleeeer add  publish network');
          },
        });
        yargs.command({
          command: 'delete',
          description: 'Delete network publish DESC',
          builder: () => {
            console.log('delete network publish builder');
          },
          handler: (a) => {
            console.log('handleeeer delete publish network');
          },
        });
      },
      handler: (a) => {
        console.log('handleeeer publish network');
      },
    });
    yargs.command({
      command: 'internet',
      description: 'internet network DESC',
      builder: () => {
        console.log('internet network builder');
      },
      handler: (a) => {
        console.log('handleeeer internet network');
      },
    });
  },
  handler: (args) => {
    console.log('network running');
  },
})
  .demand(1, 'must provide a valid command')
  .help('h')
  .alias('h', 'help');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment