Skip to content

Commit ed55a76

Browse files
committed
Update CLI
1 parent fdf7fd4 commit ed55a76

File tree

6 files changed

+346
-88
lines changed

6 files changed

+346
-88
lines changed

lib/node_modules/@stdlib/string/pad/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ for ( i = 0; i < 100; i++ ) {
180180
### Usage
181181

182182
```text
183-
Usage: padstr [options] <string> --len <length>
183+
Usage: padstr [options] [<string>] --len <length>
184184
185185
Options:
186186
@@ -205,6 +205,13 @@ $ padstr beep --len 10 --lpad b --rpad p
205205
bbbbeepppp
206206
```
207207

208+
To use as a [standard stream][standard-streams],
209+
210+
```bash
211+
$ echo -n 'beep' | pad --len 9 --lpad a --rpad o
212+
aabeepoo0
213+
```
214+
208215
</section>
209216

210217
<!-- /.examples -->
@@ -219,6 +226,8 @@ bbbbeepppp
219226

220227
[@stdlib/string/right-pad]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/right-pad
221228

229+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
230+
222231
</section>
223232

224233
<!-- /.links -->

lib/node_modules/@stdlib/string/pad/bin/cli

100644100755
Lines changed: 72 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,100 +3,86 @@
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 opts = require( './opts.json' );
11-
var pkg = require( './../package.json' );
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' );
1211
var pad = require( './../lib' );
1312

1413

15-
// FUNCTIONS //
14+
// MAIN //
1615

1716
/**
18-
* Performs initialization tasks.
17+
* Main execution sequence.
1918
*
2019
* @private
21-
* @example
22-
* init();
20+
* @returns {void}
2321
*/
24-
function init() {
22+
function main() {
23+
var flags;
24+
var args;
2525
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()
37-
38-
/**
39-
* Prints usage information.
40-
*
41-
* @private
42-
* @example
43-
* help();
44-
* // => '...'
45-
*/
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 );
26+
var cli;
27+
var len;
28+
var str;
29+
30+
// Create a command-line interface:
31+
cli = new CLI({
32+
'pkg': require( './../package.json' ),
33+
'options': require( './../etc/cli_opts.json' ),
34+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
35+
'encoding': 'utf8'
36+
})
37+
});
38+
args = cli.args();
39+
flags = cli.flags();
40+
41+
if ( args.length ) {
42+
str = args[ 0 ];
43+
} else {
44+
// Treat an empty value as an empty string:
45+
str = '';
46+
}
47+
len = parseInt( flags.len, 10 );
48+
opts = {};
49+
if ( flags.lpad ) {
50+
opts.lpad = flags.lpad;
51+
}
52+
if ( flags.rpad ) {
53+
opts.rpad = flags.rpad;
54+
}
55+
if ( flags.cright ) {
56+
opts.centerRight = flags.cright;
5457
}
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-
76-
77-
// MAIN //
78-
79-
init();
80-
81-
// Parse command-line arguments:
82-
args = parseArgs( process.argv.slice( 2 ), opts );
83-
84-
if ( args.help ) {
85-
return help();
86-
}
87-
if ( args.version ) {
88-
return version();
89-
}
90-
91-
opts = {};
92-
if ( args.lpad ) {
93-
opts.lpad = args.lpad;
94-
}
95-
if ( args.rpad ) {
96-
opts.rpad = args.rpad;
97-
}
98-
if ( args.cright ) {
99-
opts.centerRight = args.cright;
100-
}
10158

102-
console.log( pad( args._[0], parseInt( args.len, 10 ), opts ) );
59+
// Check if we are receiving data from `stdin`...
60+
if ( !process.stdin.isTTY ) {
61+
return stdin( onRead );
62+
}
63+
console.log( pad( str, len, opts ) ); // eslint-disable-line no-console
64+
65+
/**
66+
* Callback invoked upon reading from `stdin`.
67+
*
68+
* @private
69+
* @param {(Error|null)} error - error object
70+
* @param {Buffer} data - data
71+
* @returns {void}
72+
*/
73+
function onRead( error, data ) {
74+
/* eslint-disable no-console */
75+
var lines;
76+
var i;
77+
if ( error ) {
78+
process.exitCode = 1;
79+
return console.error( 'Error: %s', error.message );
80+
}
81+
lines = data.toString().split( RE_EOL );
82+
for ( i = 0; i < lines.length; i++ ) {
83+
console.log( pad( lines[ i ], len, opts ) );
84+
}
85+
} // end FUNCTION onRead()
86+
} // end FUNCTION main()
87+
88+
main();

lib/node_modules/@stdlib/string/pad/bin/usage.txt renamed to lib/node_modules/@stdlib/string/pad/docs/usage.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Usage: padstr [options] <string> --len <length>
2+
Usage: padstr [options] [<string>] --len <length>
33

44
Options:
55

File renamed without changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
proxyquire( fpath, {
9+
'@stdlib/utils/read-stdin': stdin
10+
});
11+
12+
function stdin( clbk ) {
13+
clbk( new Error( 'beep' ) );
14+
}

0 commit comments

Comments
 (0)