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

command-line-parser

v0.2.10

Published

Simple lightweight function takes an array of command-line arguments and returns a parsed object

Downloads

3,029

Readme

command-line-parser

This statement:

const argsObj = require('command-line-parser')();

returns this argsObj object:

{
	v: true,
	debug: true,
	host: 'there.com',
	port: '8081',
	_args: [ 'myfile1', 'myfile2' ]
}

when invoked with:

node ./myscript.js myfile1 -v -debug -host there.com -port 8081 myfile2

This simple lightweight module exports a default function that takes an array of command-line arguments and returns a parsed object (a bit simpler than the venerable minimist or commander).

Arguments as keys and values

A key argument (starts with a dash) looks to the next argument for a value, such as -foo bar. If foo does not find a value, a default value of true will be set.

There's a few ways in which foo might not find a value:

  • the following argument also starts with a dash, as in -foo -hey
  • there are no more arguments
  • foo is set in booleanKeys of config (an optional argument):
const argsObj = require('command-line-parser')({
	booleanKeys: [ 'foo' ]
});

A key with multiple leading dashes such as --debug will be the same as -debug (with the exception of overriding the optional argument allowKeyGrouping described below).

A key with embedded dashes or spaces will be converted to camelCase from camel-case.

Arguments that don't fulfill the role of a key or a value will be added in an array to a key called _args in the parsed object.

To avoid any arguments intended for _args from becoming the unintentional value of a preceding boolean key, either specify that boolean key in booleanKeys of config, or follow that boolean key with a placeholder argument such as a single - or double dash -- on the command line.

node ./myscript.js -d -- foo1 foo2 foo3

Embedding numerical values within keys

Instead of having to separate a numerical argument from its key:

node ./myscript.js -n 8

it may be embedded in the key:

node ./myscript.js -n8

by setting allowEmbeddedValues in the config:

const argsObj = require('command-line-parser')({
	allowEmbeddedValues: true
});

to give the result

{
	n: 8
}

Single letter key grouping

To parse a leading-dash argument such as -abc as single-letter keys instead of one multi-letter key, then set allowKeyGrouping in config so that

node ./myscript.js -abc

when parsed by

const argsObj = require('command-line-parser')({
	allowKeyGrouping: true
});

gives the result

{
	a: true,
	b: true,
	c: true
}

allowKeyGrouping can be overridden on the command-line by prefixing the argument with double-dashes, so that

node ./myscript.js --abc

becomes

{
	abc: true
}

Passing configuration

An optional configuration object may be passed in that will override these default values:

const argsObj = require('command-line-parser')({
	args = process.argv.slice(2),	// skip the binary and script file args
	booleanKeys = [],
	allowKeyGrouping: false,
	allowEmbeddedValues: false
});

Installation and testing

Install with npm install command-line-parser.

Test via npm test (and see test/index.js for some examples).

Useful destructuring assignment pattern

This is a common pattern using destructuring assignment (available in ES6 or via Babel) which can be useful here, especially with default values, and perhaps renaming _args:

const {
	v,
	debug = false,
	host = 'default.com',
	port = '80',
	_args: files = []
} = argsObj ;

will assign the locally scoped constant variables v, debug, host, port, and files.