nodejs-argv

1.0.2 • Public • Published

nodejs-argv

Simple command line parsing nodejs module.

Installation

$ npm install nodejs-agrv

Usage

e.g.

const nodejs_argv = require("../nodejs-argv");
const argv = nodejs_argv.new()
argv.option([
    ["-u", "--url",      "str=",         "Destination address"],
    ["-o", "--outdir",   "str",         "Save the directory"],
    ["--limit",          "num=5",       "Concurrency limit"],
    ["--types",          "[]",          "Limit file type"],
    ["-r", "--restart",  "bool",        "Restart"],
    ["-v", "--version",  "bool",        "Display version"],
    ["-h", "--help",                    "Display help"]
])
try {
    argv.parse()
}catch (e) {
    console.log("Error:", e)
}
if (argv.get("-v")) {
    console.log("Version:", argv.version() )
}
if (argv.get("-h")) {
    console.log("Help:\n", argv.help() )
}
console.log("Argv Values:\n", argv.values() )
$ node test.js --help

1

$ node test.js -v

1

$ node test.js -u google.com --types png jpg gif -o ./download

1

e.g. Arguments is error!

$ node test.js --outdir ./download
$ node test.js --limit abc

1 1

Nodejs-argv Module Methods

  • new(options, args)

    arguments options and args is optional

    Return a new nodejs-argv object

Nodejs-argv Object Methods

  • option(options)

    Define options, argument like [[shot name, long name, type=default value, description]]

    e.g.

    argv.option([["-u", "--url", "str=", "Destination address"]])
    // —url :  limit type is string and can not be empty
     
    argv.option([["--limit", "num=5", "Concurrency limit"]])
    // --limit:  limit type is number and default value is 5
     
    argv.option([["--types", "[]", "Limit file type"]])
    // --types:  limit type is array
  • parse(args)

    Parse command line arguments, args type is array. if not args, then parse process.argv

  • get(name)

    Return command line arguments

  • help()

    Return options description string

  • values()

    Return all the arguments

  • pipe(callback, timeout = 500)

    Receive stdin from the command line

    argv.pipe(function(value){
        console.log("pipe value:\n", value)
    }, 1000)
    $ ls ./ | node test.js

    1

  • version()

    Return package.json.version in the require.main directory

Package Sidebar

Install

npm i nodejs-argv

Weekly Downloads

8

Version

1.0.2

License

MIT

Last publish

Collaborators

  • wl879