Skip to content

Commit e58a6e9

Browse files
committed
Update CLI
1 parent aa1d66c commit e58a6e9

File tree

6 files changed

+318
-92
lines changed

6 files changed

+318
-92
lines changed

lib/node_modules/@stdlib/string/starts-with/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool = startsWith( str, 'filthy', -10 );
9898
### Usage
9999

100100
```text
101-
Usage: starts-with [options] --search=<string> <string>
101+
Usage: starts-with [options] --search=<string> [<string>]
102102
103103
Options:
104104
@@ -121,6 +121,13 @@ $ starts-with --search=be beep
121121
true
122122
```
123123

124+
To use as a [standard stream][standard-streams],
125+
126+
```bash
127+
$ echo -n 'boop' | starts-with --search=bo
128+
true
129+
```
130+
124131
</section>
125132

126133
<!-- /.examples -->
@@ -131,6 +138,8 @@ true
131138

132139
<section class="links">
133140

141+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
142+
134143
</section>
135144

136145
<!-- /.links -->

lib/node_modules/@stdlib/string/starts-with/bin/cli

100644100755
+67-91
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,78 @@
33

44
// MODULES //
55

6-
var fs = require( 'fs' );
7-
var path = require( 'path' );
8-
var parseArgs = require( 'minimist' );
9-
var notifier = require( 'update-notifier' );
10-
var pkg = require( './../package.json' );
11-
var opts = require( './opts.json' );
12-
var main = require( './../lib' );
6+
var resolve = require( 'path' ).resolve;
7+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
8+
var CLI = require( '@stdlib/tools/cli' );
9+
var stdin = require( '@stdlib/utils/read-stdin' );
10+
var RE_EOL = require( '@stdlib/regexp/eol' );
11+
var startsWith = require( './../lib' );
1312

1413

15-
// FUNCTIONS //
16-
17-
/**
18-
* Performs initialization tasks.
19-
*
20-
* @private
21-
* @example
22-
* init();
23-
*/
24-
function init() {
25-
var opts;
26-
27-
// Check if newer versions exist for this package:
28-
opts = {
29-
'pkg': pkg
30-
};
31-
notifier( opts ).notify();
32-
33-
// Set the process title to allow the process to be more easily identified:
34-
process.title = pkg.name;
35-
process.stdout.on( 'error', process.exit );
36-
} // end FUNCTION init()
14+
// MAIN //
3715

3816
/**
39-
* Prints usage information.
17+
* Main execution sequence.
4018
*
4119
* @private
42-
* @example
43-
* help();
44-
* // => '...'
20+
* @returns {void}
4521
*/
46-
function help() {
47-
var fpath = path.join( __dirname, 'usage.txt' );
48-
fs.createReadStream( fpath )
49-
.pipe( process.stdout )
50-
.on( 'close', onClose );
51-
52-
function onClose() {
53-
process.exit( 0 );
22+
function main() {
23+
var flags;
24+
var args;
25+
var cli;
26+
var pos;
27+
var str;
28+
29+
// Create a command-line interface:
30+
cli = new CLI({
31+
'pkg': require( './../package.json' ),
32+
'options': require( './../etc/cli_opts.json' ),
33+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
34+
'encoding': 'utf8'
35+
})
36+
});
37+
args = cli.args();
38+
flags = cli.flags();
39+
40+
if ( args.length ) {
41+
str = args[ 0 ];
42+
} else {
43+
// Treat an empty value as an empty string:
44+
str = '';
5445
}
55-
} // end FUNCTION help()
56-
57-
/**
58-
* Prints the package version.
59-
*
60-
* @private
61-
* @example
62-
* version();
63-
* // => '#.#.#'
64-
*/
65-
function version() {
66-
var msg = pkg.version.toString()+'\n';
67-
process.stdout.write( msg, 'utf8' );
68-
process.exit( 0 );
69-
} // end FUNCTION version()
70-
71-
72-
// VARIABLES //
73-
74-
var args;
75-
var str;
76-
var pos;
77-
78-
79-
// MAIN //
80-
81-
init();
82-
83-
// Parse command-line arguments:
84-
args = parseArgs( process.argv.slice( 2 ), opts );
85-
86-
if ( args.help ) {
87-
return help();
88-
}
89-
if ( args.version ) {
90-
return version();
91-
}
92-
if ( args._.length ) {
93-
str = args._[ 0 ];
94-
} else {
95-
// Treat an empty value as an empty string:
96-
str = '';
97-
}
98-
if ( args.pos ) {
99-
pos = parseInt( args.pos, 10 );
100-
} else {
101-
pos = 0;
102-
}
103-
104-
console.log( main( str, args.search, pos ) );
46+
if ( flags.pos ) {
47+
pos = parseInt( flags.pos, 10 );
48+
} else {
49+
pos = 0;
50+
}
51+
// Check if we are receiving data from `stdin`...
52+
if ( !process.stdin.isTTY ) {
53+
return stdin( onRead );
54+
}
55+
console.log( startsWith( str, flags.search, pos ) ); // eslint-disable-line no-console
56+
57+
/**
58+
* Callback invoked upon reading from `stdin`.
59+
*
60+
* @private
61+
* @param {(Error|null)} error - error object
62+
* @param {Buffer} data - data
63+
* @returns {void}
64+
*/
65+
function onRead( error, data ) {
66+
/* eslint-disable no-console */
67+
var lines;
68+
var i;
69+
if ( error ) {
70+
process.exitCode = 1;
71+
return console.error( 'Error: %s', error.message );
72+
}
73+
lines = data.toString().split( RE_EOL );
74+
for ( i = 0; i < lines.length; i++ ) {
75+
console.log( startsWith( lines[ i ], flags.search, pos ) );
76+
}
77+
} // end FUNCTION onRead()
78+
} // end FUNCTION main()
79+
80+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var resolve = require( 'path' ).resolve;
2+
var proxyquire = require( 'proxyquire' );
3+
4+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
5+
6+
process.stdin.isTTY = false;
7+
8+
process.stdin.write = '--search=beep';
9+
10+
proxyquire( fpath, {
11+
'@stdlib/utils/read-stdin': stdin
12+
});
13+
14+
function stdin( clbk ) {
15+
clbk( new Error( 'beep' ) );
16+
}

0 commit comments

Comments
 (0)