Socket
Socket
Sign inDemoInstall

@danmasta/args

Package Overview
Dependencies
2
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @danmasta/args

Arguments parser for node apps


Version published
Weekly downloads
1
Maintainers
1
Install size
1.41 MB
Created
Weekly downloads
 

Readme

Source

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

nametypedescription
posbooleanIf true any positional arguments from argv will be added to output at _. Default is true
subbooleanIf true any sub-process arguments from argv will be added to output at --. Default is true
defaultsobjectDefault option values to set for all arguments. Default is null

Methods

namedescription
resolve(locals)Resolve argument values with optionanl local overrides

Arguments

Arguments can be configured with the following options:

nametypedescription
idstringThis is used for initial value lookup from argv. It is also used as the output key in resolved options. Default is undefined
descriptionstringDescription for the argument. Default is undefined
aliasstringWhich alias key to use for argv arguments. Default is undefined
argvbooleanWhether or not to parse argument values from process.argv. Default is true
envbooleanWhich env variable key to use for value lookup. Default is undefined
camelbooleanIf true will also support camelCase keys for id in argv arguments. Default is false
snakebooleanIf true will also support snake_case keys for id in argv arguments. Default is false
kebabbooleanIf true will also support kebab-case keys for id in argv arguments. Default is true
typeconstructor|functionIf 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
enumarray|anyIf set, will attempt to validate against this value. Can be an array of possible values or a single value. Default is undefined.
requiredbooleanIf true will create an error if value is undefined. Default is false
valueanyValue of the argument. Default is undefined
defaultanyWhich value to use as the default for specified argument. Default is undefined
parseUndefinedbooleanIf false will ignore undefined values when attempting to parse to native and/or custom types. Default is true
warnbooleanIf true will write errors to stderr for missing required fields and invalid enum values. Default is false
throwbooleanIf true will throw errors on missing required fields and invalid enum values. Default is false
nativeTypebooleanIf true will attempt to coerce values to native types if possible. Default is true
nullablebooleanIf 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

Keywords

FAQs

Last updated on 18 Apr 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc