New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@drizzle-team/brocli

Package Overview
Dependencies
Maintainers
4
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@drizzle-team/brocli

Typed CLI command runner

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
718K
decreased by-6.58%
Maintainers
4
Weekly downloads
 
Created
Source

BroCLI

Run CLI commands with fully typed handlers

Code

Defining options

defineOptions()

Start defining options using defineOptions() function:

import { defineOptions, string, boolean } from 'brocli'

const commandOptions = defineOptions({
    opt1: string(),
    opt2: boolean('flag').alias('f'),
    // And so on... 
})

Keys of the object passed to defineOptions() determine to which keys parsed options will be assigned to inside your handler.

Option builder

Initial builder functions:

  • string(name?: string) - defines option as a string-type option which requires data to be passed as --option=value

    • name - name by which option is passed in cli args
      If not specified, defaults to key of this option in defineOptions
      :warning: - must not contain = character and be unique per each defineOptions()
      :speech_balloon: - will be automatically prefixed with - if one character long, -- if longer
      If you wish to have only single hyphen as a prefix on multi character name - simply specify name with it: string('-longname')
  • boolean(name?: string) - defines option as a boolean-type option which requires data to be passed as --option

    • name - name by which option is passed in cli args
      If not specified, defaults to key of this option in defineOptions
      :warning: - must not contain = character and be unique per each defineOptions()
      :speech_balloon: - will be automatically prefixed with - if one character long, -- if longer
      If you wish to have only single hyphen as a prefix on multi character name - simply specify name with it: boolean('-longname')

Extensions:

  • .alias(...aliases: string[]) - defines aliases for option

    • aliases - aliases by which option is passed in cli args
      :warning: - must not contain = character and be unique per each defineOptions()
      :speech_balloon: - will be automatically prefixed with - if one character long, -- if longer
      If you wish to have only single hyphen as a prefix on multi character alias - simply specify alias with it: .alias('-longname')
  • .desc(description: string) - defines description for option to be displayed in help command

  • .required() - sets option as required, which means that application will print an error if it is not present in cli args

  • .default(value: string | boolean) - sets default value for option which will be assigned to it in case it is not present in cli args

  • .hidden() - sets option as hidden - option will be omitted from being displayed in help command

Creating handlers

Normally, you can write handlers right in the defineCommand() function, however there might be cases where you'd want to define your handlers separately.
For such cases, you'd want to infer type of options that will be passes inside your handler.
You can do it using TypeOf type:

import { defineOptions, string, boolean, type TypeOf } from 'brocli'

const commandOptions = defineOptions({
    opt1: string(),
    opt2: boolean('flag').alias('f'),
    // And so on... 
})

export const commandHandler = (options: TypeOf<typeof commandOptions>) => {
    // Your logic goes here...
}

Defining commands

To define commands, use defineCommand() function:

import { defineOptions, defineCommand, string, boolean, type TypeOf } from 'brocli'

const commandOptions = defineOptions({
    opt1: string(),
    opt2: boolean('flag').alias('f'),
    // And so on... 
})

const commandHandler = (options: TypeOf<typeof commandOptions>) => {
    // Your logic goes here...
}

defineCommand({
    name: 'command', 
    aliases: ['c', 'cmd'],
    description: 'Description goes here',
    hidden: false,
    options: commandOptions,
    handler: commandHandler,
});

Parameters:

  • name - name by which command is searched in cli args
    :warning: - must not start with - character and be unique per application

  • aliases - aliases by which command is searched in cli args
    :warning: - must not start with - character and be unique per application

  • description - description for command to be displayed in help command

  • hidden - sets command as hidden - if true, command will be omitted from being displayed in help command

  • options - command options created using defineOptions() function

  • handler - function, which will be executed in case of successful option parse

:speech_balloon: - BroCLI starts with having help command predefined, and despite the requirement for command names to be unique, help can actually be redefined so that your app could have it matching your output style.

Running commands

After defining commands, you're going to need to execute runCli() function to start command execution

import { runCli } from 'brocli'

await runCli()

:speech_balloon: - in case cli arguments are not stored in process.argv in your environment, you can pass custom argument source as an argument to runCli(), however note that first two elements of such source will be ignored as they are expected to store executable and executed file paths instead of args.

CLI

In BroCLI, command doesn't have to be the first argument, instead it may be contained in any order.
To make this possible, hovewer, string-type options are limited to --option=value syntax.
Options are parsed in strict mode, meaning that having any unrecognized options will result in anerror.

FAQs

Package last updated on 14 Jun 2024

Did you know?

Socket

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc