{"_id":"opt","_rev":"87-1c7a9046c28789f7d83a205fff0c03cc","name":"opt","description":"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.","dist-tags":{"latest":"0.1.14"},"versions":{"0.0.9":{"name":"opt","version":"0.0.9","description":"A configuration, cmd line options and RESTful web processor module for NodeJS.","main":"./opt.js","repository":{"type":"git","url":"https://github.com/rsdoiel/opt.git"},"author":{"name":"R. S. Doiel","email":"rsdoiel@gmail.com","url":"https://github.com/rsdoiel"},"maintainers":[{"name":"rsdoiel","email":"rsdoiel@gmail.com"}],"engines":{"node":">= 0.7.12","npm":">= 1.1.30"},"dependencies":{"github-flavored-markdown":"1.0.1"},"scripts":{"test":"node opt_test.js"},"bugs":{"url":"http://github.com/rsdoiel/opt/issues"},"readme":"opt\n===\nrevision 0.0.9\n--------------\n\n# Overview\n\nopt is a toolkit for building either command line programs or RESTful\nweb services. It uses a common pattern for setting up and processing\nJSON based configuration files, command line options and RESTful API\ncalls.\n\nUsing the module is usually invoked by\n\n```javascript\nvar opt = require(\"opt\").create();\n```\nIt is available from github at https://github.com/rsdoiel/opt and\ncan be installed using npm\n\n```sh\nnpm install opt\n```\n\n\n# Examples Code\n\nThe following examples build by topics\n\n* configuration processing\n* command line option processing\n* a RESTful Hello World API\n\n## Config Example\n\nThis is the synchronous version.\n\n```javascript\n/*jslint node: true */\n\"use strict\";\n\nvar path = require(\"path\"),\n\topt = require(\"../opt\").create();\n\nvar config = { name: \"fred\", email: \"fred@example.com\" },\n\tsearch_paths = [ \"config-example-1.conf\",\n\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\"/etc/config-example.conf\" ];\n\nconsole.log(\"Unprocessed config:\", config);\nopt.config(config, search_paths);\n\nopt.on(\"ready\", function (config) {\n\t// config should now hold the merge configuration\n\t// from default_config and the first configuration file \n\t// found in the search path list.\n\tconsole.log(\"Processed config: \", config);\n});\n```\n\n## Adding Option processing\n\nDisplay a help message with -h and --help on the command line.\n\n```javascript\n/*jslint node: true */\n\"use strict\";\n\nvar path = require(\"path\"),\n\topt = require(\"../opt\").create();\n\nvar config = { name: \"fred\", email: \"fred@example.com\" },\n\tsearch_paths = [ \"config-example-1.conf\",\n\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\"/etc/config-example.conf\" ];\n\nconsole.log(\"Unprocessed config:\", config);\nopt.config(config, search_paths);\n\nopt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\"OPTIONS:\",\n\t\" copyright (c) 2012 all rights reserved\\n\" +\n\t\" Released under New the BSD License.\\n\" +\n\t\" See: http://opensource.org/licenses/bsd-license.php\\n\");\n\n\nopt.on(\"ready\", function (config) {\n    opt.option([\"-n\", \"--name\"], function (param) {\n        if (param.trim()) {\n            config.name = param.trim();\n        }\n    }, \"Set the name parameter\");\n\n    opt.option([\"-e\", \"--email\"], function (param) {\n        if (param.trim()) {\n            config.email = param.trim();\n        }\n    }, \"Set the email parameter\");\n    \n    opt.option([\"-g\", \"--generate\"], function (param) {\n        if (param.trim()) {\n            fs.writeFile(param.trim(), JSON.stringify(config));\n        } else {\n            console.log(JSON.stringify(config));\n        }\n        process.exit(0);\n    }, \"Generate a configuration file\");\n    \n    opt.option([\"-h\", \"--help\"], function () {\n        opt.usage();\n    }, \"This help document.\");\n\n    opt.optionWith(process.argv[1]);\n\n    // config should now hold the merge configuration\n    // from default_config and the first configuration file \n    // found in the search path list.\n    console.log(\"Processed config: \", config);\n});\n```\n\n## A simple web server API\n\n```javascript\n// Importing some modules\nvar util = require(\"util\"),\n    http = require(\"http\"),\n    path = require(\"path\"),\n    // import and create the opt object\n    opt = require(\"../opt\").create();\n\n// Define your configuration defaults and load your local configuration\n// file or it.\nopt.config({ host: \"localhost\", port: 8080, name: \"John Doe\"},\n    [ \"/etc/helloworld.json\", path.join(process.env.HOME, \"etc/helloworld.json\") ]);\n\n// When your configuration is \"ready\" parse the command lines\n// and setup your RESTful hello world web service\nopt.on(\"ready\", function (config) {\n    // Setup how your help page will look\n    opt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n        \"SYNOPSIS:\\n\\tThis is a simple hello world web service.\",\n        \"OPTIONS:\",\n        \"this is an opt demo\");\n\n    // Define your command line options\n    opt.option([\"-H\", \"--host\"], function (hostname) {\n        config.host = hostname.trim();\n    });\n    opt.option([\"-p\", \"--port\"], function (portname) {\n        config.port = Number(portname.trim());\n    });\n    opt.option([\"-n\", \"--name\"], function (name) {\n        config.name = name.trim();\n    });\n    opt.option([\"-h\", \"--help\"], function () {\n        opt.usage();\n    });\n\n    // Define you restful service\n    var helloworld = function (req, res, matching, rule_no) {\n        res.writeHead(200, {\"content-type\": \"text/plain\"});\n        res.end(\"Hello \" + config.name + \".\\nThis is what I found: \" + util.inspect(matching) + \"\\nRule No.:\" + rule_no);\n    };\n    opt.rest(\"get\", new RegExp(\"^$|^/$|^/index.html|^/helloworld.html\"), helloworld);\n\n    var status404 = function (req, res) {\n        res.writeHead(404, {\"content-type\": \"text/plain\"});\n        res.end(\"File not found. \" + req.url);\n    };\n    opt.rest(\"get\", new RegExp(\"^/*\"), status404);\n\n    // Process your command line args.\n    opt.optionWith(process.argv);\n\n    // Process your restful requets\n    console.log(\"Configuration:\", config);\n    http.createServer(function (req, res) {\n        console.log(\"request:\", req.url);\n        opt.restWith(req, res);\n    }).listen(config.port, config.host);\n    console.log(\"Web server listening on \" + config.host + \":\" + config.port);\n});\n```\n","_id":"opt@0.0.9","dist":{"shasum":"1e0a95009032e875dda56f71745ec86b95ef0cb0","tarball":"https://registry.npmjs.org/opt/-/opt-0.0.9.tgz","integrity":"sha512-aJjbn2mDMIwR4x3qWhYrcKo8KG9tfJCOVna5sY/S5dOR+EuT1vPSx+1Yd8CtAiqoiJiQ7zNGzkKCsteY0oXYQQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC7GimpLynba1TjnNRZZTabTtF/pul+SU23W00tEDL4HwIhAKkK02P1ArrKDd2m2/vMQLtD5XP4Qy/vjkxiSALRKmyV"}]},"directories":{}},"0.1.0":{"name":"opt","version":"0.1.0","description":"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.","main":"./opt.js","repository":{"type":"git","url":"https://github.com/rsdoiel/opt.git"},"author":{"name":"R. S. Doiel","email":"rsdoiel@gmail.com","url":"https://github.com/rsdoiel"},"maintainers":[{"name":"rsdoiel","email":"rsdoiel@gmail.com"}],"engines":{"node":">= 0.7.12","npm":">= 1.1.30"},"dependencies":{"harness":">= 0.0.2"},"scripts":{"test":"node tests/opt_test.js"},"bugs":{"url":"http://github.com/rsdoiel/opt/issues"},"readme":"[![build status](https://secure.travis-ci.org/rsdoiel/opt.png)](http://travis-ci.org/rsdoiel/opt)\nopt\n===\nrevision 0.0.10\n---------------\n\n### Why another command line argument processor?\n\nThere are three reasons I created opt.js\n\n* 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\n* I wanted a very simple command line option parser that included automatically generating a help page (I'm forgetful about updating docs outside of \n* I wanted a simple http route process that generate docs like I used for the command line\n\nThat was my itch.  There are many file example of options parsing libraries in Node but they didn't quite scratch the itch I had.\n\n\n# Overview\n\nopt is a toolkit for building either command line programs and RESTful\nweb services. It uses a common pattern for setting up and processing\nJSON based configuration files, command line options and RESTful API\ncalls.\n\nUsing the module is usually invoked by\n\n```javascript\n\tvar opt = require(\"opt\").create();\n```\nIt is available from github at https://github.com/rsdoiel/opt and\ncan be installed using npm\n\n```shell\n\tnpm install opt\n```\n\n\n# Examples Code\n\nThe following examples build by topics\n\n* configuration processing\n* command line option processing\n* a RESTful Hello World API\n\n## Config Example\n\nThis is the synchronous version.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\n\tvar path = require(\"path\"),\n\t    opt = require(\"../opt\").create();\n\n\t    var config = { name: \"fred\", email: \"fred@example.com\" },\n\t    \tsearch_paths = [ \"config-example-1.conf\",\n\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\"/etc/config-example.conf\" ];\n\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\n\topt.on(\"ready\", function (config) {\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## Adding Option processing\n\nDisplay a help message with -h and --help on the command line.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\t\n\tvar path = require(\"path\"),\n\t\topt = require(\"../opt\").create();\n\t\n\tvar config = { name: \"fred\", email: \"fred@example.com\" },\n\t\tsearch_paths = [ \"config-example-1.conf\",\n\t\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\t\"/etc/config-example.conf\" ];\n\t\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\t\n\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\t\"OPTIONS:\",\n\t\t\" copyright (c) 2012 all rights reserved\\n\" +\n\t\t\" Released under New the BSD License.\\n\" +\n\t\t\" See: http://opensource.org/licenses/bsd-license.php\\n\");\n\t\n\t\n\topt.on(\"ready\", function (config) {\n\t\topt.option([\"-n\", \"--name\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.name = param.trim();\n\t\t\t}\n\t\t}, \"Set the name parameter\");\n\t\n\t\topt.option([\"-e\", \"--email\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.email = param.trim();\n\t\t\t}\n\t\t}, \"Set the email parameter\");\n\t\t\n\t\topt.option([\"-g\", \"--generate\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tfs.writeFile(param.trim(), JSON.stringify(config));\n\t\t\t} else {\n\t\t\t\tconsole.log(JSON.stringify(config));\n\t\t\t}\n\t\t\tprocess.exit(0);\n\t\t}, \"Generate a configuration file\");\n\t\t\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t}, \"This help document.\");\n\t\n\t\topt.optionWith(process.argv[1]);\n\t\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## A simple web server API\n\n```javascript\n\t// Importing some modules\n\tvar util = require(\"util\"),\n\t\thttp = require(\"http\"),\n\t\tpath = require(\"path\"),\n\t\t// import and create the opt object\n\t\topt = require(\"../opt\").create();\n\t\n\t// Define your configuration defaults and load your local configuration\n\t// file or it.\n\topt.config({ host: \"localhost\", port: 8080, name: \"John Doe\"},\n\t\t[ \"/etc/helloworld.json\", path.join(process.env.HOME, \"etc/helloworld.json\") ]);\n\t\n\t// When your configuration is \"ready\" parse the command lines\n\t// and setup your RESTful hello world web service\n\topt.on(\"ready\", function (config) {\n\t\t// Setup how your help page will look\n\t\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\t\"SYNOPSIS:\\n\\tThis is a simple hello world web service.\",\n\t\t\t\"OPTIONS:\",\n\t\t\t\"this is an opt demo\");\n\t\n\t\t// Define your command line options\n\t\topt.option([\"-H\", \"--host\"], function (hostname) {\n\t\t\tconfig.host = hostname.trim();\n\t\t});\n\t\topt.option([\"-p\", \"--port\"], function (portname) {\n\t\t\tconfig.port = Number(portname.trim());\n\t\t});\n\t\topt.option([\"-n\", \"--name\"], function (name) {\n\t\t\tconfig.name = name.trim();\n\t\t});\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t});\n\t\n\t\t// Define you restful service\n\t\tvar helloworld = function (req, res, matching, rule_no) {\n\t\t\tres.writeHead(200, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"Hello \" + config.name + \".\\nThis is what I found: \" + util.inspect(matching) + \"\\nRule No.:\" + rule_no);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^$|^/$|^/index.html|^/helloworld.html\"), helloworld);\n\t\n\t\tvar status404 = function (req, res) {\n\t\t\tres.writeHead(404, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"File not found. \" + req.url);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^/*\"), status404);\n\t\n\t\t// Process your command line args.\n\t\topt.optionWith(process.argv);\n\t\n\t\t// Process your restful requets\n\t\tconsole.log(\"Configuration:\", config);\n\t\thttp.createServer(function (req, res) {\n\t\t\tconsole.log(\"request:\", req.url);\n\t\t\topt.restWith(req, res);\n\t\t}).listen(config.port, config.host);\n\t\tconsole.log(\"Web server listening on \" + config.host + \":\" + config.port);\n\t});\n```\n","_id":"opt@0.1.0","dist":{"shasum":"c651465c81ebbbc96072f628834fcc9fe28fc1f7","tarball":"https://registry.npmjs.org/opt/-/opt-0.1.0.tgz","integrity":"sha512-6ywH7l8UKF4lSoOxwT9ZRWyvNSpt1zAlqMHz/jrQu2cCp5nJbt1zYnpQBwX1NKa7vH6bor2Ebl4E89AogJbLOg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIELZ8OqA0E1LmG3yu8C280k32diPyrdCtaUpnNdvJxIpAiEA3XOe5aA/mXZq0mFRXOdlqOasCjqkF3hQfsgV1nvN4CE="}]},"_npmVersion":"1.1.62","_npmUser":{"name":"rsdoiel","email":"rsdoiel@gmail.com"}},"0.1.1":{"name":"opt","version":"0.1.1","description":"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.","main":"./opt.js","repository":{"type":"git","url":"https://github.com/rsdoiel/opt.git"},"author":{"name":"R. S. Doiel","email":"rsdoiel@gmail.com","url":"https://github.com/rsdoiel"},"maintainers":[{"name":"rsdoiel","email":"rsdoiel@gmail.com"}],"engines":{"node":">= 0.7.12","npm":">= 1.1.30"},"dependencies":{"harness":">= 0.0.2"},"scripts":{"test":"node tests/opt_test.js"},"bugs":{"url":"http://github.com/rsdoiel/opt/issues"},"readme":"[![build status](https://secure.travis-ci.org/rsdoiel/opt.png)](http://travis-ci.org/rsdoiel/opt)\nopt\n===\nrevision 0.0.10\n---------------\n\n### Why another command line argument processor?\n\nThere are three reasons I created opt.js\n\n* 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\n* I wanted a very simple command line option parser that included automatically generating a help page (I'm forgetful about updating docs outside of \n* I wanted a simple http route process that generate docs like I used for the command line\n\nThat was my itch.  There are many file example of options parsing libraries in Node but they didn't quite scratch the itch I had.\n\n\n# Overview\n\nopt is a toolkit for building either command line programs and RESTful\nweb services. It uses a common pattern for setting up and processing\nJSON based configuration files, command line options and RESTful API\ncalls.\n\nUsing the module is usually invoked by\n\n```javascript\n\tvar opt = require(\"opt\").create();\n```\nIt is available from github at https://github.com/rsdoiel/opt and\ncan be installed using npm\n\n```shell\n\tnpm install opt\n```\n\n\n# Examples Code\n\nThe following examples build by topics\n\n* configuration processing\n* command line option processing\n* a RESTful Hello World API\n\n## Config Example\n\nThis is the synchronous version.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\n\tvar path = require(\"path\"),\n\t    opt = require(\"../opt\").create();\n\n\t    var config = { name: \"fred\", email: \"fred@example.com\" },\n\t    \tsearch_paths = [ \"config-example-1.conf\",\n\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\"/etc/config-example.conf\" ];\n\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\n\topt.on(\"ready\", function (config) {\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## Adding Option processing\n\nDisplay a help message with -h and --help on the command line.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\t\n\tvar path = require(\"path\"),\n\t\topt = require(\"../opt\").create();\n\t\n\tvar config = { name: \"fred\", email: \"fred@example.com\" },\n\t\tsearch_paths = [ \"config-example-1.conf\",\n\t\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\t\"/etc/config-example.conf\" ];\n\t\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\t\n\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\t\"OPTIONS:\",\n\t\t\" copyright (c) 2012 all rights reserved\\n\" +\n\t\t\" Released under New the BSD License.\\n\" +\n\t\t\" See: http://opensource.org/licenses/bsd-license.php\\n\");\n\t\n\t\n\topt.on(\"ready\", function (config) {\n\t\topt.option([\"-n\", \"--name\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.name = param.trim();\n\t\t\t}\n\t\t}, \"Set the name parameter\");\n\t\n\t\topt.option([\"-e\", \"--email\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.email = param.trim();\n\t\t\t}\n\t\t}, \"Set the email parameter\");\n\t\t\n\t\topt.option([\"-g\", \"--generate\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tfs.writeFile(param.trim(), JSON.stringify(config));\n\t\t\t} else {\n\t\t\t\tconsole.log(JSON.stringify(config));\n\t\t\t}\n\t\t\tprocess.exit(0);\n\t\t}, \"Generate a configuration file\");\n\t\t\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t}, \"This help document.\");\n\t\n\t\topt.optionWith(process.argv[1]);\n\t\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## A simple web server API\n\n```javascript\n\t// Importing some modules\n\tvar util = require(\"util\"),\n\t\thttp = require(\"http\"),\n\t\tpath = require(\"path\"),\n\t\t// import and create the opt object\n\t\topt = require(\"../opt\").create();\n\t\n\t// Define your configuration defaults and load your local configuration\n\t// file or it.\n\topt.config({ host: \"localhost\", port: 8080, name: \"John Doe\"},\n\t\t[ \"/etc/helloworld.json\", path.join(process.env.HOME, \"etc/helloworld.json\") ]);\n\t\n\t// When your configuration is \"ready\" parse the command lines\n\t// and setup your RESTful hello world web service\n\topt.on(\"ready\", function (config) {\n\t\t// Setup how your help page will look\n\t\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\t\"SYNOPSIS:\\n\\tThis is a simple hello world web service.\",\n\t\t\t\"OPTIONS:\",\n\t\t\t\"this is an opt demo\");\n\t\n\t\t// Define your command line options\n\t\topt.option([\"-H\", \"--host\"], function (hostname) {\n\t\t\tconfig.host = hostname.trim();\n\t\t});\n\t\topt.option([\"-p\", \"--port\"], function (portname) {\n\t\t\tconfig.port = Number(portname.trim());\n\t\t});\n\t\topt.option([\"-n\", \"--name\"], function (name) {\n\t\t\tconfig.name = name.trim();\n\t\t});\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t});\n\t\n\t\t// Define you restful service\n\t\tvar helloworld = function (req, res, matching, rule_no) {\n\t\t\tres.writeHead(200, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"Hello \" + config.name + \".\\nThis is what I found: \" + util.inspect(matching) + \"\\nRule No.:\" + rule_no);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^$|^/$|^/index.html|^/helloworld.html\"), helloworld);\n\t\n\t\tvar status404 = function (req, res) {\n\t\t\tres.writeHead(404, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"File not found. \" + req.url);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^/*\"), status404);\n\t\n\t\t// Process your command line args.\n\t\topt.optionWith(process.argv);\n\t\n\t\t// Process your restful requets\n\t\tconsole.log(\"Configuration:\", config);\n\t\thttp.createServer(function (req, res) {\n\t\t\tconsole.log(\"request:\", req.url);\n\t\t\topt.restWith(req, res);\n\t\t}).listen(config.port, config.host);\n\t\tconsole.log(\"Web server listening on \" + config.host + \":\" + config.port);\n\t});\n```\n","_id":"opt@0.1.1","dist":{"shasum":"20b857a9ce194d7e2c59043ae485c44fac82e322","tarball":"https://registry.npmjs.org/opt/-/opt-0.1.1.tgz","integrity":"sha512-AG6Cd2HA+qpyYtmi/YXsu+h93PX4DwHPZt2kw8ST1Q5ychi0dGGGpN1+HmBiUWJq9g6jN1SfO66FwdhcB2gpww==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIE3rFEo97uu+LoMCAzX+HC40niyhYEeQfb6IOOvyrfmCAiBvxoXjgF69x488mB4Ky3Hmza9l/gEhLVNTjBhGF49UOA=="}]},"_npmVersion":"1.1.62","_npmUser":{"name":"rsdoiel","email":"rsdoiel@gmail.com"}},"0.1.11":{"name":"opt","version":"0.1.11","description":"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.","main":"./opt.js","repository":{"type":"git","url":"https://github.com/rsdoiel/opt.git"},"author":{"name":"R. S. Doiel","email":"rsdoiel@gmail.com","url":"https://github.com/rsdoiel"},"maintainers":[{"name":"rsdoiel","email":"rsdoiel@gmail.com"}],"engines":{"node":">= 0.7.12","npm":">= 1.1.30"},"dependencies":{"harness":">= 0.0.2"},"scripts":{"test":"node tests/opt_test.js"},"bugs":{"url":"http://github.com/rsdoiel/opt/issues"},"readme":"[![build status](https://secure.travis-ci.org/rsdoiel/opt.png)](http://travis-ci.org/rsdoiel/opt)\nopt\n===\n\n\n### Why another command line argument processor?\n\nThere are three reasons I created opt.js\n\n* 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)\n* 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)\n* I wanted a simple http route library that generate docs like I used for the command line\n\nThat was my itch.  There are many fine existing options parsing libraries in Node but they didn't quite scratch the itch I had.\n\n\n# Overview\n\nopt is a toolkit for building command line programs and RESTful\nweb services. It uses a common idiom for setting up and processing\nJSON based configuration files, command line options parsing and \ndefining a RESTful API calls.\n\nUse the module by invoking opt's constructor Opt() or\nopt.create() method..\n\n\n```javascript\n\tvar options = require(\"opt\"),\n\t    opt = new options.Opt();\n```\n\n```javascript\n\tvar options = require(\"opt\"),\n\t\topt = options.create();\n```\n\n\nIt is available from github at https://github.com/rsdoiel/opt and\ncan be installed using npm\n\n```shell\n\tnpm install opt\n```\n\n\n# Examples Code\n\nThe following examples build by topics\n\n* configuration processing\n* command line option processing\n* a RESTful Hello World API\n\n## Config Example\n\nThis is the synchronous version.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\n\tvar path = require(\"path\"),\n\t    opt = require(\"opt\").create();\n\n\t    var config = { name: \"fred\", email: \"fred@example.com\" },\n\t    \tsearch_paths = [ \"config-example-1.conf\",\n\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\"/etc/config-example.conf\" ];\n\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\n\topt.on(\"ready\", function (config) {\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## Adding Option processing\n\nDisplay a help message with -h and --help on the command line.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\t\n\tvar path = require(\"path\"),\n\t\topt = require(\"opt\").create();\n\t\n\tvar config = { name: \"fred\", email: \"fred@example.com\" },\n\t\tsearch_paths = [ \"config-example-1.conf\",\n\t\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\t\"/etc/config-example.conf\" ];\n\t\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\t\n\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\t\"OPTIONS:\",\n\t\t\" copyright (c) 2012 all rights reserved\\n\" +\n\t\t\" Released under New the BSD License.\\n\" +\n\t\t\" See: http://opensource.org/licenses/bsd-license.php\\n\");\n\t\n\t\n\topt.on(\"ready\", function (config) {\n\t\topt.option([\"-n\", \"--name\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.name = param.trim();\n\t\t\t}\n\t\t}, \"Set the name parameter\");\n\t\n\t\topt.option([\"-e\", \"--email\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.email = param.trim();\n\t\t\t}\n\t\t}, \"Set the email parameter\");\n\t\t\n\t\topt.option([\"-g\", \"--generate\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tfs.writeFile(param.trim(), JSON.stringify(config));\n\t\t\t} else {\n\t\t\t\tconsole.log(JSON.stringify(config));\n\t\t\t}\n\t\t\tprocess.exit(0);\n\t\t}, \"Generate a configuration file\");\n\t\t\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t}, \"This help document.\");\n\t\n\t\topt.optionWith(process.argv);\n\t\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## A simple web server API\n\n```javascript\n\t// Importing some modules\n\tvar util = require(\"util\"),\n\t\thttp = require(\"http\"),\n\t\tpath = require(\"path\"),\n\t\t// import and create the opt object\n\t\topt = require(\"opt\").create();\n\t\n\t// Define your configuration defaults and load your local configuration\n\t// file or it.\n\topt.config({ host: \"localhost\", port: 8080, name: \"John Doe\"},\n\t\t[ \"/etc/helloworld.json\", path.join(process.env.HOME, \"etc/helloworld.json\") ]);\n\t\n\t// When your configuration is \"ready\" parse the command lines\n\t// and setup your RESTful hello world web service\n\topt.on(\"ready\", function (config) {\n\t\t// Setup how your help page will look\n\t\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\t\"SYNOPSIS:\\n\\tThis is a simple hello world web service.\",\n\t\t\t\"OPTIONS:\",\n\t\t\t\"this is an opt demo\");\n\t\n\t\t// Define your command line options\n\t\topt.option([\"-H\", \"--host\"], function (hostname) {\n\t\t\tconfig.host = hostname.trim();\n\t\t});\n\t\topt.option([\"-p\", \"--port\"], function (portname) {\n\t\t\tconfig.port = Number(portname.trim());\n\t\t});\n\t\topt.option([\"-n\", \"--name\"], function (name) {\n\t\t\tconfig.name = name.trim();\n\t\t});\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t});\n\t\n\t\t// Define you restful service\n\t\tvar helloworld = function (req, res, matching, rule_no) {\n\t\t\tres.writeHead(200, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"Hello \" + config.name + \".\\nThis is what I found: \" + util.inspect(matching) + \"\\nRule No.:\" + rule_no);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^$|^/$|^/index.html|^/helloworld.html\"), helloworld);\n\t\n\t\tvar status404 = function (req, res) {\n\t\t\tres.writeHead(404, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"File not found. \" + req.url);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^/*\"), status404);\n\t\n\t\t// Process your command line args.\n\t\topt.optionWith(process.argv);\n\t\n\t\t// Process your restful requets\n\t\tconsole.log(\"Configuration:\", config);\n\t\thttp.createServer(function (req, res) {\n\t\t\tconsole.log(\"request:\", req.url);\n\t\t\topt.restWith(req, res);\n\t\t}).listen(config.port, config.host);\n\t\tconsole.log(\"Web server listening on \" + config.host + \":\" + config.port);\n\t});\n```\n","readmeFilename":"README.md","_id":"opt@0.1.11","dist":{"shasum":"df5e59643baac06db32b061a44438d574639ad39","tarball":"https://registry.npmjs.org/opt/-/opt-0.1.11.tgz","integrity":"sha512-u5Y+TpkpFHjf5ig7wSpUkNPZBtMDiM/eYADvAzA0Q+9/JUdFosJ5NAD3/5PVYmuwmR40TVqOc27PfoPCCLbBXg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG0Fe9O5j9EDA1zMlsED7ImwjtfKI3wjlw4eyIP9c+3hAiBnW/Abfefw/9rE9n8GV6XuNXJUCUpF/zu6fuswca0BPg=="}]},"_npmVersion":"1.1.65","_npmUser":{"name":"rsdoiel","email":"rsdoiel@gmail.com"}},"0.1.13":{"name":"opt","version":"0.1.13","description":"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.","main":"./opt.js","repository":{"type":"git","url":"https://github.com/rsdoiel/opt.git"},"author":{"name":"R. S. Doiel","email":"rsdoiel@gmail.com","url":"https://github.com/rsdoiel"},"maintainers":[{"name":"rsdoiel","email":"rsdoiel@gmail.com"}],"engines":{"node":">= 0.8.15","npm":">= 1.1.30"},"devDependencies":{"harness":">= 0.0.2"},"scripts":{"test":"node tests/opt_test.js"},"bugs":{"url":"http://github.com/rsdoiel/opt/issues"},"readme":"[![build status](https://secure.travis-ci.org/rsdoiel/opt.png)](http://travis-ci.org/rsdoiel/opt)\nopt\n===\n\n\n### Why another command line argument processor?\n\nThere are three reasons I created opt.js\n\n* 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)\n* 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)\n* I wanted a simple http route library that generate docs like I used for the command line\n\nThat was my itch.  There are many fine existing options parsing libraries in Node but they didn't quite scratch the itch I had.\n\n\n# Overview\n\nopt is a toolkit for building command line programs and RESTful\nweb services. It uses a common idiom for setting up and processing\nJSON based configuration files, command line options parsing and \ndefining a RESTful API calls.\n\nUse the module by invoking opt's constructor Opt() or\nopt.create() method..\n\n\n```javascript\n\tvar options = require(\"opt\"),\n\t    opt = new options.Opt();\n```\n\n```javascript\n\tvar options = require(\"opt\"),\n\t\topt = options.create();\n```\n\n\nIt is available from github at https://github.com/rsdoiel/opt and\ncan be installed using npm\n\n```shell\n\tnpm install opt\n```\n\n\n# Examples Code\n\nThe following examples build by topics\n\n* configuration processing\n* command line option processing\n* a RESTful Hello World API\n\n## Config Example\n\nThis is the synchronous version.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\n\tvar path = require(\"path\"),\n\t    opt = require(\"opt\").create();\n\n\t    var config = { name: \"fred\", email: \"fred@example.com\" },\n\t    \tsearch_paths = [ \"config-example-1.conf\",\n\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\"/etc/config-example.conf\" ];\n\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\n\topt.on(\"ready\", function (config) {\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## Adding Option processing\n\nDisplay a help message with -h and --help on the command line.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\t\n\tvar path = require(\"path\"),\n\t\topt = require(\"opt\").create();\n\t\n\tvar config = { name: \"fred\", email: \"fred@example.com\" },\n\t\tsearch_paths = [ \"config-example-1.conf\",\n\t\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\t\"/etc/config-example.conf\" ];\n\t\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\t\n\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\t\"OPTIONS:\",\n\t\t\" copyright (c) 2012 all rights reserved\\n\" +\n\t\t\" Released under New the BSD License.\\n\" +\n\t\t\" See: http://opensource.org/licenses/bsd-license.php\\n\");\n\t\n\t\n\topt.on(\"ready\", function (config) {\n\t\topt.option([\"-n\", \"--name\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.name = param.trim();\n\t\t\t}\n\t\t}, \"Set the name parameter\");\n\t\n\t\topt.option([\"-e\", \"--email\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.email = param.trim();\n\t\t\t}\n\t\t}, \"Set the email parameter\");\n\t\t\n\t\topt.option([\"-g\", \"--generate\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tfs.writeFile(param.trim(), JSON.stringify(config));\n\t\t\t} else {\n\t\t\t\tconsole.log(JSON.stringify(config));\n\t\t\t}\n\t\t\tprocess.exit(0);\n\t\t}, \"Generate a configuration file\");\n\t\t\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t}, \"This help document.\");\n\t\n\t\topt.optionWith(process.argv);\n\t\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## A simple web server API\n\n```javascript\n\t// Importing some modules\n\tvar util = require(\"util\"),\n\t\thttp = require(\"http\"),\n\t\tpath = require(\"path\"),\n\t\t// import and create the opt object\n\t\topt = require(\"opt\").create();\n\t\n\t// Define your configuration defaults and load your local configuration\n\t// file or it.\n\topt.config({ host: \"localhost\", port: 8080, name: \"John Doe\"},\n\t\t[ \"/etc/helloworld.json\", path.join(process.env.HOME, \"etc/helloworld.json\") ]);\n\t\n\t// When your configuration is \"ready\" parse the command lines\n\t// and setup your RESTful hello world web service\n\topt.on(\"ready\", function (config) {\n\t\t// Setup how your help page will look\n\t\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\t\"SYNOPSIS:\\n\\tThis is a simple hello world web service.\",\n\t\t\t\"OPTIONS:\",\n\t\t\t\"this is an opt demo\");\n\t\n\t\t// Define your command line options\n\t\topt.option([\"-H\", \"--host\"], function (hostname) {\n\t\t\tconfig.host = hostname.trim();\n\t\t});\n\t\topt.option([\"-p\", \"--port\"], function (portname) {\n\t\t\tconfig.port = Number(portname.trim());\n\t\t});\n\t\topt.option([\"-n\", \"--name\"], function (name) {\n\t\t\tconfig.name = name.trim();\n\t\t});\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t});\n\t\n\t\t// Define you restful service\n\t\tvar helloworld = function (req, res, matching, rule_no) {\n\t\t\tres.writeHead(200, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"Hello \" + config.name + \".\\nThis is what I found: \" + util.inspect(matching) + \"\\nRule No.:\" + rule_no);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^$|^/$|^/index.html|^/helloworld.html\"), helloworld);\n\t\n\t\tvar status404 = function (req, res) {\n\t\t\tres.writeHead(404, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"File not found. \" + req.url);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^/*\"), status404);\n\t\n\t\t// Process your command line args.\n\t\topt.optionWith(process.argv);\n\t\n\t\t// Process your restful requets\n\t\tconsole.log(\"Configuration:\", config);\n\t\thttp.createServer(function (req, res) {\n\t\t\tconsole.log(\"request:\", req.url);\n\t\t\topt.restWith(req, res);\n\t\t}).listen(config.port, config.host);\n\t\tconsole.log(\"Web server listening on \" + config.host + \":\" + config.port);\n\t});\n```\n\n\n# Simple webserve example\n\nThis example sets up a simple hello web server.\n\n```JavaScript\n\tvar fs = require(\"fs\"),\n\t\tpath = require(\"path\"),\n\t\thttp = require(\"http\"),\n\t\turl = require(\"url\"),\n\t\topt = require(\"opt\").create(),\n\t\tTBone = require(\"tbone\"),\n\t\tH = new TBone.HTML(),\n\t\tconfig_filename = false,\n\t\tconfig = {\n\t\t\tport: 8123,\n\t\t\thost: \"localhost\"\n\t\t};\n\t\n\topt.optionHelp(\n\t\t\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\" +\n\t\t\"\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\t\"OPTIONS:\",\n\t\t\"ACME Gelatin Company\"\n\t);\n\t\n\topt.consume(true);\n\topt.option([\"-p\", \"--port\"], function (arg) {\n\t\ttry {\n\t\t\tconfig.port = Number(arg);\n\t\t} catch (err0) {\n\t\t\tconsole.error(err0);\n\t\t\tprocess.exit(1);\n\t\t}\n\t\topt.consume(arg);\n\t}, \"Set the port to listen on.\");\n\t\n\topt.option([\"-c\", \"--config\"], function (arg) {\n\t\tconfig_filename = arg;\n\t\toption.consume(arg);\n\t}, \"Set the configuration file to use.\");\n\t\n\topt.option([\"-h\", \"--help\"], function (arg) {\n\t\topt.usage();\n\t}, \"This help page.\");\n\t\n\topt.optionWith(process.argv);\n\t\n\tif (config_filename) {\n\t\tconfig = JSON.parse(fs.readFileSync(config_filename).toString());\n\t}\n\t\n\thttp.createServer(function (request, response) {\n\t\tconsole.log(\"request:\", request.url);\n\t\tresponse.writeHead(200, \"text/html\");\n\t\tresponse.end(H.html(\n\t\t\tH.head(\n\t\t\t\tH.title(\"Hello World\")\n\t\t\t),\n\t\t\tH.body(\n\t\t\t\tH.h1(\"Hello World\")\n\t\t\t)\n\t\t).attr({lang: \"en\"}));\n\t}).listen(config.port);\n```\n","readmeFilename":"README.md","_id":"opt@0.1.13","dist":{"shasum":"7e6016d9db19cfafbd44e3b07f0f3fa0d06d5d77","tarball":"https://registry.npmjs.org/opt/-/opt-0.1.13.tgz","integrity":"sha512-WK06jFcR0SBd35K6w1OEI6ksatQH9y+iI+f9yuTYtX/lHwZDrIIhfQ6OpM6mp0xSvXONA/I0djWv36RDekSilQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCgDEyq13IYI5Q2hT+EAPe4DduV/14dZPZNB5fez/NITgIhAI4XsUS7DDQ/ESBRpIZRKBSrBiXB8bdpig52GpmSi0m9"}]},"_from":".","_npmVersion":"1.2.24","_npmUser":{"name":"rsdoiel","email":"rsdoiel@gmail.com"}},"0.1.14":{"name":"opt","version":"0.1.14","description":"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.","main":"./opt.js","repository":{"type":"git","url":"https://github.com/rsdoiel/opt.git"},"author":{"name":"R. S. Doiel","email":"rsdoiel@gmail.com","url":"https://github.com/rsdoiel"},"maintainers":[{"name":"rsdoiel","email":"rsdoiel@gmail.com"}],"engines":{"node":">= 0.8.15","npm":">= 1.1.30"},"devDependencies":{"yui":"3.10.x"},"scripts":{"test":"node tests/opt_test.js"},"bugs":{"url":"http://github.com/rsdoiel/opt/issues"},"readme":"[![build status](https://secure.travis-ci.org/rsdoiel/opt.png)](http://travis-ci.org/rsdoiel/opt)\nopt\n===\n\n\n### Why another command line argument processor?\n\nThere are three reasons I created opt.js\n\n* 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)\n* 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)\n* I wanted a simple http route library that generate docs like I used for the command line\n\nThat was my itch.  There are many fine existing options parsing libraries in Node but they didn't quite scratch the itch I had.\n\n\n# Overview\n\nopt is a toolkit for building command line programs and RESTful\nweb services. It uses a common idiom for setting up and processing\nJSON based configuration files, command line options parsing and \ndefining a RESTful API calls.\n\nUse the module by invoking opt's constructor Opt() or\nopt.create() method..\n\n\n```javascript\n\tvar options = require(\"opt\"),\n\t    opt = new options.Opt();\n```\n\n```javascript\n\tvar options = require(\"opt\"),\n\t\topt = options.create();\n```\n\n\nIt is available from github at https://github.com/rsdoiel/opt and\ncan be installed using npm\n\n```shell\n\tnpm install opt\n```\n\n\n# Examples Code\n\nThe following examples build by topics\n\n* configuration processing\n* command line option processing\n* a RESTful Hello World API\n\n## Config Example\n\nThis is the synchronous version.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\n\tvar path = require(\"path\"),\n\t    opt = require(\"opt\").create();\n\n\t    var config = { name: \"fred\", email: \"fred@example.com\" },\n\t    \tsearch_paths = [ \"config-example-1.conf\",\n\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\"/etc/config-example.conf\" ];\n\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\n\topt.on(\"ready\", function (config) {\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## Adding Option processing\n\nDisplay a help message with -h and --help on the command line.\n\n```javascript\n\t/*jslint node: true */\n\t\"use strict\";\n\t\n\tvar path = require(\"path\"),\n\t\topt = require(\"opt\").create();\n\t\n\tvar config = { name: \"fred\", email: \"fred@example.com\" },\n\t\tsearch_paths = [ \"config-example-1.conf\",\n\t\t\t\tpath.join(process.env.HOME, \".config-examplerc\"),\n\t\t\t\t\"/usr/local/etc/config-example.conf\",\n\t\t\t\t\"/usr/etc/config-example.conf\",\n\t\t\t\t\"/etc/config-example.conf\" ];\n\t\n\tconsole.log(\"Unprocessed config:\", config);\n\topt.config(config, search_paths);\n\t\n\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\t\"OPTIONS:\",\n\t\t\" copyright (c) 2012 all rights reserved\\n\" +\n\t\t\" Released under New the BSD License.\\n\" +\n\t\t\" See: http://opensource.org/licenses/bsd-license.php\\n\");\n\t\n\t\n\topt.on(\"ready\", function (config) {\n\t\topt.option([\"-n\", \"--name\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.name = param.trim();\n\t\t\t}\n\t\t}, \"Set the name parameter\");\n\t\n\t\topt.option([\"-e\", \"--email\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tconfig.email = param.trim();\n\t\t\t}\n\t\t}, \"Set the email parameter\");\n\t\t\n\t\topt.option([\"-g\", \"--generate\"], function (param) {\n\t\t\tif (param.trim()) {\n\t\t\t\tfs.writeFile(param.trim(), JSON.stringify(config));\n\t\t\t} else {\n\t\t\t\tconsole.log(JSON.stringify(config));\n\t\t\t}\n\t\t\tprocess.exit(0);\n\t\t}, \"Generate a configuration file\");\n\t\t\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t}, \"This help document.\");\n\t\n\t\topt.optionWith(process.argv);\n\t\n\t\t// config should now hold the merge configuration\n\t\t// from default_config and the first configuration file \n\t\t// found in the search path list.\n\t\tconsole.log(\"Processed config: \", config);\n\t});\n```\n\n## A simple web server API\n\n```javascript\n\t// Importing some modules\n\tvar util = require(\"util\"),\n\t\thttp = require(\"http\"),\n\t\tpath = require(\"path\"),\n\t\t// import and create the opt object\n\t\topt = require(\"opt\").create();\n\t\n\t// Define your configuration defaults and load your local configuration\n\t// file or it.\n\topt.config({ host: \"localhost\", port: 8080, name: \"John Doe\"},\n\t\t[ \"/etc/helloworld.json\", path.join(process.env.HOME, \"etc/helloworld.json\") ]);\n\t\n\t// When your configuration is \"ready\" parse the command lines\n\t// and setup your RESTful hello world web service\n\topt.on(\"ready\", function (config) {\n\t\t// Setup how your help page will look\n\t\topt.optionHelp(\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\t\"SYNOPSIS:\\n\\tThis is a simple hello world web service.\",\n\t\t\t\"OPTIONS:\",\n\t\t\t\"this is an opt demo\");\n\t\n\t\t// Define your command line options\n\t\topt.option([\"-H\", \"--host\"], function (hostname) {\n\t\t\tconfig.host = hostname.trim();\n\t\t});\n\t\topt.option([\"-p\", \"--port\"], function (portname) {\n\t\t\tconfig.port = Number(portname.trim());\n\t\t});\n\t\topt.option([\"-n\", \"--name\"], function (name) {\n\t\t\tconfig.name = name.trim();\n\t\t});\n\t\topt.option([\"-h\", \"--help\"], function () {\n\t\t\topt.usage();\n\t\t});\n\t\n\t\t// Define you restful service\n\t\tvar helloworld = function (req, res, matching, rule_no) {\n\t\t\tres.writeHead(200, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"Hello \" + config.name + \".\\nThis is what I found: \" + util.inspect(matching) + \"\\nRule No.:\" + rule_no);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^$|^/$|^/index.html|^/helloworld.html\"), helloworld);\n\t\n\t\tvar status404 = function (req, res) {\n\t\t\tres.writeHead(404, {\"content-type\": \"text/plain\"});\n\t\t\tres.end(\"File not found. \" + req.url);\n\t\t};\n\t\topt.rest(\"get\", new RegExp(\"^/*\"), status404);\n\t\n\t\t// Process your command line args.\n\t\topt.optionWith(process.argv);\n\t\n\t\t// Process your restful requets\n\t\tconsole.log(\"Configuration:\", config);\n\t\thttp.createServer(function (req, res) {\n\t\t\tconsole.log(\"request:\", req.url);\n\t\t\topt.restWith(req, res);\n\t\t}).listen(config.port, config.host);\n\t\tconsole.log(\"Web server listening on \" + config.host + \":\" + config.port);\n\t});\n```\n\n\n# Simple webserve example\n\nThis example sets up a simple hello web server.\n\n```JavaScript\n\tvar fs = require(\"fs\"),\n\t\tpath = require(\"path\"),\n\t\thttp = require(\"http\"),\n\t\turl = require(\"url\"),\n\t\topt = require(\"opt\").create(),\n\t\tTBone = require(\"tbone\"),\n\t\tH = new TBone.HTML(),\n\t\tconfig_filename = false,\n\t\tconfig = {\n\t\t\tport: 8123,\n\t\t\thost: \"localhost\"\n\t\t};\n\t\n\topt.optionHelp(\n\t\t\"USAGE node \" + path.basename(process.argv[1]),\n\t\t\"SYNOPSIS: Demonstrate how opt works to parse command line options.\\n\" +\n\t\t\"\\n\\t\\t node \" + path.basename(process.argv[1]) + \" --help\",\n\t\t\"OPTIONS:\",\n\t\t\"ACME Gelatin Company\"\n\t);\n\t\n\topt.consume(true);\n\topt.option([\"-p\", \"--port\"], function (arg) {\n\t\ttry {\n\t\t\tconfig.port = Number(arg);\n\t\t} catch (err0) {\n\t\t\tconsole.error(err0);\n\t\t\tprocess.exit(1);\n\t\t}\n\t\topt.consume(arg);\n\t}, \"Set the port to listen on.\");\n\t\n\topt.option([\"-c\", \"--config\"], function (arg) {\n\t\tconfig_filename = arg;\n\t\toption.consume(arg);\n\t}, \"Set the configuration file to use.\");\n\t\n\topt.option([\"-h\", \"--help\"], function (arg) {\n\t\topt.usage();\n\t}, \"This help page.\");\n\t\n\topt.optionWith(process.argv);\n\t\n\tif (config_filename) {\n\t\tconfig = JSON.parse(fs.readFileSync(config_filename).toString());\n\t}\n\t\n\thttp.createServer(function (request, response) {\n\t\tconsole.log(\"request:\", request.url);\n\t\tresponse.writeHead(200, \"text/html\");\n\t\tresponse.end(H.html(\n\t\t\tH.head(\n\t\t\t\tH.title(\"Hello World\")\n\t\t\t),\n\t\t\tH.body(\n\t\t\t\tH.h1(\"Hello World\")\n\t\t\t)\n\t\t).attr({lang: \"en\"}));\n\t}).listen(config.port);\n```\n","readmeFilename":"README.md","_id":"opt@0.1.14","dist":{"shasum":"03f9ff4567587961d5985a5e9c983833122934af","tarball":"https://registry.npmjs.org/opt/-/opt-0.1.14.tgz","integrity":"sha512-0obuRE/sczuhbpgCl/o2M36ALa4+CrcGg3ZhBQMIWIoib6gzsk1CJZkYW5eQK9ZTJ3d6uaxHmhh8pUBf2Yq5ow==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDiJruz0U7cgoWYUiP8zhusEL/QdUAI0S6LOJFaJvOtMAIgF34tLAJy06UKTH7c8asn+m1tb1oDzE9elOzwMA2+0Aw="}]},"_from":".","_npmVersion":"1.2.24","_npmUser":{"name":"rsdoiel","email":"rsdoiel@gmail.com"}}},"readme":"opt\n===\nrevision 0.0.1\n--------------\n\n# Overview\n\nA very simple options module for NodeJS command line apps.\n\n# Example\n\nSource code example-1.js\n\n\tvar util = require('util'),\n\t\topt = require('./opt'),\n\t\tconfig = {},\n\t\tUSAGE = function () {\n\t\t\treturn \"\\n\\nUSAGE node example-1.js -- demo options.\\n\\n SYNOPSIS\\n\\n\\t\\tnode example-1.js --help \";\n\t\t};\n\t\n\topt.set(['-h','--help'], function () {\n\t\t\tvar help = opt.help(), ky;\n\t\n\t\t\tconsole.log(USAGE() + \"\\n\\n OPTIONS\\n\");\n\t\t\tfor (ky in help) {\n\t\t\t\t\tconsole.log(\"\\t\" + ky + \"\\t\\t\" + help[ky]);     \n\t\t\t}\n\t\t\tconsole.log(\"\\n\\n\");\n\t\t\tprocess.exit(0);\n\t}, \"This help document.\");\n\t\t\n\t// Parse the command line options\n\tif (process.argv.length < 3) {\n\t\tconsole.log(\"Try using a command line option for demo:\\n\" + USAGE());\n\t} else {\n\t\topt.parse(process.argv);\n\t}\n\tconsole.log(\"\\nConfig object properties set by opt: \");\n\tconsole.log(util.inspect(config));\n","maintainers":[{"name":"rsdoiel","email":"rsdoiel@gmail.com"}],"time":{"modified":"2022-06-23T01:03:33.163Z","created":"2011-12-13T15:27:22.421Z","0.0.1":"2011-12-13T15:27:23.784Z","0.0.2":"2012-01-19T18:24:11.179Z","0.0.2b":"2012-01-19T18:47:25.703Z","0.0.2c":"2012-01-19T19:02:04.585Z","0.0.2d":"2012-03-08T18:36:42.426Z","0.0.2e":"2012-03-08T19:42:03.041Z","0.0.3":"2012-04-09T21:34:30.438Z","0.0.3b":"2012-04-10T04:15:27.242Z","0.0.3c":"2012-04-10T16:57:33.770Z","0.0.4":"2012-05-03T14:17:51.815Z","0.0.5":"2012-05-03T15:00:20.410Z","0.0.6":"2012-05-05T20:58:53.273Z","0.0.7":"2012-06-24T03:17:13.730Z","0.0.8":"2012-06-25T22:48:18.684Z","0.0.9":"2012-06-27T06:01:42.214Z","0.0.10":"2012-09-15T04:56:18.239Z","0.1.0":"2012-10-13T01:52:08.742Z","0.1.1":"2012-10-13T03:53:40.325Z","0.1.11":"2012-11-06T06:50:36.168Z","0.1.13":"2013-05-31T22:12:15.166Z","0.1.14":"2013-06-02T18:13:00.380Z"},"author":{"name":"R. S. Doiel","email":"rsdoiel@gmail.com","url":"https://github.com/rsdoiel"},"repository":{"type":"git","url":"https://github.com/rsdoiel/opt.git"}}