1.7.1 • Published 3 years ago

termkit v1.7.1

Weekly downloads
6
License
ISC
Repository
-
Last release
3 years ago

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

1.7.1

3 years ago

1.7.0

3 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.9

4 years ago

1.4.8

4 years ago

1.4.7

4 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago