time-to-botec

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

cli (2836B)


      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 RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP;
     31 var reFromString = require( '@stdlib/utils/regexp-from-string' );
     32 var fromCodePoint = require( './../lib' );
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Main execution sequence.
     39 *
     40 * @private
     41 * @returns {void}
     42 */
     43 function main() {
     44 	var flags;
     45 	var args;
     46 	var cli;
     47 	var i;
     48 
     49 	// Create a command-line interface:
     50 	cli = new CLI({
     51 		'pkg': require( './../package.json' ),
     52 		'options': require( './../etc/cli_opts.json' ),
     53 		'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
     54 			'encoding': 'utf8'
     55 		})
     56 	});
     57 
     58 	// Get any provided command-line options:
     59 	flags = cli.flags();
     60 	if ( flags.help || flags.version ) {
     61 		return;
     62 	}
     63 
     64 	// Get any provided command-line arguments:
     65 	args = cli.args();
     66 
     67 	// Check if we are receiving data from `stdin`...
     68 	if ( stdinStream.isTTY ) {
     69 		for ( i = 0; i < args.length; i++ ) {
     70 			args[ i ] = parseInt( args[ i ], 10 );
     71 		}
     72 		return console.log( fromCodePoint( args ) ); // eslint-disable-line no-console
     73 	}
     74 	return stdin( onRead );
     75 
     76 	/**
     77 	* Callback invoked upon reading from `stdin`.
     78 	*
     79 	* @private
     80 	* @param {(Error|null)} error - error object
     81 	* @param {Buffer} data - data
     82 	* @returns {void}
     83 	*/
     84 	function onRead( error, data ) {
     85 		var flags;
     86 		var sep;
     87 		if ( error ) {
     88 			return cli.error( error );
     89 		}
     90 		flags = cli.flags();
     91 		if ( flags.split ) {
     92 			sep = reFromString( flags.split );
     93 			if ( sep === null ) {
     94 				// If the previous command "failed", we were not provided a regular expression:
     95 				sep = flags.split;
     96 			}
     97 		} else {
     98 			sep = RE_EOL;
     99 		}
    100 		data = data.toString().split( sep );
    101 
    102 		// Remove any trailing separators (e.g., trailing newline)...
    103 		if ( data[ data.length-1 ] === '' ) {
    104 			data.pop();
    105 		}
    106 		// Cast all values to integers...
    107 		for ( i = 0; i < data.length; i++ ) {
    108 			data[ i ] = parseInt( data[ i ], 10 );
    109 		}
    110 		console.log( fromCodePoint( data ) ); // eslint-disable-line no-console
    111 	}
    112 }
    113 
    114 main();