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

@lukecjohnson/args

v1.0.1

Published

A quick, lightweight command-line argument parser with type checking, shorthand flags, default values, and help text generation

Downloads

1

Readme

@lukecjohnson/args

A quick, lightweight command-line argument parser with type checking, shorthand flags, default values, and help text generation

Installation

npm install @lukecjohnson/args

Note: @lukecjohnson/args is an ESM-only package

Usage

Options

parse accepts an options object that can be used to define flags and customize its behavior. The following options are available:

  • flags: An object defining the flags the program or command accepts. Each entry's key is a flag name that can be used with a double hyphen on the command line (--example) and its value is an object further describing the flag with the following properties:
    • type: A string indicating the expected type of the flag's value ("string", "number", or "boolean")
    • default: The default value assigned to the flag if no value is provided by the end-user
    • shorthand: A single-letter alias that can be used with a single hyphen on the command line
    • description: A short description of the flag to be included in the generated help text
  • argv: An array of raw arguments to be parsed (Default: process.argv.slice(2))
  • usage: The general usage pattern of the program or command to be included in the generated help text
  • disableHelp: When true, the built-in --help and -h flags are disabled (Default: false)
  • stopAtPositional: When true, all arguments after the first positional, non-flag argument are pushed to result.args (Default: false)

Result

parse returns an object containing args and flags:

  • args: An array of non-flag arguments provided by the end-user
  • flags: An object containing the values of flags provided by the end-user or their default values

Example

import parse from '@lukecjohnson/args';

const { args, flags } = parse({
  flags: {
    host: {
      type: 'string',
      shorthand: 'H',
      description: 'Hostname to bind',
      default: 'localhost',
    },
    port: {
      type: 'number',
      shorthand: 'p',
      description: 'Port to bind',
      default: 3000,
    },
    debug: {
      type: 'boolean',
      shorthand: 'd',
      description: 'Show debugging information',
      default: false,
    }
  },
  usage: 'node serve.js [directory] [flags]'
});

console.log({ args, flags });
$ node serve.js public --host 0.0.0.0 --port=8080 -d

{
  args: ['public'],
  flags: {
    host: '0.0.0.0',
    port: 8080,
    debug: true
  }
}
$ node serve.js --help

Usage:
  node serve.js [directory] [flags]

Flags:
  -H, --host            Hostname to bind (Default: "localhost")
  -p, --port            Port to bind (Default: 3000)
  -d, --debug           Show debugging information (Default: false)

Benchmarks

@lukecjohnson/args    2,227,270 ops/sec ±0.70% (94 runs sampled)
arg                   1,067,095 ops/sec ±1.24% (92 runs sampled)
mri                     520,460 ops/sec ±0.39% (95 runs sampled)
minimist                216,378 ops/sec ±0.39% (97 runs sampled)
command-line-args        61,050 ops/sec ±0.85% (93 runs sampled)
yargs-parser             24,684 ops/sec ±2.47% (93 runs sampled)

See /benchmark for benchmark details