4.0.0 • Published 2 months ago

make-cli v4.0.0

Weekly downloads
680
License
MIT
Repository
github
Last release
2 months ago

make-cli

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:

Thanks a lot for your support! ❤️

License

MIT License © Sebastian Landwehr

4.0.0

2 months ago

3.0.5

9 months ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.15

2 years ago

2.0.13

2 years ago

2.0.14

2 years ago

2.0.12

3 years ago

2.0.9

3 years ago

2.0.11

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

1.2.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.29

3 years ago

1.0.30

3 years ago

1.0.28

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.27

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago