
Company News
AWS Security Hub Adds Socket for Supply Chain Security
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.
A configuration, cmd line options and RESTful web processor module for NodeJS. A simple help text is also automatically generated based if the command line options or RESTful paths you define so you don't have to remember to do that later.
optThere are three reasons I created opt.js
That was my itch. There are many fine existing options parsing libraries in Node but they didn't quite scratch the itch I had.
opt is a toolkit for building command line programs and RESTful web services. It uses a common idiom for setting up and processing JSON based configuration files, command line options parsing and defining a RESTful API calls.
Use the module by invoking opt's constructor Opt() or opt.create() method..
var options = require("opt"),
opt = new options.Opt();
var options = require("opt"),
opt = options.create();
It is available from github at https://github.com/rsdoiel/opt and can be installed using npm
npm install opt
The following examples build by topics
This is the synchronous version.
/*jslint node: true */
"use strict";
var path = require("path"),
opt = require("opt").create();
var config = { name: "fred", email: "fred@example.com" },
search_paths = [ "config-example-1.conf",
path.join(process.env.HOME, ".config-examplerc"),
"/usr/local/etc/config-example.conf",
"/usr/etc/config-example.conf",
"/etc/config-example.conf" ];
console.log("Unprocessed config:", config);
opt.config(config, search_paths);
opt.on("ready", function (config) {
// config should now hold the merge configuration
// from default_config and the first configuration file
// found in the search path list.
console.log("Processed config: ", config);
});
Display a help message with -h and --help on the command line.
/*jslint node: true */
"use strict";
var path = require("path"),
opt = require("opt").create();
var config = { name: "fred", email: "fred@example.com" },
search_paths = [ "config-example-1.conf",
path.join(process.env.HOME, ".config-examplerc"),
"/usr/local/etc/config-example.conf",
"/usr/etc/config-example.conf",
"/etc/config-example.conf" ];
console.log("Unprocessed config:", config);
opt.config(config, search_paths);
opt.optionHelp("USAGE node " + path.basename(process.argv[1]),
"SYNOPSIS: Demonstrate how opt works to parse command line options.\n\n\t\t node " + path.basename(process.argv[1]) + " --help",
"OPTIONS:",
" copyright (c) 2012 all rights reserved\n" +
" Released under New the BSD License.\n" +
" See: http://opensource.org/licenses/bsd-license.php\n");
opt.on("ready", function (config) {
opt.option(["-n", "--name"], function (param) {
if (param.trim()) {
config.name = param.trim();
}
}, "Set the name parameter");
opt.option(["-e", "--email"], function (param) {
if (param.trim()) {
config.email = param.trim();
}
}, "Set the email parameter");
opt.option(["-g", "--generate"], function (param) {
if (param.trim()) {
fs.writeFile(param.trim(), JSON.stringify(config));
} else {
console.log(JSON.stringify(config));
}
process.exit(0);
}, "Generate a configuration file");
opt.option(["-h", "--help"], function () {
opt.usage();
}, "This help document.");
opt.optionWith(process.argv);
// config should now hold the merge configuration
// from default_config and the first configuration file
// found in the search path list.
console.log("Processed config: ", config);
});
// Importing some modules
var util = require("util"),
http = require("http"),
path = require("path"),
// import and create the opt object
opt = require("opt").create();
// Define your configuration defaults and load your local configuration
// file or it.
opt.config({ host: "localhost", port: 8080, name: "John Doe"},
[ "/etc/helloworld.json", path.join(process.env.HOME, "etc/helloworld.json") ]);
// When your configuration is "ready" parse the command lines
// and setup your RESTful hello world web service
opt.on("ready", function (config) {
// Setup how your help page will look
opt.optionHelp("USAGE node " + path.basename(process.argv[1]),
"SYNOPSIS:\n\tThis is a simple hello world web service.",
"OPTIONS:",
"this is an opt demo");
// Define your command line options
opt.option(["-H", "--host"], function (hostname) {
config.host = hostname.trim();
});
opt.option(["-p", "--port"], function (portname) {
config.port = Number(portname.trim());
});
opt.option(["-n", "--name"], function (name) {
config.name = name.trim();
});
opt.option(["-h", "--help"], function () {
opt.usage();
});
// Define you restful service
var helloworld = function (req, res, matching, rule_no) {
res.writeHead(200, {"content-type": "text/plain"});
res.end("Hello " + config.name + ".\nThis is what I found: " + util.inspect(matching) + "\nRule No.:" + rule_no);
};
opt.rest("get", new RegExp("^$|^/$|^/index.html|^/helloworld.html"), helloworld);
var status404 = function (req, res) {
res.writeHead(404, {"content-type": "text/plain"});
res.end("File not found. " + req.url);
};
opt.rest("get", new RegExp("^/*"), status404);
// Process your command line args.
opt.optionWith(process.argv);
// Process your restful requets
console.log("Configuration:", config);
http.createServer(function (req, res) {
console.log("request:", req.url);
opt.restWith(req, res);
}).listen(config.port, config.host);
console.log("Web server listening on " + config.host + ":" + config.port);
});
This example sets up a simple hello web server.
var fs = require("fs"),
path = require("path"),
http = require("http"),
url = require("url"),
opt = require("opt").create(),
TBone = require("tbone"),
H = new TBone.HTML(),
config_filename = false,
config = {
port: 8123,
host: "localhost"
};
opt.optionHelp(
"USAGE node " + path.basename(process.argv[1]),
"SYNOPSIS: Demonstrate how opt works to parse command line options.\n" +
"\n\t\t node " + path.basename(process.argv[1]) + " --help",
"OPTIONS:",
"ACME Gelatin Company"
);
opt.consume(true);
opt.option(["-p", "--port"], function (arg) {
try {
config.port = Number(arg);
} catch (err0) {
console.error(err0);
process.exit(1);
}
opt.consume(arg);
}, "Set the port to listen on.");
opt.option(["-c", "--config"], function (arg) {
config_filename = arg;
option.consume(arg);
}, "Set the configuration file to use.");
opt.option(["-h", "--help"], function (arg) {
opt.usage();
}, "This help page.");
opt.optionWith(process.argv);
if (config_filename) {
config = JSON.parse(fs.readFileSync(config_filename).toString());
}
http.createServer(function (request, response) {
console.log("request:", request.url);
response.writeHead(200, "text/html");
response.end(H.html(
H.head(
H.title("Hello World")
),
H.body(
H.h1("Hello World")
)
).attr({lang: "en"}));
}).listen(config.port);
FAQs
A configuration, cmd line options and RESTful web processor module for NodeJS. A simple help text is also automatically generated based if the command line options or RESTful paths you define so you don't have to remember to do that later.
The npm package opt receives a total of 97 weekly downloads. As such, opt popularity was classified as not popular.
We found that opt 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.

Company News
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.

Research
/Security News
Popular npm packages keyv and cacheable compromised.

Security News
A misconfiguration gave three Anthropic models internet access, and one, believing it was in a simulation, shipped a credential-stealing package to PyPI.