npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsflags

v1.3.0

Published

An extremely simple yet flexible JavaScript library for parsing command-line flags, inspired by the golang "flags" library.

Downloads

4

Readme

jsflags

An extremely simple yet flexible JavaScript library for parsing command-line flags, inspired by the golang "flags" library.

Features

  • No dependencies.
  • Easy and flexible validation API.
  • TypeScript and JavaScript support.
  • CJS and ESM support.

Basic Usage

import FlagSet, { integer, boolean, defaultValue } from "jsflags"
import { parseNodejs } from "jsflags/node"

const flags = new FlagSet()
const portRef = flags.flag(defaultValue(integer, 3000), "port", "Specify what port to host on.")
const verboseRef = flags.flag(boolean, "verbose", "Enable verbose logging.")

parseNodejs(flags)
console.log(portRef.value, verboseRef.value)

Full Usage

import FlagSet, { integer, string, boolean, defaultValue, single } from "jsflags"
import { parseNodejs } from "jsflags/node"

const flags = new FlagSet((values) => {
  return single(values)
})

const portRef = flags.flag(integer, "port", "Specify the port to host on.")
const nameRef = flags.flag(string, "name", "Specify the name of the application.")
const verboseRef = flags.flag(boolean, "verbose", "Enable verbose logging.")
const multipleRef = flags.flag(multiple)

const positional = parseNodejs(flags)
console.log(portRef.value, nameRef.value, verboseRef.value, positional)
// $ application --port 200 --name "some fancy name" positional
// 200 "some fancy name" false "positional"
// 
// $ application --port 4200 --name "some fancy name" --verbose arg
// 4200 "some fancy name" true "arg"

console.log(flags.help())
// Usage:
//   	Positionals:	single
//
//   	-port	integer	Specify the port to host on.
//
//   	-name	string	Specify the name of the application.
//
//   	-verbose	boolean (default: false)	Enable verbose logging.
  • Each flag returns a "reference", an object { value: ... } whose property value is set to the value of the flag.
  • A reference's "value" is set to null before parseFlags(...) is called.
  • Flag names must only contain letters, numbers, _ and -. (Regex: [\w-])
  • Quoted flag values must always use double quotes.

The following formats are accepted for arguments:

| Args | Result | | --- | --- | | -flag | [""] | | -flag value | ["value"] | | -flag=value | ["value"] | | -flag=v1 -flag v2 -flag | ["v1", "v2", ""] | | --flag | [""] | | --flag value | ["value"] | | --flag=value | ["value"] | | --flag=v1 --flag v2 --flag | ["v1", "v2", ""] |

Custom validation and array values

All flags possess a "validator" to verify and transform the raw string values of the flag into the final value in the reference.

// T is the type in the reference.
export type Validator<T> = (values: string[]) => T;

A validator that does nothing (like flag((values) => values, ...)) allows for multiple (or zero) values for a given flag. This is often undesired, so the single validator is used by default on most of the built-in validators.

The single validator throws an error if there is not exactly one value passed in. (Do note that the string validator is just an alias for single as the two do the exact same thing.)

This means you can pass in your own validators to validate a custom type.

function json(values: string[]) {
  return JSON.parse(single(values))
}
const jsonRef = flag(json, "json", "...")
parseFlags(process.argv.slice(2)) // $ application --json {"json": [1, "oh no", 3]}
console.log(jsonRef.value) // {json: [1, "oh no", 3]}

The type of a value in the help message is simply the name of the validator function. In the example above, it would show up like -json json ....

If you want a list of typed values. multiple(validator) returns a validator that maps a given validator over each element in the values passed in, it then returns an array of the values returned.

const portRef = flag(multiple(integer), "port", "")
parseFlags(process.argv.slice(2)) // $ application --port 323 --port=424
console.log(portRef.value) // [323, 424]