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

hash-arg

v1.0.9

Published

CLI parameter parser to get the named and typed value

Downloads

35,923

Readme

hash-arg

version license Build Status Coverage Status node version

This is a CLI parameter parser to get the named and typed value. But any options started with '-' or '--' never be parsed by this package. If such options are needed, At first, use other option parser like 'node-getopt' and then process the rest parameters with this package.

Simple use with 'process.argv'

Simply, get method names each elements in process.argv.

simple.js

const args = require("hash-arg").get(
        "inputFilePath outputFilePath");

console.log(JSON.stringify(args, null, "  "));

Outputs:

$ node test/simple.js input.json output.json
{
  "inputFilePath": "input.json",
  "outputFilePath": "output.json"
}

Using with an argv parser like 'node-getopt' module

The optional second parameter of the get method is normal array. So, for instance, you can specify a node-getopt's argv property for it.

with-node-argv.js

getopt = require("node-getopt").create([
    ['s', '', 'short option'],
    ['l', 'long', 'long option'],
    ['S', 'short-with-arg=ARG', 'option with argument']
]).parseSystem();

args = require("hash-arg").get([
        "inputFilePath",
        {
            "name":"outputFilePath",
            "default": "out.json"
        }
        ], getopt.argv);

console.log(JSON.stringify(args, null, "  "));

Outputs:

$ node test/with-node-getopt.js -S DUMMY input.json -sl output.json
{
  "inputFilePath": "input.json",
  "outputFilePath": "output.json"
}

METHOD GET

prototype

HashArg.get(<argument-def> [, <argv-source-array>]);

argument-def

This can be specified as a string, an array of string, or an array of definition object.

1) string

The string that contains parameter names separated by space.

"inputFilePath outputFilePath"

If the string contains ';' character, each elements splited by the character declare the type and name.

"string inputFilePath; number countOfFile"

Or, following type specification is also available. It is used in a UML class diagram.

"inputFilePath:string; countOfFile:number"

When the type is not specified, it is regarded for var.

2) Array of string

Each element represents the parameter name.

["inputFilePath", "outputFilePath"]

type declaration:

You can specify the type of the value. The available type is 'string' or 'number'.

When the declaration is separated by space, it represents the type and its name.

And, when it is separated by a colon, those are the name and its type.

["string inputFilePath", "number countOfFile"]

And, Following is available too.

["inputFilePath:string", "countOfFile:number"]

specify default value:

You can specify the default value, If the value is not specified.

['inputFilePath:string="foo.txt"', "countOfFile:number=1234"]

A string value must be quoted by double quotation rather than single, or the parsing will fail. This is a specification of JSON.parse.

When the default value is not declared, null will be used.

3) Array of definition object

Following declaration is available.

[
    {"name":"inputFilePath"},
    {
        "name"      : "outputFilePath",
        "type"      : "string" // 'string' or 'number'
        "default"   : "out.json"
    }
]

Type Specification

To specify the type to a named parameter. Following two styles are available.

  1. "<type> <name>" - ( C/C++ style )
  2. "<name> : <type>" - ( UML style )

Array Type Specification

The last argument can be set as an array. The rest arguments in the list will be contained to the parameter.

To specify, pair of square brackets could be put after the type name. The brackets must be empty.

Followings are all now available.

["string inputFilePath", "number[] countOfFile"]
["inputFilePath:string", "countOfFile:number[]"]
[
    {"name":"inputFilePath"},
    {
        "name"      : "outputFilePath",
        "type"      : "string[]"
    }
]

argv-source-array (optional)

An array of string to parse as command line parameters.

The process.argv is used by default, when it is not specified,

LICENSE

MIT