Skip to content

Commit 2bc539e

Browse files
committed
Update CLI
1 parent 0a36708 commit 2bc539e

File tree

4 files changed

+182
-89
lines changed

4 files changed

+182
-89
lines changed

lib/node_modules/@stdlib/datasets/spache-revised/bin/cli

+33-89
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,48 @@
44
// MODULES //
55

66
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' );
7+
var resolve = require( 'path' ).resolve;
8+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
9+
var stdout = require( '@stdlib/streams/base/stdout' );
10+
var CLI = require( '@stdlib/tools/cli' );
1211

1312

14-
// FUNCTIONS //
15-
16-
/**
17-
* Performs initialization tasks.
18-
*
19-
* @private
20-
* @example
21-
* init();
22-
*/
23-
function init() {
24-
var opts;
25-
26-
// Check if newer versions exist for this package:
27-
opts = {
28-
'pkg': pkg
29-
};
30-
notifier( opts ).notify();
31-
32-
// Set the process title to allow the process to be more easily identified:
33-
process.title = pkg.name;
34-
process.stdout.on( 'error', process.exit );
35-
} // end FUNCTION init()
36-
37-
/**
38-
* Prints usage information.
39-
*
40-
* @private
41-
* @example
42-
* help();
43-
* // => '...'
44-
*/
45-
function help() {
46-
var fpath = path.join( __dirname, 'usage.txt' );
47-
fs.createReadStream( fpath )
48-
.pipe( process.stdout )
49-
.on( 'close', onClose );
50-
51-
function onClose() {
52-
process.exit( 0 );
53-
}
54-
} // end FUNCTION help()
55-
56-
/**
57-
* Prints the package version.
58-
*
59-
* @private
60-
* @example
61-
* version();
62-
* // => '#.#.#'
63-
*/
64-
function version() {
65-
var msg = pkg.version.toString()+'\n';
66-
process.stdout.write( msg, 'utf8' );
67-
process.exit( 0 );
68-
} // end FUNCTION version()
13+
// MAIN //
6914

7015
/**
71-
* Prints data as text.
16+
* Main execution sequence.
7217
*
7318
* @private
7419
*/
75-
function txt() {
76-
var fpath = path.resolve( __dirname, '../data/words.txt' );
20+
function main() {
21+
var fpath;
22+
var cli;
23+
24+
// Create a command-line interface:
25+
cli = new CLI({
26+
'pkg': require( './../package.json' ),
27+
'options': require( './../etc/cli_opts.json' ),
28+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
29+
'encoding': 'utf8'
30+
})
31+
});
32+
33+
// Resolve the data file path:
34+
fpath = resolve( __dirname, '..', 'data', 'words.txt' );
35+
36+
// Print the data to stdout:
7737
fs.createReadStream( fpath )
78-
.pipe( process.stdout )
38+
.pipe( stdout )
7939
.on( 'close', onClose );
8040

41+
/**
42+
* Exits the CLI.
43+
*
44+
* @private
45+
*/
8146
function onClose() {
82-
process.exit( 0 );
83-
}
84-
} // end FUNCTION txt()
85-
86-
87-
// VARIABLES //
88-
89-
var args;
90-
91-
92-
// MAIN //
93-
94-
init();
95-
96-
// Parse command-line arguments:
97-
args = parseArgs( process.argv.slice( 2 ), opts );
98-
99-
if ( args.help ) {
100-
return help();
101-
}
102-
if ( args.version ) {
103-
return version();
104-
}
47+
cli.exit( 0 );
48+
} // end FUNCTION onClose()
49+
} // end FUNCTION main()
10550

106-
// Run main:
107-
txt();
51+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var resolve = require( 'path' ).resolve;
6+
var exec = require( 'child_process' ).exec;
7+
var tape = require( 'tape' );
8+
var IS_BROWSER = require( '@stdlib/assert/is-browser' );
9+
var IS_WINDOWS = require( '@stdlib/assert/is-windows' );
10+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
11+
12+
13+
// VARIABLES //
14+
15+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
16+
var opts = {
17+
'skip': IS_BROWSER || IS_WINDOWS
18+
};
19+
20+
21+
// FIXTURES //
22+
23+
var PKG_VERSION = require( './../package.json' ).version;
24+
25+
26+
// TESTS //
27+
28+
tape( 'command-line interface', function test( t ) {
29+
t.ok( true, __filename );
30+
t.end();
31+
});
32+
33+
tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
34+
var expected;
35+
var cmd;
36+
37+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
38+
'encoding': 'utf8'
39+
});
40+
cmd = [
41+
process.execPath,
42+
fpath,
43+
'--help'
44+
];
45+
46+
exec( cmd.join( ' ' ), done );
47+
48+
function done( error, stdout, stderr ) {
49+
if ( error ) {
50+
t.fail( error.message );
51+
} else {
52+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
53+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
54+
}
55+
t.end();
56+
}
57+
});
58+
59+
tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
60+
var expected;
61+
var cmd;
62+
63+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
64+
'encoding': 'utf8'
65+
});
66+
cmd = [
67+
process.execPath,
68+
fpath,
69+
'-h'
70+
];
71+
72+
exec( cmd.join( ' ' ), done );
73+
74+
function done( error, stdout, stderr ) {
75+
if ( error ) {
76+
t.fail( error.message );
77+
} else {
78+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
79+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
80+
}
81+
t.end();
82+
}
83+
});
84+
85+
tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
86+
var cmd = [
87+
process.execPath,
88+
fpath,
89+
'--version'
90+
];
91+
92+
exec( cmd.join( ' ' ), done );
93+
94+
function done( error, stdout, stderr ) {
95+
if ( error ) {
96+
t.fail( error.message );
97+
} else {
98+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
99+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
100+
}
101+
t.end();
102+
}
103+
});
104+
105+
tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
106+
var cmd = [
107+
process.execPath,
108+
fpath,
109+
'-V'
110+
];
111+
112+
exec( cmd.join( ' ' ), done );
113+
114+
function done( error, stdout, stderr ) {
115+
if ( error ) {
116+
t.fail( error.message );
117+
} else {
118+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
119+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
120+
}
121+
t.end();
122+
}
123+
});
124+
125+
tape( 'the command-line interface prints the list of words', opts, function test( t ) {
126+
var expected;
127+
var cmd;
128+
129+
cmd = [
130+
process.execPath,
131+
fpath
132+
];
133+
134+
expected = readFileSync( resolve( __dirname, '..', 'data', 'words.txt' ) );
135+
136+
exec( cmd.join( ' ' ), done );
137+
138+
function done( error, stdout, stderr ) {
139+
if ( error ) {
140+
t.fail( error.message );
141+
} else {
142+
stdout = stdout.toString();
143+
expected = expected.toString();
144+
t.strictEqual( stdout, expected, 'prints words' );
145+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
146+
}
147+
t.end();
148+
}
149+
});

0 commit comments

Comments
 (0)