Commit 8ad14a50 authored by Luc Renambot's avatar Luc Renambot

Added command line parsing: current options

   --help or -h for help (builtin)
   --version or -V for version (builtin)
   --interactive or -i for interactive command loop
Added a command input loop to the server
Added exit commands: exit,quit,bye


Former-commit-id: 5764e7dcf774b0394c5e5dfa936acc283aa9fb4c
parent 95f870f0
......@@ -33,7 +33,7 @@ httpserver.prototype.onreq = function(req, res) {
if(getName == "/") getName = "/index.html";
var pathname = this.publicDirectory + getName;
console.log(pathname);
//console.log(pathname);
var header = {};
var type = mime.lookup(pathname);
......
......@@ -52,6 +52,7 @@
"ytdl": "latest",
"jshint": "latest",
"jslint": "latest",
"commander": "latest",
"sprint": "^0.3.1"
},
"bundledDependencies": [],
......
......@@ -39,6 +39,8 @@ var os = require('os'); // operating system access
var path = require('path'); // file path extraction and creation
var request = require('request'); // external http requests
var sprint = require('sprint'); // pretty formating (sprintf)
var readline = require('readline'); // to build an evaluation loop (builtin module)
var program = require('commander'); // parsing command-line arguments
// custom node modules
var httpserver = require('node-httpserver'); // creates web server
......@@ -47,6 +49,11 @@ var loader = require('node-itemloader'); // handles sage item creation
var interaction = require('node-interaction'); // handles sage interaction (move, resize, etc.)
var sagepointer = require('node-sagepointer'); // handles sage pointers (creation, location, etc.)
// Command line arguments
program
.version('0.1.0')
.option('-i, --interactive', 'Interactive prompt')
.parse(process.argv);
// load config file - looks for user defined file, then file that matches hostname, then uses default
var config = loadConfiguration();
......@@ -1435,6 +1442,54 @@ index.listen(config.index_port);
server.listen(config.port);
/***************************************************************************************/
// Command loop: reading input commands
if (program.interactive)
{
// Create line reader for stdin and stdout
var shell = readline.createInterface({
input: process.stdin, output: process.stdout
});
// Set the prompt
shell.setPrompt('> ');
// Start the loop
shell.prompt();
// Callback for each line
shell.on('line', function(line) {
switch(line.trim()) {
case '': // ignore
break;
case 'hello':
console.log('world!');
break;
case 'exit':
case 'quit':
case 'bye':
console.log('');
console.log('SAGE2 done');
console.log('');
process.exit(0);
break;
default:
console.log('Say what? I might have heard `' + line.trim() + '`');
break;
}
// loop through
shell.prompt();
}).on('close', function() {
// Close with CTRL-D or CTRL-C
console.log('');
console.log('SAGE2 done');
console.log('');
process.exit(0);
});
}
/***************************************************************************************/
function broadcast(func, data, type) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment