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 🙏

© 2025 – Pkg Stats / Ryan Hefner

smartarg

v1.0.1

Published

Forked repo of Arg, with smart help and version logging

Downloads

17

Readme

Smart Arg 🤓

Smart Arg is a forked repo of Arg, with smart help and version logging.

Installation

Use Yarn or NPM to install.

$ yarn add smartarg

or

$ npm install smartarg

Usage

all examples are in type script

import SmartArg from "smartarg";

interface args {
    "--say": string,
    "--secret": boolean
}

const args: args = new SmartArg<args>()
    .name("SmartArg")
    .version("0.0.1")
    .description("Forked repo of Arg, with smart help and version logging")
    .option(["-s", "--say"], String, "prints the value of --say")
    .option(["--secret"], Boolean, "prints a secret")
    .smartParse()

if (args["--say"])
    console.log(`You asked me to say ${args["--say"]}`);
if (args["--secret"])
    console.log("Checkout @eadded/firejs. Best React Static Generator");

Output on -h

image

Functions

name(string) specify project name

version(string) specify project version

description(string) specify project description

usage(string) specify commandline usage, default : name

examples(string) specify an example, default : name -h

primaryColor(number) specify ANSI color code, default : 1

secondaryColor(number) specify ANSI color code, default : 33

smartParse(config) terminates the program after printing help on [-h,--help] and version on [-v,--version], else returns result

parse(config) returns result

Result

It returns an object with any values present on the command-line (missing options are thus missing from the resulting object). SmartArg performs no validation/requirement checking - we leave that up to the application.

All parameters that aren't consumed by options (commonly referred to as "extra" parameters) are added to result._, which is always an array (even if no extra parameters are passed, in which case an empty array is returned).

For example:

$ node ./hello.js --verbose -vvv --port=1234 -n 'My name' foo bar --tag qux --tag=qix -- --foobar
// test.js
import SmartArg, {COUNT} from "smartarg";
interface args {
    "--verbose": string,
    "--port": boolean,
    "--name": string,
    "--tag": [string],
    "--label": string
}

const args: args = new SmartArg<args>()
    .option(["-v", "--verbose"], COUNT, "Counts the number of times --verbose is passed")
    .option(["-p", "--port"], COUNT, "Counts the number of times --verbose is passed")
    .option(["-n", "--name"], String, "Counts the number of times --verbose is passed")
    .option(["-t", "--tag"], [String], "Counts the number of times --verbose is passed")
    .option(["--label"], COUNT, "Counts the number of times --verbose is passed")
    .parse();

console.log(args);
/*
{
	_: ["foo", "bar", "--foobar"],
	'--port': 1234,
	'--verbose': 4,
	'--name': "My name",
	'--tag': ["qux", "qix"]
}
*/

The values for each key=>value pair is either a type (function or [function]) or a string (indicating an alias).

  • In the case of a function, the string value of the argument's value is passed to it, and the return value is used as the ultimate value.

  • In the case of an array, the only element must be a type function. Array types indicate that the argument may be passed multiple times, and as such the resulting value in the returned object is an array with all of the values that were passed using the specified flag.

  • In the case of a string, an alias is established. If a flag is passed that matches the key, then the value is substituted in its place.

Type functions are passed three arguments:

  1. The parameter value (always a string)
  2. The parameter name (e.g. --label)
  3. The previous value for the destination (useful for reduce-like operations or for supporting -v multiple times, etc.)

This means the built-in String, Number, and Boolean type constructors "just work" as type functions.

Note that Boolean and [Boolean] have special treatment - an option argument is not consumed or passed, but instead true is returned. These options are called "flags".

For custom handlers that wish to behave as flags, you may pass the function through flag():

import SmartArg, {flag} from "./SmartArg";

const argv = ['--foo', 'bar', '-ff', 'baz', '--foo', '--foo', 'qux', '-fff', 'qix'];

function myHandler(value, argName, previousValue) {
    /* `value` is always `true` */
    return 'na ' + (previousValue || 'batman!');
}

interface args {
    "--foo": string
}

const args = new SmartArg<args>()
    .option(["-f","--foo"], flag(myHandler),"")
    .parse({argv});

console.log(args);
/*
{
	_: ['bar', 'baz', 'qux', 'qix'],
	'--foo': 'na na na na na na na na batman!'
}
*/

As well, SmartArg supplies a helper argument handler called COUNT, which equivalent to a [Boolean] argument's .length property - effectively counting the number of times the boolean flag, denoted by the key, is passed on the command line.

Options

Object passed to parse() or smartParse() specifies parsing options to modify the behavior.

argv

If you have already sliced or generated a number of raw arguments to be parsed (as opposed to letting SmartArg slice them from process.argv) you may specify them in the argv option.

permissive

When permissive set to true, SmartArg will push any unknown arguments onto the "extra" argument array (result._) instead of throwing an error about an unknown flag.

stopAtPositional

When stopAtPositional is set to true, SmartArg will halt parsing at the first positional argument.

Errors

Some errors that SmartArg throws provide a .code property in order to aid in recovering from user error, or to differentiate between user error and developer error (bug).

ARG_UNKNOWN_OPTION

If an unknown option (not defined in the spec object) is passed, an error with code ARG_UNKNOWN_OPTION will be thrown

License & Copyright

Copyright (C) 2020 Aniket Prajapati

Licensed under the MIT LICENSE

Contributors