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

node-cli-arg-by-config

v0.11.0

Published

Parsing command line arguments for node scripts.

Downloads

86

Readme

Node CLI Args By Config

Summary

A command line args for node scripts. The focus is to be simple to configure and use, but powerful if you need it.

Limitations

  • only supports positional and double-dash-equals (i.e. --myarg=myval or --myflag) args.
  • assumes you will be running from a node script at this point.

Supports

  • positional args
  • named arguments (e.g., --name)
  • optional and required arguments (named only, positional are always required)
  • decent looking help, which prints help on the arguments and an optional header
  • easy way to validate your argument configuration
  • typed arguments of 'int', 'float', 'date', 'string'
  • argument factories, handy for creating an object out of a CLI arg
  • custom validation if needed

CLI examples

  • yarn myscript positinalArgVal1 positinalArgVal2 --other-arg=other-value --some-flag
  • yarn myscript --other-arg=other-value --some-flag

Configuration

Simple setup

interface IMyArgObj
{
    arg1?: string;
    arg2?: number;
    arg3: number;
}

const myConfig: IArgsConfig = {
    helpHeader: 'My little script is handy.',
    definitions: [
        { name: 'arg1' },
        { name: 'arg2', type: 'int' },
        { name: 'arg3', type: 'int', required: true }
    ],
};

validateArgDefinitions(myConfig); //for testing only as next call validates anyway

const args = processArgs<IMyArgObj>(myConfig);
if(!args)
{
    //this only happens if there was an error with args, and help was printed out
    process.exit(-1);
}

console.log(`arg1 is ${args.arg1}`);
console.log(`arg2 is ${args.arg2}`);
console.log(`arg3 plus one is ${args.arg3+1}`);

Available fields for Args

  • @param name: name of the flag. This will also be the flag set on result object.
  • @param type: type of value. If not set, and factory not set, assume 'string'. Mstring, number, date are accepted.
  • @param description: description that will appear in helper.
  • @param index: for required positional parameters. Starting at 0, ignoring interpreter and script name. i.e., ts-node npm-script arg0 arg1...
  • @param required: set to true if this argument is required. Positional default to true. Only last positional can be optional.
  • @param factory: a function which converts a string value into a TVal
  • @param validator: a function which verifies that the constructed value is correct. It returns an error message if not valid.

Example with a factory and validator

const sampelNumFactory = (s: string) => { return { theNumber: parseInt(s), ts: new Date() } };
const sampleBetween1and10 = (n: number) => (n < 1 || n > 10) ? 'must be between 1 and 10' : '';

const myConfig: IArgsConfig = {
    helpHeader: 'My little script is handy.',
    definitions: [
        { name: 'arg1' },
        { name: 'arg2', factory: sampelNumFactory, validator: sampleBetween1and10 },
        { name: 'arg3', type: 'int', required: true }
    ],
};

const args = processArgs<IMyArgObj>(myConfig);

console.log(`the number is ${args.arg2.theNumber}`);

Available Calls

  • processArgs - get arguments from the command line based on your configuration.
  • printHelp - print command line help based on your configuration.
  • validateArgDefinitions - run automatically when you process arguments, but handy for testing.