sywac CLI Parser

1.3.0 · active · verified Wed Apr 22

sywac (So You Want A CLI) is a Node.js library for parsing command-line arguments and building robust CLIs. It provides a fluid, asynchronous API for defining positional arguments, options, and complex nested commands, with features like type-based argument parsing and flexible auto-generated help content. The current stable version is 1.3.0. While its release cadence has been moderate, it remains an active project, most notably adding a `.strict()` mode in v1.3.0 for enhanced security against unknown flags. This differentiator helps prevent unexpected behavior from typos in sensitive operations. sywac is designed for Node.js environments (v4+) and focuses on a coherent API for both simple and complex CLI applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining commands, options, enabling strict mode, and parsing arguments using the `parseAndExit` method, showing how unknown flags are handled.

const sywac = require('sywac');

const cli = sywac
  .positional('<command>', { desc: 'The command to execute' })
  .command('add <item>', { 
    desc: 'Add a new item', 
    run: (argv) => console.log(`Adding item: ${argv.item}`)
  })
  .command('remove <item>', { 
    desc: 'Remove an item', 
    run: (argv) => console.log(`Removing item: ${argv.item}`)
  })
  .option('-f, --force', { 
    type: 'boolean', 
    desc: 'Force the operation' 
  })
  .strict()
  .help()
  .version();

async function main() {
  // Example usage: node cli.js add my-item --force
  // Example usage: node cli.js remove unknown-item --typo
  try {
    const argv = await cli.parseAndExit();
    console.log('Parsed arguments:', JSON.stringify(argv, null, 2));
  } catch (error) {
    console.error('CLI Error:', error.message);
    // sywac.parseAndExit() typically handles exiting, but in a test environment
    // or when catching errors manually, you might want to exit.
    process.exit(1);
  }
}

if (require.main === module) {
  main();
}

view raw JSON →