Page cover

init

Initialize your CLI after defining its commands.

This function must be called after all commands have been configured. It will then search for and load your application's configuration file and initialize Yargs which will parse process.argv. The parsed arguments and configuration are then passed to the appropriate command's handler.

This function sets the following global parameters:

This function accepts an optional callback, SaffronInitCallbackarrow-up-right that will be passed the global yargs object which can be used to perform any additional global configuration of Yargs.

Handling Yargs Output Manually

This callback may optionally return a yargs.ParseCallbackarrow-up-right that can can be used to intercept output from Yargs, manipulate it, and write it to an output stream explicitly.

Example:

cli.js
import * as cli from '@darkobits/saffron';

// Configure commands for the application.
cli.command({
  // ...
});

cli.command({
  // ...
});

cli.init(yargs => {
  yargs.wrap(120);

  // Optionally return a ParseCallback.
  return (err, argv, output) => {
    if (err) {
      process.stderr.write(err.message);
      process.exit(err.code ?? 1);
    }

    if (output) {
      process.stdout.write(output);
      process.exit(0);
    }
  };
});

Last updated