From d52d9caeb35c0e84fd7313c16eedeb719d5b1e8e Mon Sep 17 00:00:00 2001 From: Ethan Brown Date: Mon, 10 Jun 2013 14:48:57 -0700 Subject: [PATCH] Tidied up command-line interface, fail gracefully if no program/input is specified. --- jawk.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/jawk.js b/jawk.js index af99a87..fcf6471 100755 --- a/jawk.js +++ b/jawk.js @@ -5,13 +5,13 @@ var fs = require('fs'), readlines = require('./lib/readlines').readlines; var opts = require('nomnom') + .script('jawk') .option( 'file', { abbr: 'f', flag: false, help: 'Script file' }) .option( 'verbose', { - abbr: 'v', flag: true, help: 'Verbose output' }) @@ -24,11 +24,27 @@ var inputEncoding = 'utf8'; if( opts.file ) { program = fs.readFileSync( opts.file, 'utf8' ).replace(/\r/g,''); // TODO: handle case of console input - inputFname = opts[0]; + if( opts._.length === 0 ) { + console.log( 'error: you must specify an input file (console input not currently supported).' ); + process.exit( 1 ); + } else if( opts._.length === 1 ) { + inputFname = opts[0]; + } else { + console.log( 'error: too many arguments.' ); + process.exit( 1 ); + } } else { - program = opts[0].replace(/\r/g,''); - // TODO: handle case of console input - inputFname = opts[1]; + if( opts._.length < 2 ) { + console.log( 'error: you must specify a jawk program and an input file (console input not currently supported).' ); + process.exit( 1 ); + } else if( opts._.length === 2 ) { + program = opts[0].replace(/\r/g,''); + // TODO: handle case of console input + inputFname = opts[1]; + } else { + console.log( 'error: too many arguments.' ); + process.exit( 1 ); + } } // note that for functions below, this!=jawkContext, so we have to use jawkContext explicitly. this is a little