Gathering detailed insights and metrics for nopt
Gathering detailed insights and metrics for nopt
Gathering detailed insights and metrics for nopt
Gathering detailed insights and metrics for nopt
npm install nopt
99.3
Supply Chain
99.5
Quality
83.6
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
528 Stars
164 Commits
43 Forks
39 Watching
3 Branches
85 Contributors
Updated on 26 Sept 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-6.4%
6,239,729
Compared to previous day
Last week
2.4%
36,537,265
Compared to previous week
Last month
9.8%
150,496,073
Compared to previous month
Last year
14.7%
1,614,555,641
Compared to previous year
1
3
If you want to write an option parser, and have it be good, there are two ways to do it. The Right Way, and the Wrong Way.
The Wrong Way is to sit down and write an option parser. We've all done that.
The Right Way is to write some complex configurable program with so many options that you hit the limit of your frustration just trying to manage them all, and defer it with duct-tape solutions until you see exactly to the core of the problem, and finally snap and write an awesome option parser.
If you want to write an option parser, don't write an option parser. Write a package manager, or a source control system, or a service restarter, or an operating system. You probably won't end up with a good one of those, but if you don't give up, and you are relentless and diligent enough in your procrastination, you may just end up with a very nice option parser.
1// my-program.js 2var nopt = require("nopt") 3 , Stream = require("stream").Stream 4 , path = require("path") 5 , knownOpts = { "foo" : [String, null] 6 , "bar" : [Stream, Number] 7 , "baz" : path 8 , "bloo" : [ "big", "medium", "small" ] 9 , "flag" : Boolean 10 , "pick" : Boolean 11 , "many1" : [String, Array] 12 , "many2" : [path, Array] 13 } 14 , shortHands = { "foofoo" : ["--foo", "Mr. Foo"] 15 , "b7" : ["--bar", "7"] 16 , "m" : ["--bloo", "medium"] 17 , "p" : ["--pick"] 18 , "f" : ["--flag"] 19 } 20 // everything is optional. 21 // knownOpts and shorthands default to {} 22 // arg list defaults to process.argv 23 // slice defaults to 2 24 , parsed = nopt(knownOpts, shortHands, process.argv, 2) 25console.log(parsed)
This would give you support for any of the following:
1$ node my-program.js --foo "blerp" --no-flag 2{ "foo" : "blerp", "flag" : false } 3 4$ node my-program.js ---bar 7 --foo "Mr. Hand" --flag 5{ bar: 7, foo: "Mr. Hand", flag: true } 6 7$ node my-program.js --foo "blerp" -f -----p 8{ foo: "blerp", flag: true, pick: true } 9 10$ node my-program.js -fp --foofoo 11{ foo: "Mr. Foo", flag: true, pick: true } 12 13$ node my-program.js --foofoo -- -fp # -- stops the flag parsing. 14{ foo: "Mr. Foo", argv: { remain: ["-fp"] } } 15 16$ node my-program.js --blatzk -fp # unknown opts are ok. 17{ blatzk: true, flag: true, pick: true } 18 19$ node my-program.js --blatzk=1000 -fp # but you need to use = if they have a value 20{ blatzk: 1000, flag: true, pick: true } 21 22$ node my-program.js --no-blatzk -fp # unless they start with "no-" 23{ blatzk: false, flag: true, pick: true } 24 25$ node my-program.js --baz b/a/z # known paths are resolved. 26{ baz: "/Users/isaacs/b/a/z" } 27 28# if Array is one of the types, then it can take many 29# values, and will always be an array. The other types provided 30# specify what types are allowed in the list. 31 32$ node my-program.js --many1 5 --many1 null --many1 foo 33{ many1: ["5", "null", "foo"] } 34 35$ node my-program.js --many2 foo --many2 bar 36{ many2: ["/path/to/foo", "path/to/bar"] }
Read the tests at the bottom of lib/nopt.js
for more examples of
what this puppy can do.
The following types are supported, and defined on nopt.typeDefs
Date
is one of the options,
then it will return a Date object, not a string.true
or false
. If an option is a boolean,
then it does not need a value, and its presence will imply true
as
the value. To negate boolean flags, do --no-whatever
or --whatever false
outfd
and logfd
config options.)Array
is specified as one of the types, then the value
will be parsed as a list of options. This means that multiple values
can be specified, and that the value will always be an array.If a type is an array of values not on this list, then those are
considered valid values. For instance, in the example above, the
--bloo
option can only be one of "big"
, "medium"
, or "small"
,
and any other value will be rejected.
When parsing unknown fields, "true"
, "false"
, and "null"
will be
interpreted as their JavaScript equivalents.
You can also mix types and values, or multiple types, in a list. For
instance { blah: [Number, null] }
would allow a value to be set to
either a Number or null. When types are ordered, this implies a
preference, and the first type that can be used to properly interpret
the value will be used.
To define a new type, add it to nopt.typeDefs
. Each item in that
hash is an object with a type
member and a validate
method. The
type
member is an object that matches what goes in the type list. The
validate
method is a function that gets called with validate(data, key, val)
. Validate methods should assign data[key]
to the valid
value of val
if it can be handled properly, or return boolean
false
if it cannot.
You can also call nopt.clean(data, types, typeDefs)
to clean up a
config object and remove its invalid properties.
By default, nopt outputs a warning to standard error when invalid values for
known options are found. You can change this behavior by assigning a method
to nopt.invalidHandler
. This method will be called with
the offending nopt.invalidHandler(key, val, types)
.
If no nopt.invalidHandler
is assigned, then it will console.error
its whining. If it is assigned to boolean false
then the warning is
suppressed.
Yes, they are supported. If you define options like this:
1{ "foolhardyelephants" : Boolean 2, "pileofmonkeys" : Boolean }
Then this will work:
1node program.js --foolhar --pil 2node program.js --no-f --pileofmon 3# etc.
Shorthands are a hash of shorter option names to a snippet of args that they expand to.
If multiple one-character shorthands are all combined, and the combination does not unambiguously match any other option or shorthand, then they will be broken up into their constituent parts. For example:
1{ "s" : ["--loglevel", "silent"] 2, "g" : "--global" 3, "f" : "--force" 4, "p" : "--parseable" 5, "l" : "--long" 6}
1npm ls -sgflp 2# just like doing this: 3npm ls --loglevel silent --global --force --long --parseable
The config object returned by nopt is given a special member called
argv
, which is an object with the following fields:
remain
: The remaining args after all the parsing has occurred.original
: The args as they originally appeared.cooked
: The args after flags and shorthands are expanded.Node programs are called with more or less the exact argv as it appears
in C land, after the v8 and node-specific options have been plucked off.
As such, argv[0]
is always node
and argv[1]
is always the
JavaScript program being run.
That's usually not very useful to you. So they're sliced off by
default. If you want them, then you can pass in 0
as the last
argument, or any other number that you'd like to slice off the start of
the list.
No vulnerabilities found.
Reason
all changesets reviewed
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
9 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 7
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More