Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wcauchois/program-builder

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wcauchois/program-builder

[![npm version](https://badge.fury.io/js/%40wcauchois%2Fprogram-builder.svg)](https://badge.fury.io/js/%40wcauchois%2Fprogram-builder) [![CircleCI Build](https://circleci.com/gh/wcauchois/program-builder.svg?style=svg)](https://circleci.com/gh/wcauchois/p

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

program-builder

npm version CircleCI Build Netlify Status

This is a TypeScript-first library for building type safe command-line interfaces.

You define your arguments and keyword arguments using a fluent builder, which gives you a Program object. You can then define a main function in terms of the strongly typed arguments of that Program, and finally execute the main function against your program which will parse and provide commandline arguments.

Documentation Website | Examples | API Docs

Installation

npm install @wcauchois/program-builder

Features

  • Positional arguments (required and optional).
  • Boolean flags (both "positive", like --unroll-loops; and "inverted", like --no-unroll-loops).
  • Keyword flags like --path foo.txt. These can be strings, integers, or floats - or you can provide a custom conversion function.
  • Validations like ensuring that all required arguments are specified (aka: types should not lie).
  • Automatic generation of help text and handling of a help argument (-h, --help).
  • Executes Promise-returning main functions and correctly exits the process.
  • Subcommands.

Example

const program = ProgramBuilder.newBuilder()
  .arg('filename', { description: `A file name` })
  .optionalArg('extraFilename', { description: `An additional optional file name`})
  .intFlag('--optionalCount,-c', { dest: 'optionalCount', default: null, description: `A count` })
  .intFlag('--requiredCount', { dest: 'requiredCount', description: `A count that is required` })
  .build();

function main(args: Arguments<typeof program>) {
  console.log(`filename is: ${args.filename}`); // args.filename: string
  console.log(`extraFilename is: ${args.extraFilename}`); // args.extraFilename: string | null
  console.log(`optionalCount is: ${args.count}`); // args.optionalCount: number | null
  console.log(`requiredCount is: ${args.requiredCount}`); // args.requiredCount: number
}

program.exec(main);

An example invocation of this program would be:

$ ts-node main.ts file1.txt file2.txt -c 1 --requiredCount 2

You can also view autogenerated help text by executing the program with an -h parameter.

$ ts-node main.ts -h
Usage: main.ts [options] <filename> [extraFilename]

Options:
  --count, -c [count]              A count
  --requiredCount [requiredCount]  A count that is required

Beta Software

As of January 2020, Program Builder is a very young library! That said, I think it occupies a valuable niche offering better type safety than yargs or commander with less verbosity than ts-command-line.

The argument parsing library that gives me the most joy is Python's argparse and I'm striving to create something similarly lightweight but powerful for TypeScript.

At this stage your feedback is extremely valuable, and if you have anything to say, please file an issue!

Keywords

FAQs

Package last updated on 17 Feb 2020

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