Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
get-options
Advanced tools
Readme
The JavaScript equivalent of getopts
. No frills, no bullshit; nothing but cold, hard option extraction.
Use this module if you
$ program --log-path /var/log/stuff.txt generate all-files --verbose
$ program generate all-files
let result = {
options: {
logPath: "/var/log/stuff.txt",
verbose: true
},
argv: [
"generate",
"all-files"
]
};
That's seriously all.
getOpts(process.argv, {
"-v, --verbose": "",
"-l, --log-level": "[level]",
"-p, --log-path": "<path>",
"-s, --set-config": "<name> <value>",
"-f, --files": "<search-path> <variadic-file-list...>"
});
Left column:
Short and long forms of each defined option, separated by commas.
Right column:
Arguments each option takes, if any.
Note: There's no requirement to enclose each parameter's name with < > [ ] ( )
. These characters are just permitted for readability, and are ignored by the function when it runs. They're allowed because some authors might find them easier on the eyes than simple space-separation.
The value that's assigned to each corresponding .options
property is either:
true
if the option doesn't take any parameters (e.g., "--verbose": ""
)"--log-path": "path"
)"-s, --set-config": "name value"
)Given the earlier example, the following line...
program --files /search/path 1.jpg 2.txt 3.gif --log-path /path/to/ subcommand param --verbose
... would yield:
let result = {
argv: ["subcommand", "param"],
options: {
files: ["/search/path", "1.jpg", "2.txt", "3.gif"],
logPath: "/path/to",
verbose: true
}
};
I'm sure you get it by now.
Yeah, that's it. You want fancy subcommands? Just leverage the .argv
property of the returned object:
let subcommands = {
generate: function(whichFiles){
console.log("Let's generate... " + whichFiles);
}
};
subcommands[ result.argv[0] ].apply(null, result.argv.slice(1));
/** -> Outputs "Let's generate... all-files" */
This would work too, if you're an eval
kinda guy:
function generate(whichFiles){ /* ... */ }
eval(result.argv[0]).apply(null, result.argv.slice(1));
Obviously you'd be checking if the function existed and all that jazz. But that's up to you.
process.argv
, you can do so by assignment:process.argv = result.argv;
This is by design. It's not reasonable to assume developers will expect the contents of the array to be automatically shifted as options are being plucked from it.
process.argv
contain the paths of the Node executable and the currently-running script.
These have been omitted from the examples documented here (perhaps misleadingly, but done so for brevity's sake).
In production, you'd probably want to pass process.argv.slice(2)
to getOpts
or something.Yeah, there's billions of CLI-handling modules on NPM. Among the most well-known and popular are Commander.JS and yargs. Since I'm a control freak, though, I prefer doing things my way. So I developed a solution that'd permit more idiosyncratic approaches than those offered by "mainstream" option modules.
FAQs
JavaScript's answer to getopts. Simple, obvious, and direct.
The npm package get-options receives a total of 499 weekly downloads. As such, get-options popularity was classified as not popular.
We found that get-options 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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.