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

pixl-args

v1.0.6

Published

A simple module for parsing command line arguments.

Downloads

3,462

Readme

Overview

This module provides a simple interface to the command-line arguments used to instantiate Node.js scripts. It parses double-dash key/value arguments in the form --key value --key value. The library then provides a get() method to get individual values, or everything as one object. Multiple keys with the same name are pushed onto an array. No dependencies.

Usage

Use npm to install the module:

npm install pixl-args

Then use require() to load it in your code:

const Args = require('pixl-args');

To use the module, instantiate an object:

let args = new Args();

This will, by default, parse all the Node.js command-line args used to instantiate your script. They are then accessible by calling get() on your args object, passing in the key you are interested in. So, imagine if your script was invoked on the CLI thusly:

node your-script.js --verbose 1 --debug 0

You could then access the command-line arguments like this:

let verbose = args.get('verbose');
let debug = args.get('debug');

If you just want a hash of all the arguments, call get() without passing a key:

let opts = args.get();
if (opts.verbose) console.log("Verbose flag is set.");
if (opts.debug) console.log("Debug flag is set.");

Default Args

You can pass in a hash of default arguments to the class constructor. The command-line will override these, or add new ones. Example:

let args = new Args( {
	verbose: 0,
	debug: 0
} );

Valueless Args

Arguments without an explicit value are set to Boolean true. Example:

node your-script.js --verbose --debug

Then calling get(), this becomes:

{
	"verbose": true,
	"debug": true
}

String Handling

Strings are handled by the shell, so anything crazy like spaces and such should be wrapped in quotes and/or escaped properly. The library doesn't do any special processing, and simply deals with what it gets.

node your-script.js --name "Joseph Huckaby" --city San\ Mateo

Then calling get(), this becomes:

{
	"name": "Joseph Huckaby",
	"city": "San Mateo"
}

Number Handling

Argument values which appear to be numbers are parsed as such. This includes negative and positive base-10 integers and floats. Everything else is considered to be a string.

node your-script.js --amount 50 --freq 0.5 --volume loud

Then calling get(), this becomes:

{
	"amount": 50,
	"freq": 0.5,
	"volume": "loud"
}

Duplicate Args

Duplicate arguments with the same name are converted into arrays, with the order preserved. Example:

node your-script.js --action delete --key value1 --key value2

Then calling get(), this becomes:

{
	"action": "delete",
	"key": [
		"value1",
		"value2"
	]
}

Other Args

Any command-line arguments that don't follow the --key value pattern, meaning those located before or after your keyed arguments, are appended to an other array. Example:

node your-script.js file1.txt file2.txt --action delete --key value1 --key value2

Then calling get(), this becomes:

{
	"action": "delete",
	"key": [
		"value1",
		"value2"
	],
	"other": [
		"file1.txt",
		"file2.txt"
	]
}

You can place your "other" args at the beginning or at the end of the keyed arguments. However, for the latter just beware of using a Valueless Arg as the final keyed argument.

Double-Dash Separator

If a double-dash separator (--) is encountered, then named argument processing stops, and anything after the -- is added onto the other array. Example:

node your-script.js --key1 value1 --key2 value2 -- --key3 value3 --key4 value4

Then calling get(), this becomes:

{
	"key1": "value1",
	"key2": "value2",
	"other": [
		"--key3",
		"value3",
		"--key4",
		"value4"
	]
}

Custom Input Args

The class constructor accepts an optional list of arguments to parse, which defaults to process.argv, but can be any array you give it. Example:

let args = new Args( ["--verbose", "1", "--debug", "0"] );

To combine this with the default arguments feature, pass the list of arguments array first, and the default arguments hash second.

License

The MIT License

Copyright (c) 2015 - 2024 Joseph Huckaby and PixCore.com.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.