Never Push Mistakes

    make-cli

    3.0.0 • Public • Published

    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 require make-cli. Then configure your command line tool like so:

    // cli.js
    
    #!/usr/bin/env node
    
    const makeCli = require('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.
      },
    })

    It is also possible to define sub-commands:

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

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

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

    $ ./cli.js push origin --yes
    $ ./cli.js pull origin
    $ ./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"
    }

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

    #!/usr/bin/env node
    
    const makeCli = require('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

    Install

    npm i make-cli

    DownloadsWeekly Downloads

    499

    Version

    3.0.0

    License

    MIT

    Unpacked Size

    10.4 kB

    Total Files

    4

    Last publish

    Collaborators

    • dword-design