@jill64/ts-cli

1.0.2 • Public • Published

@jill64/ts-cli

npm-version npm-license npm-download-month npm-min-size ci.yml

> Solidly-Typed CLI Router

Installation

npm i @jill64/ts-cli

Example

import { App } from '@jill64/ts-cli'

new App(
  {
    args: [
      ['arg1', 'Argument 1'],
      ['arg2', 'Argument 2'],
      ['arg3', 'Argument 3']
    ],
    options: {
      verbose: {
        alias: 'V',
        description: 'Verbose output'
      },
      host: {
        alias: 'h',
        description: 'Host name',
        type: 'string'
      }
    },
    optional: [
      ['optional-1', 'Optional Argument 1'],
      ['optional-2', 'Optional Argument 2'],
      ['optional-3', 'Optional Argument 3']
    ],
    rest: {
      placeholder: 'rest-argument',
      description: 'Rest Argument'
    },
    codes: {
      0: success,
      1: failure
    }
  },
  ({ args, options, optional, rest }) => {
    // `<command> <arg1> <arg2> <arg3> [options] [optional1?] [optional2?] [optional3?] <...rest>`

    options.verbose // boolean
    options.host // string

    // Allow 0 or 1 or void
    return 0
  }
)

Add Route (Subcommands)

The add function defines a route.

import { App } from '@jill64/ts-cli'

new App(/* ... */)
  .add(
    'test',
    {
      // Config
    },
    () => {
      // `<command> test`
      // ...
    }
  )
  .add(
    'test start',
    {
      // Config
    },
    () => {
      // `<command> test start`
      // ...
    }
  )

Run as Command

The run function executes the command immediately using process.argv.

import { App } from '@jill64/ts-cli'
import process from 'node:process'

new App(/* ... */).run(process.argv)

Export as API

// index.js
import { App } from '@jill64/ts-cli'

export const command = new App(/* ... */).add(/* ... */).add(/* ... */)
import { command } from 'index.js'

// `example`
command.execute({
  args: {
    arg1: 'value1',
    arg2: 'value2',
    arg3: 'value3'
  },
  optional: {
    'optional-1': 'optional-value1',
    'optional-2': 'optional-value2',
    'optional-3': 'optional-value3'
  },
  // or
  // rest: ['rest1', 'rest2', 'rest3'],
  options: {
    verbose: true,
    host: 'example.com'
  }
})

// `example test`
command.invoke.test({
  // ...
})

// `example test start`
command.invoke['test start']({
  // ...
})

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @jill64/ts-cli

Weekly Downloads

2,183

Version

1.0.2

License

MIT

Unpacked Size

17.5 kB

Total Files

47

Last publish

Collaborators

  • jill64