Shell CLI Argument Parser and Router

0.12.0 · active · verified Tue Apr 21

Shell.js is a declarative command-line argument parser and stringifier for Node.js, currently at version 0.12.0, with its latest update published approximately a year ago. It's designed to simplify the creation of complex CLI applications by providing a structured way to define options, commands, and their associated routing logic. Key features include automatic argument discovery, support for multi-level commands (e.g., `git pull origin main`), built-in type conversion for arguments (string, boolean, integer, array), and auto-generated help messages. It differentiates itself through a declarative API that integrates parsing, stringifying, and routing into a single configuration object, making CLI development more intuitive than some imperative alternatives. The package is actively maintained and currently requires Node.js version 20 or later.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart defines a CLI with global options, subcommands, and subcommand-specific options, demonstrating the declarative API for parsing and routing with `shell.js`.

import { shell } from 'shell';

const myCli = shell({
  name: 'complex-app',
  description: 'A robust command-line application demonstrating parsing and routing.',
  options: {
    verbose: { shortcut: 'v', description: 'Enable verbose logging', type: 'boolean' },
    config: { shortcut: 'c', description: 'Path to configuration file', type: 'string', default: './config.json' }
  },
  commands: {
    start: {
      description: 'Start a service or process.',
      options: {
        port: { shortcut: 'p', description: 'Port to listen on', type: 'integer', default: 3000 }
      },
      main: async ({ options }) => {
        console.log(`Starting service on port ${options.port} with config: ${options.config} (verbose: ${options.verbose})`);
        // Simulate async operation
        await new Promise(resolve => setTimeout(resolve, 1000));
        console.log('Service started successfully.');
      }
    },
    stop: {
      description: 'Stop a running service.',
      main: () => {
        console.log('Stopping service...');
        console.log('Service stopped.');
      }
    }
  }
});

myCli.route().catch(err => {
  console.error('CLI Error:', err.message);
  process.exit(1);
});

// To run this: save as `cli.js`, add `"type": "module"` to package.json.
// Then run: `node cli.js start -p 8080 --verbose` or `node cli.js stop -c my-dev-config.json`

view raw JSON →