Search code examples
javascripttypescriptyargs

Epilogue for `yargs` Commands


I'm writing a command-line application in TypeScript using yargs. I want to add an epilogue to the auto-generated help. That works fine for the overall usage information but not for (sub)commands. This is the relevant my code:

import yargs from 'yargs';

yargs(process.argv.slice(2))
    .command({
        command: 'whatever',
        describe: 'Do something',
        builder: (argv: yargs.Argv) => argv.options({}),
    })
    .help()
    .epilogue('Report bugs at ...')
    .parse();

If I just run the script with option --help but without a command, everything works fine:

$ node --import=tsx src/index.ts --help
index.ts [command]

Commands:
  zzzso.ts whatever  Do something

Options:
  --version  Show version number                                       [boolean]
  --help     Show help                                                 [boolean]

Report bugs at ...

But if I specify a command, there is no epilogue:

$ node --import=tsx src/index.ts whatever --help
index.ts whatever

Do something

Options:
  --version  Show version number                                       [boolean]
  --help     Show help                                                 [boolean]

Any idea how I can add the line "Report bugs at ..." also to the help output for the commands?


Solution

  • There is not support for a global epilog displayed for subcommands too.

    You can specify the epilog for each subcommand yourself.

    import yargsFactory from 'yargs/yargs';
    
    const myEpilog = 'Report bugs at ...';
    
    const yargs = yargsFactory(process.argv.slice(2))
       .epilogue(myEpilog)
       .help()
       .command({
          command: 'whatever',
          describe: 'Do something',
          builder: (y) => y.epilog(myEpilog),
       });
    
    % node index.mjs whatever --help
    index.mjs whatever
    
    Do something
    
    Options:
      --version  Show version number                                       [boolean]
      --help     Show help                                                 [boolean]
    
    Report bugs at ...