Argv-parser
Argv-parser is a small and simple node.js module to parse
process.argv
Argv-parser is designed to be simple and will do nothing about:
- option registration
- description of options
- output
Installation
npm install argv-parser --save
Usage
var parser = ;
parser.parse(argv, options)
Parse argument vector (argv) or something like argv.
ret
Object
Returns - parsed:
Object
the parsed object - warnings:
Object
the warnings of each option. If not exists,ret.warning
will be an empty object - errors:
Object
the errors of each option.
Array
argv process.argv
or something like that.
Object
options - rules:
Object
an extendednopt
rules - offset:
Number
(optional, default to2
) the offset from which the parser should start to parse.
parser.clean(data, options)
Returns
The same as parser.parse
Object
options - rules:
Object
- types:
Object
(optional) type definitions. For most cases, you needn't this option
options.rules
Example
test.js
var rules = open: type: Boolean value: true port: type: Number short: 'p' { if!port port = 9230; ifport < 8000 tool; ifport < 1000 tool; return; return port; } html: type: 'html' name: type: String ; var data = parser;
Default values:
$ node test.js
> data.parsed.open; // true
> data.parsed.port; // 9230
Type limitation:
$ node test.js --port 8888 --no-open --name name<script>alert(123)</script>
> data.parsed.open; // false
> data.parsed.port; // 8888
> data.parsed.name; // 'namealert(123)'; -> stripped
> data.errors; // {}
> data.warnings; // {}
Warnings and errors:
$ node test.js --port 888 --no-open --name name<script>alert(123)</script>
> data.parsed.open; // false
> data.parsed.port; // undefined; -> error
> data.parsed.name; // 'name<script>alert(123)</script>'
> data.errors; // {port: ['port < 100 which is forbidden']}
> data.warnings; // {port: ['port < 8000 which is dangerous']}