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

janap

v1.0.8

Published

Just Another Node Argument Parser

Downloads

91

Readme

janap

Just Another Node Argument Parser is a super small and super flexible command line argument parser for node

I wrote this because all other argument parsers out there were large, had dependencies, or didn't allow customization

Install

npm install janap

Use

var janap = require('janap');
var args = janap.parse(process.argv);

//args contain parsed arguments
//i.e. node myscript.js --arg1=false --boolean -hello world
// = {arg1: false, boolean: true, hello: "world"}

Skip Initial parsing (argument 0 and 1)

You can tell janap NOT to skip arguments 0 and 1 by setting the _initial option to false

var janap = require('janap');
var args = janap.parse(process.argv.slice(2), {
  _initial: false
});

Defaults

Defaults can be specified simply by passing a _defaults object into the options

var janap = require('janap'),
    path = require('path');

var args = janap.parse(process.argv, {
  _defaults: {
    force: true,
    path: path.resolve()
  }
});

Data Types

With janap you can force a data type per-argument:

var janap = require('janap');
var args = janap.parse(process.argv, {
  force: Boolean,
  count: Number,
  paths: Array
});

//note: you can also use strings such as "bool", "boolean", "number", "array", or something custom

Aliases

var janap = require('janap');
var args = janap.parse(process.argv, {
  _alias: {
    'o': 'option'
  }
});

//Arguments of "--o=value" or "-o value" will result in "args" object of {option: value}

Customize

janap was built with customization in mind. You can override 'match' to specify your own argument format:

var janap = require('janap');
janap.match = function(arg) {
  //We want triple dashes on our arguments
  //the second capture group is always the value
  //i.e. ---argument=value
  return ('' + arg).match(/^---([\w-]+)(?:=(.*))?$/);
}

var args = janap.parse(process.argv);

Custom type converters

With janap you can specify custom type converters. Just pass in a "_converter" key to the options object to specify your own custom converter:

var janap = require('janap');

function MyArgumentConverter() {
  janap.ArgumentConverter.call(this);
}

var p = MyArgumentConverter.prototype = Object.create(ArgumentConverter.prototype);
p.constructor = MyArgumentConverter;
p.convertValue = function(value, type) {
  if (this.name === 'my-special-case-argument')
    return 'some other value';

  //Custom data-type, specified through {'my-argument': 'custom'} in the options object
  if (type === 'custom')
    return myCustomValueConverter(value);

  if (type === 'number')
    return this.toNumber(value);

  if (type === 'boolean')
    return this.toBoolean(value);

  //this.continue tells the parser to use this value,
  //but continue onto with the next argument using the same "name" / key
  if (type === 'array') {
    var currentValue = this.get(this.name);
    if (currentValue === undefined) {
      //Not set yet
      return this.continue([value]);
    }

    return this.continue(currentValue.concat(value));
  }

  if (this.name === 'do-stuff') {
    //This adds/sets an argument called "doStuffRequested" to true in the final arguments object
    this.set('doStuffRequested', true);
    return value;
  }

  //Default value converter
  //super is a convenience helper that is always set to "ArgumentConverter.prototype"
  return this.super.convertValue.call(this, value, type);
};

var args = janap.parse(process.argv, {
  _converter: new MyArgumentConverter()
});

Enjoy! :)