Notwithstanding Previous Misdemeanors
    Have ideas to improve npm?Join in the discussion! »

    make-cli

    2.0.5 • 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

    Build command line tools declaratively with a configuration object and a single function call. Based on Commander.js.

    Install

    # npm
    $ npm install make-cli
    
    # Yarn
    $ yarn add make-cli

    Usage

    First the CLI has to be implemented:

    #!/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',
            },
          ],
          action: (remote, options) => { /* push the stuff */ },
        },
        {
          name: 'pull',
          // ...
        },
      ],
    })

    Then it can be called like so:

    $ my-cli push origin --yes
    $ my-cli pull origin
    $ my-cli --help
    $ my-cli --version

    You can also allow to pass unknown options and those 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 my-cli --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

    1,573

    Version

    2.0.5

    License

    MIT

    Unpacked Size

    9.72 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar