nopt
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
abbrev | 1.1.1 | 2.25 kB | ISC | prod |
Nopt is an option parsing utility for Node.js. By examining user input, Nopt conveniently informs an application about the types of the values provided, making it possible to manage them accordingly. It is an important tool for writing complex, configurable programs in the Node environment. Notably, this package is leveraged by npm for its functionality.
To use Nopt, start by including it in your Node.js script using require("nopt")
.
Example:
var nopt = require("nopt")
, Stream = require("stream").Stream
, path = require("path")
, knownOpts = { "foo" : [String, null]
, "bar" : [Stream, Number]
, "baz" : path
, "bloo" : [ "big", "medium", "small" ]
, "flag" : Boolean
, "pick" : Boolean
, "many1" : [String, Array]
, "many2" : [path, Array]
}
, shortHands = { "foofoo" : ["--foo", "Mr. Foo"]
, "b7" : ["--bar", "7"]
, "m" : ["--bloo", "medium"]
, "p" : ["--pick"]
, "f" : ["--flag"]
}
var parsed = nopt(knownOpts, shortHands, process.argv, 2)
console.log(parsed)
In this script, you can define types and values for options with the knownOpts
object. Then user-input arguments can be shortened using the shortHands
object. When the script is run with different option arguments, Nopt parses them and the console.log(parsed)
output reflects the parsed options.
The documentation for Nopt is included in its source file, lib/nopt.js
, at the bottom. The tests serve as examples showcasing of how to use the package and what it is capable of doing. You can also visit the package's GitHub repository at https://github.com/npm/nopt to find the readme and code examples.