opt
Why another command line argument processor?
There are three reasons I created opt.js
- I wanted to simplify the boilerplate I used with JSON based configuration files including handling a set of search paths (e.g. look sequentially in a list of paths for a configuration file)
- I wanted a very simple command line option parser that included automatically generate a help page (I'm forgetful about updating docs outside of my code)
- I wanted a simple http route library that generate docs like I used for the command line
That was my itch. There are many fine existing options parsing libraries in Node but they didn't quite scratch the itch I had.
Overview
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 = opt = ;
var options = opt = options;
It is available from github at https://github.com/rsdoiel/opt and can be installed using npm
npm install opt
Examples Code
The following examples build by topics
- configuration processing
- command line option processing
- a RESTful Hello World API
Config Example
This is the synchronous version.
/*jslint node: true */ "use strict"; var path = opt = ; var config = name: "fred" email: "fred@example.com" search_paths = "config-example-1.conf" path "/usr/local/etc/config-example.conf" "/usr/etc/config-example.conf" "/etc/config-example.conf" ; console; opt; opt;
Adding Option processing
Display a help message with -h and --help on the command line.
/*jslint node: true */ "use strict"; var path = opt = ; var config = name: "fred" email: "fred@example.com" search_paths = "config-example-1.conf" path "/usr/local/etc/config-example.conf" "/usr/etc/config-example.conf" "/etc/config-example.conf" ; console; opt; opt; opt;
A simple web server API
// Importing some modules var util = http = path = // import and create the opt object opt = ; // Define your configuration defaults and load your local configuration // file or it. opt; // When your configuration is "ready" parse the command lines // and setup your RESTful hello world web service opt;
Simple webserve example
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);