make-cli: Declarative CLI Framework

6.0.2 · active · verified Wed Apr 22

make-cli is a declarative framework for building command-line interfaces in Node.js, distinguished by its streamlined approach that centers around a single configuration object and a single function call. It aims to simplify CLI development by abstracting away much of the boilerplate often associated with argument parsing and command definition. The current stable version is 6.0.2, with major versions typically aligned with Node.js LTS releases (e.g., v5 required Node.js >= 20, v6 requires Node.js >= 22). Patch and minor releases occur regularly to address bugs and introduce minor enhancements. Built on top of Commander.js, make-cli leverages its robust parsing capabilities while offering a more functional and declarative API. This design makes it particularly appealing for developers who prefer a concise and config-driven style for their CLI tools, differentiating it from more imperative or class-based alternatives by providing a highly approachable entry point for creating complex command structures.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates defining a CLI with global arguments and options, an main action handler, and a subcommand. It includes TypeScript types, async operations, and option validation with 'choices'.

#!/usr/bin/env node

import makeCli from 'make-cli';
import { resolve } from 'path';

interface MyCliOptions {
  yes?: boolean;
  value: string;
  force?: boolean;
}

makeCli<MyCliOptions>({
  version: '0.1.0',
  name: 'my-cli',
  usage: 'A simple demonstration CLI for make-cli',
  arguments: '<remote> [extra]',
  options: [
    {
      name: '-y, --yes',
      description: 'Skip interactive prompts and confirm actions',
      default: false
    },
    {
      name: '--value <value>',
      description: 'Specifies an important configuration value',
      defaultValue: 'default_foo',
      choices: ['default_foo', 'bar', 'baz']
    }
  ],
  action: async (remote: string, extra: string | undefined, options: MyCliOptions) => {
    console.log(`Executing action for remote: ${remote || '[no remote]'}`);
    console.log(`Extra argument: ${extra || '[no extra]'}`);
    console.log(`Options: Yes=${options.yes}, Value=${options.value}`);
    
    if (options.yes) {
      console.log('Skipping interactive confirmation due to --yes flag.');
    } else {
      console.log('Performing interactive confirmation...');
    }
    console.log(`Resolved path example: ${resolve('.')}`);
    await new Promise(resolve => setTimeout(resolve, 100)); // Simulate async work
    console.log('Main action completed.');
  },
  commands: [
    {
      name: 'push',
      description: 'Pushes changes to the remote repository',
      arguments: '<remote>',
      options: [
        {
          name: '-f, --force',
          description: 'Force push',
          default: false
        }
      ],
      handler: async (remote: string, { force }: { force: boolean }) => {
        console.log(`Executing 'push' command to ${remote} with force=${force}`);
        await new Promise(resolve => setTimeout(resolve, 50));
        console.log('Push command executed.');
      }
    }
  ]
});

view raw JSON →