0.2.7 • Published 9 years ago

dot-argv v0.2.7

Weekly downloads
40
License
-
Repository
github
Last release
9 years ago

node-dot-argv

A command-line argument helper for Node.js that turns complex argument keys (with dot paths) into objects with JSON parsed values. Use inspired by ecdeveloper's very useful named-argv.

Installing

npm install dot-argv

Usage

Execute:

node app.js param0 --opt1 value1 -opt2 val=ue2 -opt3 \"value3\" -opt4.a.x [3,\"ok\",false] -opt5\\.z {\"b\":3} par\\=am1 {\"c\":3}

Where, in app.js:

var argv = require('dot-argv');
console.log(argv.opts); 
console.log(argv.params);

Which gives argv.opts as:

{
    "opt1": "value1",
    "opt2": "val=ue2",
    "opt3": "value3",
    "opt4": {
        "a": {
            "x": [ 3, "ok", false ]
        }
    }, 
    "opt5.z": {
        "b": 3
    }
}

And argv.params as:

[ "param0", "par=am1", { "c": 3 } ]

Extras

val

Read or write a value in an object by its dotted path. E.g. with the argv.opts from the usage example:

argv.val(argv.opts, "opt4.a.x"); // returns [ 3, "ok", false ]
argv.val(argv.opts, "opt4.a.x", 1000); // writes the value

argv.val(argv.opts, "opt5\\.z.b"); // returns 3
argv.val(argv.opts, "opt5\\.z.b", 4); // writes the value

collapse/expand

Collapse an object into a single level. E.g. with the argv.opts from the usage example:

argv.collapse(argv.opts);

Returns:

{
    "opt1": "value1",
    "opt2": "val=ue2",
    "opt3": "value3",
    "opt4.a.x.0": 3,
    "opt4.a.x.1": "ok",
    "opt4.a.x.2": false,
    "opt5\\.z.b": 3
}

Recreate the structure with:

argv.expand(flat);

Which returns a copy of the original object, argv.opts.

overwrite

Write properties in one object into another. Useful if you have an object containing default values that need to be partially overwritten. E.g. writing argv.opts into a defaults object:

var defaults = {
    "opt1": "default value1",
    "opt4": {
        a: false;
    },
    "opt6": 1000
};

var opts = argv.overwrite(argv.opts, defaults);

Then opts is:

{
    "opt1": "value1",
    "opt2": "val=ue2",
    "opt3": "value3",
    "opt4": {
        "a": {
            "x": [ 3, "ok", false ]
        }
    }, 
    "opt5.z": {
        "b": 3
    },
    "opt6": 1000 // this property is preserved from defaults object 
}

arrayify

Create an array of command-line option strings. E.g. with the argv.opts in the usage example:

argv.arrayify(argv.opts);

Returns an array of the collapsed form, with JSON.stringify()'d values:

[
    '"param0"', 
    '"par\\=am1"', 
    '{"c":3}',
    'opt1', '"value1"',
    'opt2', '"val=ue2"',
    'opt3', '"value3"',
    'opt4.a.x.0', '3',
    'opt4.a.x.1', '"ok"',
    'opt4.a.x.2', 'false',
    'opt5\\.z.b', '3'
]

Order of the keyed options is not guaranteed.

stringify

Similar to arrayify, but returns a string. E.g. with the argv.opts in the usage example:

argv.arrayify(argv.opts);

Returns the string:

'"param0" "par\\=am1" {"c":3} --opt1 "value1" -opt2 "val=ue2" -opt3 "value3" -opt4.a.x.0 3 -opt4.a.x.1 "ok" -opt4.a.x.2 false -opt5\.z.b 3' 
0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago