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

opts

v2.0.2

Published

Command line argument parser with automatic help text generation.

Downloads

956,295

Readme

Table of Contents

  1. NAME
  2. SYNOPSIS 1. running: 2. produces: 3. running: 4. produces: 5. running: 6. produces:
  3. INSTALLATION
    1. Stand-alone version
    2. NPM version
  4. USAGE
    1. LOADING
    2. CONFIGURING
      1. options
      2. arguments
      3. help text generator
  5. AUTHOR / CHANGELOG / LICENSE

Find the full documentation, source code, and examples online at https://khtdr.com/opts.

Or download this README as a man-page.

curl -o opts.3 https://raw.githubusercontent.com/khtdr/opts/master/man.3
man ./opts.3

NAME

opts.js - a command line parser for options and arguments

SYNOPSIS

The following example uses a custom version function, and opts in to the automatic help text. No pun intended.

var opts = require('opts');

var options = [
  { short       : 'v'
  , long        : 'version'
  , description : 'Show version and exit'
  , callback    : function () { console.log('v1.0'); process.exit(1); }
  }
];

opts.parse(options, true);
console.log('Example 1');
process.exit(0);

See https://raw.githubusercontent.com/khtdr/opts/master/examples/example1.js

running:

$ node ./example1

produces:

Example 1

running:

$ node ./example1 --help

produces:

Usage: node ./example1 [options]
Show this help message
   --help
Show version and exit
   -v, --version

running:

node ./example1 -v

produces:

v1.0

INSTALLATION

You do not need to use NPM or any package manager. It is written in plain-old Javascript and can be downloaded and included in your Node.js project, as-is. All of the examples use this approach. .RE See https://github.com/khtdr/opts/tree/master/examples

Stand-alone version

cd /path/to/your/project
curl -o opts.js https://raw.githubusercontent.com/khtdr/opts/master/src/opts.js

NPM version

npm install opts

USAGE

LOADING

With classic syntax:

var opts = require('opts');
opts.parse(options, arguments, help);

With modern syntax:

import * as opts from 'opts';
opts.parse(options, arguments, help);

If you installed opts with NPM, the typescript definitions should automatically be available in your editor. Otherwise you can download the .d.ts file manually. .RE See https://raw.githubusercontent.com/khtdr/opts/master/src/opts.d.ts

CONFIGURING

opts.parse(options, arguments, help)

Options are flag-arguments. Arguments are everything else. Consider the following hypothetical command for starting a server that listens on http://0.0.0.0:4000

node ./my-app start --host 0.0.0.0 -p 4000

In this example, the options are --host 0.0.0.0 and -p 4000. The argument is start. The arguments can be after, before, or among the options.

options

options is an array of option objects. Each option in the array can have the following fields. None are required, but you should at least provide a short or long name.

let options = [
  { short       : 'l',
    long        : 'list',
    description : 'Show a list',
    value       : false, // default false
    required    : true, // default false
    callback    : function (value) { ... },
  }, // ... followed by more options
];

arguments

arguments require less configuration. This is an optional argument to opts.parse:

let arguments =
  { name     : 'script',
    required : true, // not required by default
    callback : function (value) { ... },
  };

help text generator

Finally, you can add an automatically generated help message by passing a last parameter of true. This is also an optional argument to opts.parse.

opts.parse(options, true);
// or if you want more control, you can do:
/*
  options.push({
    long        : 'help',
    description : 'Show this help message',
    callback    : require('opts').help,
  }
  opts.parse(options);
*/

AUTHOR / CHANGELOG / LICENSE

Email: [email protected]

Relatively unchanged since 2010. .RE See https://github.com/khtdr/opts/blob/master/CHANGES.org

BSD 2-Clause License .RE See https://github.com/khtdr/opts/blob/master/LICENSE.txt