time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

cli (2241B)


      1 #!/usr/bin/env node
      2 
      3 /**
      4 * @license Apache-2.0
      5 *
      6 * Copyright (c) 2018 The Stdlib Authors.
      7 *
      8 * Licensed under the Apache License, Version 2.0 (the "License");
      9 * you may not use this file except in compliance with the License.
     10 * You may obtain a copy of the License at
     11 *
     12 *    http://www.apache.org/licenses/LICENSE-2.0
     13 *
     14 * Unless required by applicable law or agreed to in writing, software
     15 * distributed under the License is distributed on an "AS IS" BASIS,
     16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17 * See the License for the specific language governing permissions and
     18 * limitations under the License.
     19 */
     20 
     21 'use strict';
     22 
     23 // MODULES //
     24 
     25 var resolve = require( 'path' ).resolve;
     26 var readFileSync = require( '@stdlib/fs/read-file' ).sync;
     27 var CLI = require( '@stdlib/cli/ctor' );
     28 var stdin = require( '@stdlib/process/read-stdin' );
     29 var stdinStream = require( '@stdlib/streams/node/stdin' );
     30 var reEOL = require( '@stdlib/regexp/eol' );
     31 var isAlphaNumeric = require( './../lib' );
     32 
     33 
     34 // MAIN //
     35 
     36 /**
     37 * Main execution sequence.
     38 *
     39 * @private
     40 * @returns {void}
     41 */
     42 function main() {
     43 	var flags;
     44 	var args;
     45 	var cli;
     46 
     47 	// Create a command-line interface:
     48 	cli = new CLI({
     49 		'pkg': require( './../package.json' ),
     50 		'options': require( './../etc/cli_opts.json' ),
     51 		'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
     52 			'encoding': 'utf8'
     53 		})
     54 	});
     55 
     56 	// Get any provided command-line options:
     57 	flags = cli.flags();
     58 	if ( flags.help || flags.version ) {
     59 		return;
     60 	}
     61 
     62 	// Get any provided command-line arguments:
     63 	args = cli.args();
     64 
     65 	// Check if we are receiving data from `stdin`...
     66 	if ( !stdinStream.isTTY ) {
     67 		return stdin( onRead );
     68 	}
     69 	console.log( isAlphaNumeric( String( args[ 0 ] ) ) ); // eslint-disable-line no-console
     70 
     71 	/**
     72 	* Callback invoked upon reading from `stdin`.
     73 	*
     74 	* @private
     75 	* @param {(Error|null)} error - error object
     76 	* @param {Buffer} data - data
     77 	* @returns {void}
     78 	*/
     79 	function onRead( error, data ) {
     80 		var lines;
     81 		var i;
     82 		if ( error ) {
     83 			return cli.error( error );
     84 		}
     85 		lines = data.toString().split( reEOL.REGEXP );
     86 		for ( i = 0; i < lines.length; i++ ) {
     87 			console.log( isAlphaNumeric( lines[ i ] ) ); // eslint-disable-line no-console
     88 		}
     89 	}
     90 }
     91 
     92 main();