termkit

Terminal command constructor and parser kit

Usage no npm install needed!

<script type="module">
  import termkit from 'https://cdn.skypack.dev/termkit';
</script>

README

Termkit

A terminal input parsing kit

Getting Started

This is how to get started using Termkit

Installing

Add this package to your project

npm install --save termkit

Using

Configure your command line program

const { command, middleware, option } = require('termkit')

const program = command('example')
  .version('1.0.0')
  .description('example program')
  .option('a', 'array', '[arr...]', 'Array variable')
  .option('r', 'required', '<reqA> <reqB>', 'Two required variables')
  .option('o', 'optional', '[opt]', 'One optional variable')
  .option('b', 'boolean', null, 'No variable')
  .middleware((options) => console.log('middleware is run before action, manipulate the options object as needed'))
  .action((options) => console.log('run action with given options'))
  .commands([
    command('first')
    .description('first nested command')
    .options([
      // Same style options as before
    ])
    .middleware((options) => console.log('middleware can be nested too'))
    .action((options) => console.log('run action with given options'))
    .commands([
      // So on and so forth
    ])
  ])

Alternatively supply options and middlewares to command in arrays

command('example')
  .options([
    option('a', 'array', '[arr...]', 'Array variable'),
    option('r', 'required', '<reqA> <reqB>', 'Two required variables'),
    option('o', 'optional', '[opt]', 'One optional variable'),
    option('b', 'boolean', null, 'No variable')
  ])
  .middlewares([
    middleware(() => console.log(1)),
    middleware(() => console.log(2)),
    middleware(() => console.log(3))
  ])

Commands nest and can have variables themselves

command('example').commands([
  command('another', '[optional]'),
  command('another', '<required>'),
  command('another', '[array...]')
])

Variables, and options are passed into middleware and action functions.

command('example', <var>)
  .option('r', 'require', <req>, 'Another example')
  .middleware(options) => {
    console.log(options.require)
  })
  .action((options) => {
    console.log(options.require)
  })

After completing constructing the CLI flow, parse the input and catch possible errors

try {
  program.parse(process.argv)
} catch(err) {
  console.log(err)
}

Built in terminal help usage function

  program.parse('_ _ help'.split(' '))

Stay tuned for more

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details