![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
getopts
Advanced tools
Readme
Getopts is a high performance CLI options parser for Node.js. It is used to parse and validate the command-line arguments passed to your program at runtime. It follows the Utility Syntax Guidelines so that your programs behave like typical UNIX utilities effortlessly. Once you learn how to use it, you'll never go back to parsing process.argv
on your own again.
npm i getopts
You want to parse the command-line arguments passed to your program at runtime. How do you do that?
$ example/demo --turbo -xw10 -- alpha beta
Getopts main export is a function that takes two arguments: an array of arguments and (optional) object with options.
The command-line arguments can be found in the process.argv
array. The first item in the array is the path to the node executable, followed by the path to the file to execute. We don't need either one, so slice everything after the second index and pass it to getopts
.
const getopts = require("getopts")
const options = getopts(process.argv.slice(2), {
alias: {
w: "warp",
t: "turbo"
}
})
The return value is an object that maps the argument names to their values. Use it to look up the value of an option by its name. The underscore _
key is reserved for operands. Operands consist of bare arguments (non-options), the dash -
symbol and every argument after a double-dash --
sequence.
{
_: ["alpha", "beta"],
w: 10,
x: true,
t: true,
warp: 10,
turbo: true
}
A short option consists of a dash -
followed by a single alphabetic character. Multiple short options can be clustered together without spaces. Short options will be casted to boolean unless followed by an operand or if adjacent to one or more non-alphabetic characters matching the regular expression /[!-@[-`{-~][\s\s]*/
.
getopts(["-ab", "-c"]) //=> { _: [], a:true, b:true, c:true }
getopts(["-a", "alpha"]) //=> { _: [], a:"alpha" }
getopts(["-abc1"]) //=> { _: [], a:true, b:true, c:1 }
Only the last character in a cluster of options can be parsed as a string or as a number depending on the argument that follows it. Any options preceding it will be true
. You can use opts.string
to indicate that one or more options should be parsed as strings.
getopts(["-abc-100"], {
string: ["b"]
}) //=> { _: [], a:true, b:"c-100" }
The argument immediately following a short or a long option, which is not an option itself, will be parsed as the value of the option. You can use opts.boolean
to indicate that one or more options should be parsed as booleans, causing an adjacent argument to be parsed as an operand and not as a value.
getopts(["-a", "alpha"], {
boolean: ["a"]
}) //=> { _: ["alpha"], a:true }
Any character listed in the ASCII table can be used as a short option if it's the first character after the dash.
getopts(["-9", "-#10", "-%0.01"]) //=> { _:[], 9:true, #:10, %:0.01 }
A long option consists of two dashes --
followed by one or more characters. Any character listed in the ASCII table can be used to create a long option except the =
symbol, which separates an option's name and value.
getopts(["--turbo", "--warp=10"]) //=> { _: [], turbo:true, warp:10 }
getopts(["--warp=e=mc^2"]) //=> { _: [], warp:"e=mc^2" }
getopts(["----", "alpha"]) //=> { _: [], --:"alpha" }
Options can be negated if they are prefixed with the sequence --no-
. Their value is always false
.
getopts(["--no-turbo"]) //=> { _: [], turbo:false }
Every argument after the first double-dash sequence --
is saved to the operands array _
.
getopts(["--", "--alpha", "001"]) //=> { _:["--alpha", "001"] }
Every non-option standalone argument is an operand.
getopts(["alpha", "-w9"]) //=> { _: ["alpha"], w:9 }
getopts(["--code=alpha", "beta"]) //=> { _: ["beta"], code:"alpha" }
A dash -
is an operand.
getopts(["--turbo", "-"]) //=> { _:["-"], turbo:true }
Options missing from the arguments array designated as a boolean or string type will be added to the result object as false
and ""
respectively.
getopts([], {
string: ["a"],
boolean: ["b"]
}) //=> { _:[], a:"", b:false }
The string "false"
is always cast to a boolean false
.
getopts(["--turbo=false"]) //=> { _:[], turbo:false }
Options that appear multiple times are parsed as an array that consists of every value in the order they are found.
getopts(["-a?alpha=beta", "-aa0.1"] //=> { _:[], a:["?alpha=beta", true, 0.1] }
A value may contain newlines or other control characters.
getopts(["--text=top\n\tcenter\bottom"]) //=> { _:[], text:"top\n\tcenter\bottom" }
Parse command line arguments. Expects an array of arguments, e.g., process.argv
, an object with options, and returns an object that maps the argument names to their values.
An array of arguments.
An object of option aliases. An alias can be a string or an array of strings. Aliases let you define alternate names for an option, e.g., the short (abbreviated) and long (canonical) variations.
getopts(["-t"], {
alias: {
turbo: ["t", "T"]
}
}) //=> { _:[], t:true, T:true, turbo:true }
An array of options that should be parsed as booleans. In the example, declaring t
as boolean causes the next argument to be parsed as an operand and not as a value.
getopts(["-t", "alpha"], {
boolean: ["t"]
}) //=> { _:["alpha"], t:true }
An array of options that should be parsed as strings. In the example, by declaring t
as a string, all adjacent characters are parsed as a single value and not as individual options.
getopts(["-atabc"], {
string: ["t"]
}) //=> { _:[], a:true, t:"abc" }
An object of default values for options that are not present in the arguments array.
getopts(["--warp=10"], {
default: {
warp: 15,
turbo: true
}
}) //=> { _:[], warp:10, turbo:true }
A function that will be invoked for every unknown option. Return false
to discard the option. Unknown options are those that appear in the arguments array, but are not present in opts.string
, opts.boolean
, opts.default
, or opts.alias
.
getopts(["-abc"], {
unknown: option => "a" === option
}) //=> { _:[], a:true }
A boolean property. If true, the operands array _
will be populated with all the arguments after the first non-option.
getopts(["-w9", "alpha", "--turbo", "beta"], {
stopEarly: true
}) //=> { _:["alpha", "--turbo", "beta"], w:9 }
This property is useful when implementing sub-commands in a CLI.
const { install, update, uninstall } = require("./commands")
const options = getopts(process.argv.slice(2), {
stopEarly: true
})
const [command, subargs] = options._
if (command === "install") {
install(subargs)
} else if (command === "update") {
update(subargs)
} else if (command === "uninstall") {
uninstall(subargs)
}
All tests run on a 2.4GHz Intel Core i7 CPU with 16 GB memory.
npm i -C bench && node bench
getopts × 1,315,998 ops/sec minimist × 260,817 ops/sec yargs × 33,520 ops/sec mri × 386,495 ops/sec
FAQs
Parse CLI arguments.
The npm package getopts receives a total of 1,470,645 weekly downloads. As such, getopts popularity was classified as popular.
We found that getopts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.