Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

takamin/hash-arg

Repository files navigation

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

About

node module: Get arguments as an object represented by specified keys and values

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published