Socket
Socket
Sign inDemoInstall

@jill64/ts-cli

Package Overview
Dependencies
1
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @jill64/ts-cli

> Solidly-Typed CLI Router


Version published
Maintainers
1
Created

Readme

Source

@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

Keywords

FAQs

Last updated on 18 Jun 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc