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

parse-options

v1.0.6

Published

Parse Command Line Options

Downloads

32

Readme

Maintenance Maintaner Website shields.io made-with-Markdown made-for-VSCode GitHub license Profile views GitHub contributors GitHub issues

GitHub forks GitHub stars GitHub watchers GitHub followers

parse-options

Parse Command Line Options

Installation

npm install parse-options;

or

yarn add parse-options;

Example

Command:

node app.js copy -m --lim 5 --exclude 'node_modules' test.txt

app.js:

const parseOptions = require('parse-options');

let options = parseOptions(
  // Example pattern.
  // Contains two commands (command="copy", file="test.txt")
  // and three parameters (boolean minimize=true, number limit=5
  // and string exclude="node_modules")
  `command file @minimize|min|m #limit|lim|l $exclude|e`,
  // Command line arguments
  process.argv,
  // Handlers applied after parsing
  {
    file => path.resolve(process.env, file),
  }
);

options === {
  // Commands list
  $commands: ['copy', 'test.txt'],
  // Named commands values
  command: 'copy',
  file: 'test.txt',
  // Named parameters
  minimize: true,
  limit: 5,
  exclude: 'node_modules',
};

Using

const parseOptions = require('parse-options');

let options = parseOptions([pattern], [argv]);

Pattern

Pattern is a template string, defining options names and types. Pattern can contain commands list (values, defined by position) and named parameters with types.

You can omit pattern by specifying null as first argument. This will parse all arguments as strings or booleans by default.

Parameters

To define simple string named parameter (for example, for parsing --file 'test.txt'), specify the following pattern:

$file|f

That pattern defines argument named file, which is parsing from argv parameter --file VALUE, -f VALUE, --file=VALUE, or -f=VALUE.

Boolean and number parameters is defining the same way:

@minimize|min|m #age|a

The code above defines two parameters: boolean minimize with aliases minimize, min and m, and age with aliases age and a.

Commands

Command is a string value, gived as a positioned argument to the CLI. For, example, the following code contains two command values: copy and test.txt:

node app.js copy -a text.txt;

Commands can be specified in any order. For example, the code above will be parsed with pattern action filename @all|a as

{
  $commands: ['copy', 'text.txt'],
  action: 'copy',
  filename: 'text.txt',
  all: true,
}

Other Example

Pattern:

from to @recursive|r $exclude|e;

Command Line:

node app.js ./source/ ./dest/ --exclude 'node_modules';

Result:

{
  $commands: ['./source/', './dest/'],
  from: './source/',
  to: './dest/',
  recursive: false,
  exclude: 'node_modules',
}