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

@thaerious/parseargs

v1.3.1

Published

Easily parse command line arguments in NodeJS.

Downloads

23

Readme

Node Argument Parser

A simple no-dependency NodeJS module for parsing command line arguments. Unlike the built-in argument parser, this does not require tracked arguments to be declared in advance. Additionally, it allows for counting argument occurances.

Features:

  • Parses single hyphen (-) and double hyphen (--) arguments.
  • Permits but does not require custom configuration.
  • Count flag occurances.

Installation

npm i @thaerious/parseargs

Default Behaviour

import ParseArgs from "../src/ParseArgs.js";
const parseArgs = new ParseArgs();

Command line arguments which are prefixed by a single '-', or double '--' dash are considered 'flags'. All other arguments are considered 'parameters'.

  • A flag followed immediately by a parameter will be assigned that parameters string value.
  • A flag with no parameter will be assigned boolean 'true'.
  • Single dash flags consist of a single character and can be chained.
  • The -- flag without text ends parsing.
  • Flag names are case sensitive.

see /demo/default.js for examples

Double Dash Flags

Flags starting with two dashes take the name of the string after the dashes. If it is followed by a parameter the flag will be assigned that value, otherwise it is assigned boolean 'true'.

Single Dash Flags

Flags starting with a single dash can be chained. Single dash flags consist of a single character. Only the last flag in the chain will be assigned a parameter value.

Example

$> node demo/default.js --path . --no-copy -abc all

{
  path: '.',
  'no-copy': true,
  a: true,
  b: true,
  c: 'all',
  '$': [
    '/opt/node/19.3.0/bin/node',
    '/home/user/parser/demo/default.js'
  ]
}

Escape Processing

All arguments found after a lone '--' are not processed and will be appended to the '$' field.

$> node demo/default.js --input filename --output -- not_processed

{
  input: 'filename',
  output: true,
  '$': [
    '/opt/node/19.3.0/bin/node',
    '/home/user/parser/demo/default.js',
    'not_processed'
  ]
}

The ParseArgs object

To access a flag's value on the 'ParseArgs' object, treat the flag like a normal POJO value. For example to access the alpha flag use: parseArgs.alpha or parseArgs["alpha"].

To access the remaining unprocessed arguments use: parseArgs.$.

Custom Behaviour

Custom behaviour is defined by passing a definition object to the ParseArgs constructor. This allows you to define a short form flag, the type of flag, and it's default value.

import ParseArgs from "../src/ParseArgs.js";
const parseArgs = new ParseArgs(definitions);

The definitions object must contain a 'flags' field which contains an array of flag-definition objects.

const definitions = {    
    flags: [
        {
            long: "pack",
            short: "p",
            default: "a.json",
            type: "string",
        },
        {
            long: "verbose",
            short: "v",
            type: "count",
        }  
    ]
};
  • Flags must have a long form name.
  • Flags may have a short form name.
  • The default value will be used if no parameter is available on the command line.
  • String flags with no default will be assigned the empty string as it's default.
  • Boolean and Count flags will not consume a parameter.
  • Short forms for a flag will share all values with it's long form.
  • Flags with a short form will only have the long form name in the resulting object.
  • Flags with an env field will that their value added to process.env in the specified field.

Flags (non-boolean) can be assigned a default value. These flags will take on the default value if none is provided. Count flags have a default value of 0 if not otherwise specified; they will start counting at their default value.

Default Flags (no type)

  • The default value of default flags is boolean false.
  • Default flags that are present and did not consume an argument will be assigned the value boolean 'true', unless the default otherwise specified.
  • Default flags that are present and consumed an argument will be assigned the consumed arguments value.

String Flags

  • The default value of string flags is a zero-length string unless otherwise specified.
  • String flags that are present and did not consume a parameter will be assigned the default value.
  • They consume the next parameter if available and use that for their value.

definition

const definitions = {    
    flags: [
        {
            long: "pack",
            short: "p",
            default: "a.json",
            type: "string",
        }
    ]
}

result [1]

$ /index.js
{ pack : 'a.json' }

$ /index.js --pack file.tar
{ pack : 'file.tar' }

Boolean Flags

  • The value of boolean flags is true if they are present, otherwise false.
  • Boolean flags do not have default values.
  • They do not consume parameters.

definition

const definitions = {    
    flags: [
        {
            long: "condense",
            short: "c",
            type: "boolean",
        }
    ]
}

result [1]

$ /index.js
{ condense : false } # default value

$ /index.js --condense file.tar # last argument ignored
{ condense : true }

$ /index.js -c file.tar # short form flag
{ condense : true }

Count Flags

  • The value of count flags is the number of times the flag appears.
  • If not present the default value is 0.
  • Counting start at the devault value.
  • They do not consume parameters.

definition

const definitions = {    
    flags: [
        {
            long: "verbose",
            short: "v",
            type: "count",
        }
    ]
}

result [1]

$ /index.js
{ verbose : 0 }

$ /index.js -v 
{ verbose : 1 }

$ /index.js -vv
{ verbose : 2 }

Notes

[1] Unprocessed arguments ($) excluded for brevity.