Gathering detailed insights and metrics for @stdlib/cli-ctor
Gathering detailed insights and metrics for @stdlib/cli-ctor
Gathering detailed insights and metrics for @stdlib/cli-ctor
Gathering detailed insights and metrics for @stdlib/cli-ctor
npm install @stdlib/cli-ctor
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
58 Commits
3 Watching
4 Branches
10 Contributors
Updated on 01 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-14.6%
61,722
Compared to previous day
Last week
-6.5%
344,347
Compared to previous week
Last month
-2.8%
1,530,430
Compared to previous month
Last year
18.7%
23,788,042
Compared to previous year
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Command-line interface.
1npm install @stdlib/cli-ctor
1var CLI = require( '@stdlib/cli-ctor' );
Command-line interface (CLI) constructor.
1var cli = new CLI(); 2// returns <CLI>
The constructor accepts the following options
:
package.json
object.pkg.version
.true
, the default title is either pkg.bin.<field>
or pkg.name
. If set to a string
, the function sets the process title to the specified string. If set to false
, the function does not set the process title.''
.boolean
indicating whether to check if a more recent version of a command-line interface exists in the package registry. In order to check for updates, the function requires both pkg.name
and pkg.version
meta data. Default: true
.array
of command-line arguments. Default: process.argv
.To provide package meta data, such as the package name
and version
, set the pkg
option.
1var opts = { 2 'pkg': require( './package.json' ) 3}; 4 5var cli = new CLI( opts ); 6// returns <CLI>
To specify a particular command-line interface version (overriding package meta data), set the version
option.
1var opts = { 2 'pkg': { 3 'name': 'beep', 4 'version': '1.1.1' 5 }, 6 'version': '1.1.1-beta' 7}; 8 9var cli = new CLI( opts ); 10// returns <CLI> 11 12cli.version(); 13// => 1.1.1-beta
By default, an instance sets the process title to either the first key in pkg.bin
or to pkg.name
. To explicitly set the process title, set the title
option.
1var proc = require( 'process' ); 2 3var opts = { 4 'title': 'beep-boop' 5}; 6 7var cli = new CLI( opts ); 8// returns <CLI> 9 10console.log( proc.title ); 11// => 'beep-boop'
To disable setting the process title, set the title
option to false
.
1var opts = { 2 'title': false 3}; 4 5var cli = new CLI( opts ); 6// returns <CLI>
When the command-line flag --help
is set, a command-line interface instance prints help text and exits the calling process. To specify the printed text, set the help
option.
1var opts = { 2 'help': 'Usage: boop [options] <beep>', 3 'argv': [ 4 '/usr/local/bin/node', 5 'foo.js', 6 '--help' 7 ] 8}; 9 10var cli = new CLI( opts ); 11// => Usage: boop [options] <beep>
By default, an instance resolves command-line arguments and flags via process.argv
. To specify a custom set of command-line arguments, set the argv
option.
1var opts = { 2 'argv': [ 3 '/usr/local/bin/node', 4 'foo.js', 5 'a', 6 'b', 7 'c' 8 ] 9}; 10 11var cli = new CLI( opts ); 12 13var args = cli.args(); 14// returns [ 'a', 'b', 'c' ]
To specify command-line argument parser options, such as command-line flag types and aliases, set the options
option.
1var opts = { 2 'options': { 3 'boolean': [ 4 'help', 5 'version' 6 ], 7 'string': [ 8 'output' 9 ], 10 'alias': { 11 'help': [ 12 'h' 13 ], 14 'version': [ 15 'V' 16 ], 17 'output': [ 18 'o' 19 ] 20 } 21 }, 22 'argv': [ 23 '/usr/local/bin/node', 24 'foo.js', 25 '-o=bar.js' 26 ] 27}; 28 29var cli = new CLI( opts ); 30 31var flags = cli.flags(); 32/* returns 33 { 34 'h': false, 35 'help': false, 36 'V': false, 37 'version': false, 38 'o': 'bar.js', 39 'output': 'bar.js' 40 } 41*/
By default, if provided sufficient package meta data (package name
and version
), an instance checks whether a newer version of a command-line interface exists in the package registry. If a newer version exists, an instance writes a message to stdout
indicating that a newer version exists. To disable this check, set the updates
option to false
.
1var opts = { 2 'updates': false 3}; 4 5var cli = new CLI( opts ); 6// returns <CLI>
Gracefully exits a command-line interface and the calling process.
1var cli = new CLI(); 2 3// Gracefully exit: 4cli.close();
To specify an exit code, provide a code
argument.
1var cli = new CLI(); 2 3// Set the exit code to `1`: 4cli.close( 1 );
Prints an error message to stderr
and exits a command-line interface and the calling process.
1var cli = new CLI(); 2 3// ... 4 5// Create a new error object: 6var err = new Error( 'invalid argument' ); 7 8// Exit due to the error: 9cli.error( err );
When exiting due to an error, the default exit code is 1
. To specify an alternative exit code, provide a code
argument.
1var cli = new CLI(); 2 3// ... 4 5// Create a new error object: 6var err = new Error( 'invalid argument' ); 7 8// Exit due to the error: 9cli.error( err, 2 );
Forcefully exits a command-line interface and the calling process.
1var cli = new CLI(); 2 3// Forcefully exit: 4cli.exit();
To specify an exit code, provide a code
argument.
1var cli = new CLI(); 2 3// Set the exit code to `1`: 4cli.exit( 1 );
Returns a list of command-line arguments.
1var cli = new CLI({ 2 'argv': [ 3 '/usr/local/bin/node', 4 'foo.js', 5 'a', 6 '--b', 7 'c', 8 'd' 9 ] 10}); 11 12var args = cli.args(); 13// returns [ 'a', 'd' ]
Returns command-line flags.
1var cli = new CLI({ 2 'argv': [ 3 '/usr/local/bin/node', 4 'foo.js', 5 'a', 6 '--b', 7 'c', 8 '-def', 9 '--g=h', 10 'i' 11 ] 12}); 13 14var flags = cli.flags(); 15// returns { 'b': 'c', 'd': true, 'e': true, 'f': true, 'g': 'h' }
Prints help text to stderr
and then exits the calling process.
1var cli = new CLI({ 2 'help': 'Usage: beep [options] <boop>' 3}); 4 5cli.help(); 6// => Usage: beep [options] <boop>
By default, the process exits with an exit code equal to 0
. To exit with a different exit code, provide a code
argument.
Prints the command-line interface version to stderr
and then exits the calling process.
1var cli = new CLI({ 2 'version': '1.1.1' 3}); 4 5cli.version(); 6// => 1.1.1
--help
or --version
command-line flag is set, a command-line interface instance prints the respective value and then exits the calling process.options.argv
, the first element is reserved for the absolute pathname of the executable which launched the calling process and the second element is reserved for the file path of the executed JavaScript file.1var join = require( 'path' ).join; 2var readFileSync = require( '@stdlib/fs-read-file' ).sync; 3var CLI = require( '@stdlib/cli-ctor' ); 4var main = require( './examples/fixtures/main.js' ); 5 6// Load help text: 7var fopts = { 8 'encoding': 'utf8' 9}; 10var help = readFileSync( join( __dirname, 'examples', 'fixtures', 'usage.txt' ), fopts ); 11 12// Set the command-line interface options: 13var opts = { 14 'pkg': require( './package.json' ), 15 'options': require( './examples/fixtures/opts.json' ), 16 'help': help, 17 'title': true, 18 'updates': true 19}; 20 21// Create a new command-line interface: 22var cli = new CLI( opts ); 23 24// Run main: 25main( 'beep' ); 26 27// Close: 28cli.close( 0 );
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2024. The Stdlib Authors.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More