Add command line parsing.

This commit is contained in:
Eddie Antonio Santos 2019-04-15 11:07:24 -06:00
parent af52f72bb9
commit 6f0661d96d
No known key found for this signature in database
GPG key ID: DD411EA4D9509D87
3 changed files with 66 additions and 5 deletions

View file

@ -6,12 +6,17 @@
const readline = require('readline');
const {EventIterator} = require('event-iterator');
const program = require('commander');
// Load the most recent LMLayer code locally.
const LMLayer = require('../../predictive-text');
///////////////////////////////// Constants /////////////////////////////////
const WORKER_DEBUG = false;
const EXIT_USAGE = 1;
/** "Control sequence introducer" for ANSI escape codes: */
const CSI = '\033[';
@ -30,16 +35,46 @@ const ANSI = {
NORMAL: CSI + 'm', // Set all graphics renditions attributes off.
};
// Do it!
//////////////////////////////////// main ////////////////////////////////////
main();
function main() {
// Command line options:
program
.version(require('./package.json').version)
.usage('-f <model-file>')
.description('CLI for trying lexical models.')
.option('-f, --model-file <file>', 'path to model file')
.parse(process.argv);
let modelFile = program.modelFile;
if (! modelFile) {
console.error(`${program.name()}: You forgot to specify a model!`);
program.outputHelp();
process.exit(EXIT_USAGE);
}
// Ensure we're running in the terminal
if (!process.stdin.isTTY) {
throw new Error('must be run from interactive terminal');
}
asyncMain()
// Show a quick "how-to" message.
console.log(unindent(`
# Testing ${modelFile}
#
# Type text in the model's language. Suggestions will appear automatically.
#
# * Press ${ANSI.BOLD}<Tab>${ANSI.NORMAL} to select a suggestion.
# * Press ${ANSI.BOLD}<Tab>${ANSI.NORMAL} again to select the next suggestion.
# * Press ${ANSI.BOLD}<Shift>${ANSI.NORMAL}+${ANSI.BOLD}<Tab>${ANSI.NORMAL} to select the previous suggestion.
# * Press ${ANSI.BOLD}<Enter>${ANSI.NORMAL} to accept the suggestion.
# * Press ${ANSI.BOLD}<Ctrl>${ANSI.NORMAL}+${ANSI.BOLD}<C>${ANSI.NORMAL} to quit.
`))
asyncMain(modelFile)
.then(_ => process.exit(0))
.catch(err => {
console.error(err);
@ -48,10 +83,10 @@ function main() {
}
async function asyncMain() {
async function asyncMain(modelFile) {
// Load the LMLayer and the desired model.
let lm = new LMLayer({}, createAsyncWorker());
let config = await lm.loadModel('./example.crk.wordlist_wahkohtowin.model.js');
let config = await lm.loadModel(modelFile);
// Setup the REPL
// > type some text
@ -221,3 +256,23 @@ function keypressesFromStdin() {
}
);
}
///////////////////////////////// Utilities /////////////////////////////////
/**
* Unindents a template literal.
*/
function unindent(string) {
let lines = string.split('\n');
// Remove empty lines from the top and bottom.
if (lines[0] === '') {
lines.shift();
}
if (lines[lines.length -1] === '') {
lines.pop();
}
let numLeadingSpaces = lines[0].match(/^ */)[0].length;
return lines.map(line => line.substr(numLeadingSpaces)).join('\n');
}

View file

@ -4,6 +4,11 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"commander": {
"version": "2.20.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz",
"integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ=="
},
"event-iterator": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/event-iterator/-/event-iterator-1.2.0.tgz",

View file

@ -1,6 +1,6 @@
{
"name": "lmlayer-cli",
"version": "1.0.0",
"version": "0.1.0",
"description": "Command line interface to the predictive text engine.",
"main": "index.js",
"scripts": {
@ -9,6 +9,7 @@
"author": "Eddie Antonio Santos <Eddie.Santos@nrc-cnrc.gc.ca>",
"license": "MIT",
"dependencies": {
"commander": "^2.20.0",
"event-iterator": "^1.2.0"
}
}