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

@ryel/arguments

v1.0.0

Published

Parse conventional CLI arguments.

Downloads

3

Readme

arguments

Parse conventional CLI arguments.

--help and --verbose arguments are included as defaults.

{
	help: {
		shortName: 'h',
		type: Boolean,
	},
	verbose: {
		shortName: 'v',
		type: Boolean,
	},
}

API

import ArgumentParser from '@ryel/arguments';

const argumentsParser = new ArgumentParser(argumentsSpecification, options);

const args = argumentsParser.parse();

argumentsSpecification is an object specifying all expected arguments. The specification must provide a long name for each argument, but all other properties are optional.

By default an argument will be considered a boolean unless a type is provided. Arguments specified with a type of Boolean can either be provided as flags, or can be provided as an argument with a value that is boolean like. Boolean like values include true, t, yes, y and 1 as truthy and false, f, no, n and 0 as falsy. Arguments specified with a type of Number will be parsed from a string to a number.

A short name can be provided by shortName and must be a single character which is unique to all other arguments.

If an argument should always be provided, required can be specified for the argument as true.

A default value can be specified for an argument, to be used in the absence of the argument using defaultValue. By default, boolean arguments are considered to have a defaultValue of false.

If boolean, string and number arguments aren't enough, mapValue can be specified for an argument as a function to transform the argument as required.

options can be an object with the following properties:

helpUsage: string = 'Options:';
helpOnNoArgs: boolean = true;
errorOnUnknownArgs: boolean = true;

helpUsage is a string to be output to the console before listing the usage of all arguments in the specification.

helpOnNoArgs will default to output help and argument usage to the console when no arguments are provided.

errorOnUnknownArgs will default to throwing an error when the parser encounters an argument not provided in the specification.

Using the following example arguments specification:

{
	age: {
		shortName: 'a',
		type: Number,
	},
	fullName: {
		shortName: 'n',
		type: String,
		required: true,
	},
	jobTitle: {
		type: String,
		defaultValue: 'Developer',
	},
	skills: {
		type: String,
		mapValue: (value) => value.split(','),
	},
	special: {
		shortName: 's',
	},
	unplugged: {
		shortName: 'u',
		type: Boolean,
		defaultValue: true,
	},
}

Arguments specified as required must provided, otherwise an error will be thrown. All arguments that are not boolean arguments require a value to be parsed, a value can be provided with the argument name or as the following argument.

> cli --fullName="Thomas A. Anderson"
> cli --fullName "Thomas A. Anderson"

Arguments specified with a shortName such as fullName can be provided in short.

> cli -n="Thomas A. Anderson"
> cli -n "Thomas A. Anderson"

Arguments specified with a defaultValue that is truthy and that are boolean arguments, such as unplugged will be inverted to false when provided.

> cli --fullName="Thomas A. Anderson" -u

Arguments specified with a shortName that are boolean arguments, such as special and verbose, can be concatenated and provided in any order.

> cli --fullName="Thomas A. Anderson" -sv
> cli --fullName="Thomas A. Anderson" -vs

Arguments specified with a mapValue such as skills can be provided and transformed (e.g. to an array ['Budo', 'Krav Maga', 'Wushu']).

> cli --fullName="Thomas A. Anderson" --skills Budo,"Krav Maga",Wushu