Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

make-cli

dword-design1.2kMIT4.0.0

Super easy declarative CLI framework with a single configuration object and a single function call.

argument-parser, arguments, cli, command-line, command-line-interface, commander, commanderjs, declarative, declarative-programming, fp, functional, interface, node, nodejs, npm, npm-package, option, options

readme

make-cli

npm version Linux macOS Windows compatible Build status Coverage status Dependency status Renovate enabled
Open in Gitpod Buy Me a Coffee PayPal Patreon

Super easy declarative CLI framework with a single configuration object and a single function call.

There are so many command line interface libraries around that it's hard to find the right one for your needs. But there aren't many that expose a single function with a single config object like most other Node.js packages do. That's why there is make-cli! Call a single function, pass a single config object and you're good to go.

Based on Commander.js and supports most of its features. In case you're missing something, feel free to open up an issue.

Install

# npm
$ npm install make-cli

# Yarn
$ yarn add make-cli

Usage

Create a .js file with Shebang and import make-cli. Then configure your command line tool like so:

// cli.js

#!/usr/bin/env node

import makeCli from 'make-cli'

makeCli({
  version: '0.1.0',
  name: 'my-cli',
  usage: 'Usage description here',
  arguments: '<remote> [extra]',
  options: [
    {
      name: '-y, --yes',
      description: 'Skip questions',
    },
    {
      name: '--value <value>',
      description: 'Specifies the value',
      defaultValue: 'foo',
      choices: ['foo', 'bar'],
    },
  ],
  action: (remote, extra, options) => {
    // options.value and options.yes
    // contain the options.
  },
})

Give it execution rights via chmod +x cli.js.

Then you can call it via the shell of your choice:

$ ./cli.js --yes
$ ./cli.js foo
$ ./cli.js --help
$ ./cli.js --version

When publishing your command line tool via NPM, you'll probably want to add the file to the bin property, so it's installed to node_modules/.bin.

{
  "name": "my-cli",
  "bin": "./cli.js"
}

Subcommands

It is possible to define subcommands like so:

makeCli({
  commands: [
    {
      name: 'push',
      description: 'Pushes to the repo',
      arguments: '<remote>',
      options: [
        {
          name: '-y, --yes',
        },
      ],
      handler: (remote, options) => { /* push the stuff */ },
    },
    {
      name: 'pull',
      // ...
    },
  ],
})

Then you can call it:

$ ./cli.js push origin

Declaring options and commands as objects

Instead of an array you can declare options and commands as objects, which is sometimes more convenient:

makeCli({
  options: [
    '-y, --yes': { description: 'Skip questions' },
    '--value <value>': {
      description: 'Specifies the value',
      defaultValue: 'foo',
      choices: ['foo', 'bar'],
    },
  ],
  commands: {
    push: {
      description: 'Pushes to the repo',
      arguments: '<remote>',
      options: [
        {
          name: '-y, --yes',
        },
      ],
      handler: (remote, options) => { /* ... */ },
    },
    pull: () => { /* ... */ },
  }
})

Unknown options

You can also allow to pass unknown options, which are then available in the action like so:

#!/usr/bin/env node

import makeCli from 'make-cli'

makeCli({
  // ...
  allowUnknownOption: true,
  options: [
    {
      name: '-y, --yes',
      description: 'Skip questions',
    },
  ],
  action: (options, command) => {
    // options.yes = true
    // command.args = ['--foo']
  },
})

If you now run $ ./cli.js --yes --foo, command.args will contain ['--foo'].

Contribute

Are you missing something or want to contribute? Feel free to file an issue or a pull request! ⚙️

Support

Hey, I am Sebastian Landwehr, a freelance web developer, and I love developing web apps and open source packages. If you want to support me so that I can keep packages up to date and build more helpful tools, you can donate here:

Buy Me a Coffee  If you want to send me a one time donation. The coffee is pretty good 😊.
PayPal  Also for one time donations if you like PayPal.
Patreon  Here you can support me regularly, which is great so I can steadily work on projects.

Thanks a lot for your support! ❤️

License

MIT License © Sebastian Landwehr

changelog

4.0.0 (2024-03-05)

Bug Fixes

BREAKING CHANGES

  • node.js >= 18

3.0.5 (2023-07-27)

Bug Fixes

  • update dependency execa to v7.2.0 (4b5f803)

3.0.4 (2023-04-09)

Bug Fixes

  • update dependency @dword-design/functions to v4.1.7 (195ffc0)

3.0.3 (2023-04-08)

Bug Fixes

3.0.2 (2023-03-15)

Bug Fixes

  • update dependency execa to v7.1.1 (24416bd)

3.0.1 (2023-02-14)

Bug Fixes

3.0.0 (2023-01-08)

Bug Fixes

BREAKING CHANGES

  • drop node 12 support, move to esm

2.0.15 (2022-01-12)

Bug Fixes

2.0.14 (2022-01-04)

Bug Fixes

2.0.13 (2022-01-01)

Bug Fixes

  • support async error handling (909e248)

2.0.12 (2021-07-20)

Bug Fixes

2.0.11 (2021-07-19)

Bug Fixes

2.0.10 (2021-07-18)

Bug Fixes

2.0.9 (2021-07-18)

Bug Fixes

  • update config files (ab7a416)
  • write GitHub metadata to package.json [description] (ac93d00)

2.0.8 (2021-07-15)

Bug Fixes

2.0.7 (2021-07-05)

Bug Fixes

2.0.6 (2021-06-28)

Bug Fixes

  • update dependency commander to v8 (#65) (a33d237)

2.0.5 (2021-06-14)

Bug Fixes

2.0.4 (2021-05-28)

Bug Fixes

2.0.3 (2021-05-04)

Bug Fixes

  • update dependency fs-extra to v10 (#58) (f3ccbdb)

2.0.2 (2021-05-04)

Bug Fixes

  • update dependency with-local-tmp-dir to v4 (#59) (766723b)

2.0.1 (2021-05-03)

Bug Fixes

  • update dependency @dword-design/functions to v4 (#57) (acb778b)

2.0.0 (2021-05-03)

Bug Fixes

  • update dependency @dword-design/base to v8 (#56) (43ec8a1)

BREAKING CHANGES

  • require Node.js >= 12

Co-authored-by: Renovate Bot bot@renovateapp.com Co-authored-by: Sebastian Landwehr info@dword-design.de Co-authored-by: GitHub Actions actions@github.com

1.2.4 (2021-04-26)

Bug Fixes

  • update dependency with-local-tmp-dir to v3 (#53) (c033e74)

1.2.3 (2021-04-20)

Bug Fixes

1.2.2 (2021-04-12)

Bug Fixes

1.2.1 (2021-03-31)

Bug Fixes

1.2.0 (2021-03-29)

Features

1.1.0 (2021-03-29)

Features

1.0.30 (2021-03-23)

Bug Fixes

  • update dependency @dword-design/functions to v3 (#41) (813310c)

1.0.29 (2021-03-22)

Bug Fixes

1.0.28 (2021-03-11)

Bug Fixes

1.0.27 (2021-02-17)

Bug Fixes

  • deps: update dependency commander to v7 (#34) (46e962b)

1.0.26 (2021-02-16)

Bug Fixes

1.0.25 (2021-02-16)

Bug Fixes

1.0.24 (2020-12-10)

Bug Fixes

  • deps: update dependency execa to v5 (4b3b48a)

1.0.23 (2020-12-02)

Bug Fixes

  • config: Update changed files (64444ad)

1.0.22 (2020-08-23)

Bug Fixes

  • upgrades (d96deec)
  • deps: update dependency commander to v6 (0a4ecd7)

1.0.21 (2020-07-05)

Bug Fixes

  • fix command handling for commander 5 (9502765)
  • deps: update dependency commander to v5 (d48e332)

1.0.20 (2020-07-04)

Bug Fixes

1.0.19 (2020-05-29)

Bug Fixes

1.0.18 (2020-03-19)

Bug Fixes

  • deps: update dependency fs-extra to v9 (a7c010b)

1.0.17 (2020-02-18)

Bug Fixes