time-to-botec

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

README.md (9656B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # CLI
     22 
     23 > Command-line interface.
     24 
     25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
     26 
     27 <section class="intro">
     28 
     29 </section>
     30 
     31 <!-- /.intro -->
     32 
     33 <!-- Package usage documentation. -->
     34 
     35 <section class="usage">
     36 
     37 ## Usage
     38 
     39 ```javascript
     40 var CLI = require( '@stdlib/cli/ctor' );
     41 ```
     42 
     43 #### CLI( \[options] )
     44 
     45 Command-line interface (CLI) constructor.
     46 
     47 ```javascript
     48 var cli = new CLI();
     49 // returns <CLI>
     50 ```
     51 
     52 The constructor accepts the following `options`:
     53 
     54 -   **pkg**: package meta data, such as a `package.json` object.
     55 -   **version**: command-line interface version. Default: `pkg.version`.
     56 -   **title**: process title. If set to `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.
     57 -   **help**: help text. Default: `''`.
     58 -   **updates**: `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`.
     59 -   **argv**: an `array` of command-line arguments. Default: `process.argv`.
     60 -   **options**: command-line argument parser options.
     61 
     62 To provide package meta data, such as the package `name` and `version`, set the `pkg` option.
     63 
     64 ```javascript
     65 var opts = {
     66     'pkg': require( './package.json' )
     67 };
     68 
     69 var cli = new CLI( opts );
     70 // returns <CLI>
     71 ```
     72 
     73 To specify a particular command-line interface version (overriding package meta data), set the `version` option.
     74 
     75 ```javascript
     76 var opts = {
     77     'pkg': {
     78         'name': 'beep',
     79         'version': '1.1.1'
     80     },
     81     'version': '1.1.1-beta'
     82 };
     83 
     84 var cli = new CLI( opts );
     85 // returns <CLI>
     86 
     87 cli.version();
     88 // => 1.1.1-beta
     89 ```
     90 
     91 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.
     92 
     93 ```javascript
     94 var proc = require( 'process' );
     95 
     96 var opts = {
     97     'title': 'beep-boop'
     98 };
     99 
    100 var cli = new CLI( opts );
    101 // returns <CLI>
    102 
    103 console.log( proc.title );
    104 // => 'beep-boop'
    105 ```
    106 
    107 To disable setting the process title, set the `title` option to `false`.
    108 
    109 ```javascript
    110 var opts = {
    111     'title': false
    112 };
    113 
    114 var cli = new CLI( opts );
    115 // returns <CLI>
    116 ```
    117 
    118 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.
    119 
    120 <!-- eslint-disable stdlib/doctest-marker -->
    121 
    122 ```javascript
    123 var opts = {
    124     'help': 'Usage: boop [options] <beep>',
    125     'argv': [
    126         '/usr/local/bin/node',
    127         'foo.js',
    128         '--help'
    129     ]
    130 };
    131 
    132 var cli = new CLI( opts );
    133 // => Usage: boop [options] <beep>
    134 ```
    135 
    136 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.
    137 
    138 ```javascript
    139 var opts = {
    140     'argv': [
    141         '/usr/local/bin/node',
    142         'foo.js',
    143         'a',
    144         'b',
    145         'c'
    146     ]
    147 };
    148 
    149 var cli = new CLI( opts );
    150 
    151 var args = cli.args();
    152 // returns [ 'a', 'b', 'c' ]
    153 ```
    154 
    155 To specify command-line argument parser options, such as command-line flag types and aliases, set the `options` option.
    156 
    157 ```javascript
    158 var opts = {
    159     'options': {
    160         'boolean': [
    161             'help',
    162             'version'
    163         ],
    164         'string': [
    165             'output'
    166         ],
    167         'alias': {
    168             'help': [
    169                 'h'
    170             ],
    171             'version': [
    172                 'V'
    173             ],
    174             'output': [
    175                 'o'
    176             ]
    177         }
    178     },
    179     'argv': [
    180         '/usr/local/bin/node',
    181         'foo.js',
    182         '-o=bar.js'
    183     ]
    184 };
    185 
    186 var cli = new CLI( opts );
    187 
    188 var flags = cli.flags();
    189 /* returns
    190     {
    191         'h': false,
    192         'help': false,
    193         'V': false,
    194         'version': false,
    195         'o': 'bar.js',
    196         'output': 'bar.js'
    197     }
    198 */
    199 ```
    200 
    201 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`.
    202 
    203 ```javascript
    204 var opts = {
    205     'updates': false
    206 };
    207 
    208 var cli = new CLI( opts );
    209 // returns <CLI>
    210 ```
    211 
    212 * * *
    213 
    214 ### Prototype Methods
    215 
    216 #### CLI.prototype.close( \[code] )
    217 
    218 Gracefully exits a command-line interface and the calling process.
    219 
    220 ```javascript
    221 var cli = new CLI();
    222 
    223 // Gracefully exit:
    224 cli.close();
    225 ```
    226 
    227 To specify an exit code, provide a `code` argument.
    228 
    229 <!-- run-disable -->
    230 
    231 ```javascript
    232 var cli = new CLI();
    233 
    234 // Set the exit code to `1`:
    235 cli.close( 1 );
    236 ```
    237 
    238 #### CLI.prototype.error( error\[, code] )
    239 
    240 Prints an error message to `stderr` and exits a command-line interface and the calling process.
    241 
    242 <!-- run-disable -->
    243 
    244 ```javascript
    245 var cli = new CLI();
    246 
    247 // ...
    248 
    249 // Create a new error object:
    250 var err = new Error( 'invalid argument' );
    251 
    252 // Exit due to the error:
    253 cli.error( err );
    254 ```
    255 
    256 When exiting due to an error, the default exit code is `1`. To specify an alternative exit code, provide a `code` argument.
    257 
    258 <!-- run-disable -->
    259 
    260 ```javascript
    261 var cli = new CLI();
    262 
    263 // ...
    264 
    265 // Create a new error object:
    266 var err = new Error( 'invalid argument' );
    267 
    268 // Exit due to the error:
    269 cli.error( err, 2 );
    270 ```
    271 
    272 #### CLI.prototype.exit( \[code] )
    273 
    274 Forcefully exits a command-line interface and the calling process.
    275 
    276 ```javascript
    277 var cli = new CLI();
    278 
    279 // Forcefully exit:
    280 cli.exit();
    281 ```
    282 
    283 To specify an exit code, provide a `code` argument.
    284 
    285 <!-- run-disable -->
    286 
    287 ```javascript
    288 var cli = new CLI();
    289 
    290 // Set the exit code to `1`:
    291 cli.exit( 1 );
    292 ```
    293 
    294 * * *
    295 
    296 ### Instance Methods
    297 
    298 #### cli.args()
    299 
    300 Returns a list of command-line arguments.
    301 
    302 ```javascript
    303 var cli = new CLI({
    304     'argv': [
    305         '/usr/local/bin/node',
    306         'foo.js',
    307         'a',
    308         '--b',
    309         'c',
    310         'd'
    311     ]
    312 });
    313 
    314 var args = cli.args();
    315 // returns [ 'a', 'd' ]
    316 ```
    317 
    318 #### cli.flags()
    319 
    320 Returns command-line flags.
    321 
    322 ```javascript
    323 var cli = new CLI({
    324     'argv': [
    325         '/usr/local/bin/node',
    326         'foo.js',
    327         'a',
    328         '--b',
    329         'c',
    330         '-def',
    331         '--g=h',
    332         'i'
    333     ]
    334 });
    335 
    336 var flags = cli.flags();
    337 // returns { 'b': 'c', 'd': true, 'e': true, 'f': true, 'g': 'h' }
    338 ```
    339 
    340 #### cli.help( \[code] )
    341 
    342 Prints help text to `stderr` and then exits the calling process.
    343 
    344 ```javascript
    345 var cli = new CLI({
    346     'help': 'Usage: beep [options] <boop>'
    347 });
    348 
    349 cli.help();
    350 // => Usage: beep [options] <boop>
    351 ```
    352 
    353 By default, the process exits with an exit code equal to `0`. To exit with a different exit code, provide a `code` argument.
    354 
    355 #### cli.version()
    356 
    357 Prints the command-line interface version to `stderr` and then exits the calling process.
    358 
    359 ```javascript
    360 var cli = new CLI({
    361     'version': '1.1.1'
    362 });
    363 
    364 cli.version();
    365 // => 1.1.1
    366 ```
    367 
    368 </section>
    369 
    370 <!-- /.usage -->
    371 
    372 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    373 
    374 * * *
    375 
    376 <section class="notes">
    377 
    378 ## Notes
    379 
    380 -   When either `--help` or `--version` command-line flag is set, a command-line interface instance prints the respective value and then exits the calling process.
    381 -   When explicitly setting `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.
    382 
    383 </section>
    384 
    385 <!-- /.notes -->
    386 
    387 <!-- Package usage examples. -->
    388 
    389 * * *
    390 
    391 <section class="examples">
    392 
    393 ## Examples
    394 
    395 <!-- eslint no-undef: "error" -->
    396 
    397 ```javascript
    398 var join = require( 'path' ).join;
    399 var readFileSync = require( '@stdlib/fs/read-file' ).sync;
    400 var CLI = require( '@stdlib/cli/ctor' );
    401 var main = require( './examples/fixtures/main.js' );
    402 
    403 // Load help text:
    404 var fopts = {
    405     'encoding': 'utf8'
    406 };
    407 var help = readFileSync( join( __dirname, 'examples', 'fixtures', 'usage.txt' ), fopts );
    408 
    409 // Set the command-line interface options:
    410 var opts = {
    411     'pkg': require( './package.json' ),
    412     'options': require( './examples/fixtures/opts.json' ),
    413     'help': help,
    414     'title': true,
    415     'updates': true
    416 };
    417 
    418 // Create a new command-line interface:
    419 var cli = new CLI( opts );
    420 
    421 // Run main:
    422 main( 'beep' );
    423 
    424 // Close:
    425 cli.close( 0 );
    426 ```
    427 
    428 </section>
    429 
    430 <!-- /.examples -->
    431 
    432 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    433 
    434 <section class="references">
    435 
    436 </section>
    437 
    438 <!-- /.references -->
    439 
    440 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    441 
    442 <section class="links">
    443 
    444 </section>
    445 
    446 <!-- /.links -->