Super easy declarative CLI framework with a single configuration object and a single function call.
npm install make-cli
src="https://img.shields.io/npm/v/make-cli.svg"
alt="npm version"
>
src="https://github.com/dword-design/make-cli/workflows/build/badge.svg"
alt="Build status"
>
src="https://codecov.io/gh/dword-design/make-cli/branch/master/graph/badge.svg"
alt="Coverage status"
>
src="https://gitpod.io/button/open-in-gitpod.svg"
alt="Open in Gitpod"
width="114"
>
src="https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg"
alt="Buy Me a Coffee"
width="114"
>
src="https://sebastianlandwehr.com/images/paypal.svg"
alt="PayPal"
width="163"
>
src="https://sebastianlandwehr.com/images/patreon.svg"
alt="Patreon"
width="163"
>
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.
``bashnpm
$ npm install make-cli
Usage
Create a
.js file with Shebang and import make-cli. Then configure your command line tool like so:`js
// cli.js#!/usr/bin/env node
import makeCli from 'make-cli'
makeCli({
version: '0.1.0',
name: 'my-cli',
usage: 'Usage description here',
arguments: ' [extra]',
options: [
{
name: '-y, --yes',
description: 'Skip questions',
},
{
name: '--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:
`bash
$ ./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.`json
{
"name": "my-cli",
"bin": "./cli.js"
}
`$3
It is possible to define subcommands like so:
`js
makeCli({
commands: [
{
name: 'push',
description: 'Pushes to the repo',
arguments: '',
options: [
{
name: '-y, --yes',
},
],
handler: (remote, options) => { / push the stuff / },
},
{
name: 'pull',
// ...
},
],
})
`Then you can call it:
`bash
$ ./cli.js push origin
`$3
Instead of an array you can declare options and commands as objects, which is sometimes more convenient:
`js
makeCli({
options: [
'-y, --yes': { description: 'Skip questions' },
'--value ': {
description: 'Specifies the value',
defaultValue: 'foo',
choices: ['foo', 'bar'],
},
],
commands: {
push: {
description: 'Pushes to the repo',
arguments: '',
options: [
{
name: '-y, --yes',
},
],
handler: (remote, options) => { / ... / },
},
pull: () => { / ... / },
}
})
`$3
You can also allow to pass unknown options, which are then available in the action like so:
`js
#!/usr/bin/env nodeimport 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']`.Are you missing something or want to contribute? Feel free to file an issue or a pull request! ⚙️
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:
src="https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg"
alt="Buy Me a Coffee"
width="114"
>
If you want to send me a one time donation. The coffee is pretty good 😊.
src="https://sebastianlandwehr.com/images/paypal.svg"
alt="PayPal"
width="163"
>
Also for one time donations if you like PayPal.
src="https://sebastianlandwehr.com/images/patreon.svg"
alt="Patreon"
width="163"
>
Here you can support me regularly, which is great so I can steadily work on projects.
Thanks a lot for your support! ❤️