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

@danmasta/args

v0.0.3

Published

Arguments parser for node apps

Downloads

6

Readme

Args

Parse arguments from argv, env variables, and locals

Features:

  • Easy to use
  • Parse arguments from argv, env variables, and locals
  • Coerce to native and/or specified type (class or factory function)
  • Supports defaults and aliases
  • Supports multiple key formats (snake, camel, kebab)
  • Error handling options
  • Supports positional and sub process (--) arguments
  • Supports enums

About

I needed a better way to parse application arguments across a variety of interfaces. It's simple enough if you build only a node api, but what if you want to expose a cli component as well? Now you have to worry about parsing argv options and types from multiple formats. What if you wanted to expose configuration for the same options via env variables? It starts to become a complicated mess of parsing values and remembering order, precidence, and types.

This package aims to take care of all that for you. Simply define your arguments in a declarative way whether you want env variables, argv variables, or local variables and it will resolve all the options and return a single object. You can request specific types by providing classes or factory functions, you can set defaults, and you can specify required-ness and how to handle errors.

Usage

Add args as a dependency for your app and install via npm

npm install @danmasta/args --save

Require the package in your app

const args = require('@danmasta/args');

Options

name | type | description -----|------|------------ pos | boolean | If true any positional arguments from argv will be added to output at _. Default is true sub | boolean | If true any sub-process arguments from argv will be added to output at --. Default is true defaults | object | Default option values to set for all arguments. Default is null

Methods

name | description -----|------------ resolve(locals) | Resolve argument values with optionanl local overrides

Arguments

Arguments can be configured with the following options: name | type | description -----|------|------------ id | string | This is used for initial value lookup from argv. It is also used as the output key in resolved options. Default is undefined description | string | Description for the argument. Default is undefined alias | string | Which alias key to use for argv arguments. Default is undefined argv | boolean | Whether or not to parse argument values from process.argv. Default is true env | boolean | Which env variable key to use for value lookup. Default is undefined camel | boolean | If true will also support camelCase keys for id in argv arguments. Default is false snake | boolean | If true will also support snake_case keys for id in argv arguments. Default is false kebab | boolean | If true will also support kebab-case keys for id in argv arguments. Default is true type | constructor\|function | If set, after the value has been resolved it will attempt to coerce to arg.type. If a constructor it will use new type(val) otherwise just type(val). Default is undefined enum | array\|any | If set, will attempt to validate against this value. Can be an array of possible values or a single value. Default is undefined. required | boolean | If true will create an error if value is undefined. Default is false value | any | Value of the argument. Default is undefined default | any | Which value to use as the default for specified argument. Default is undefined parseUndefined | boolean | If false will ignore undefined values when attempting to parse to native and/or custom types. Default is true warn | boolean | If true will write errors to stderr for missing required fields and invalid enum values. Default is false throw | boolean | If true will throw errors on missing required fields and invalid enum values. Default is false nativeType | boolean | If true will attempt to coerce values to native types if possible. Default is true nullable | boolean | If true will allow undefined for required fields or enums. Default is false

Examples

const Args = require('@danmasta/args');

process.env.PROJECT='APP';
process.env.HOST='app.net';
process.env.PORT=8080;

let args = [
    {
        id: 'project',
        type: String,
        env: 'PROJECT'
    },
    {
        id: 'host',
        alias: 'h',
        type: String,
        env: 'HOST'
    },
    {
        id: 'port',
        alias: 'p',
        type: Number,
        env: 'PORT'
    }
];

let opts = Args(args).resolve();

// {
//     project: 'APP',
//     host: 'app.net',
//     port: 8080
// }

Testing

Testing is currently run using mocha and chai. To execute tests just run npm run test. To generate unit test coverage reports just run npm run coverage

Contact

If you have any questions feel free to get in touch