JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 43599
  • Score
    100M100P100Q146099F

Node.js module for standard input/output management

Package Exports

  • stdio

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stdio) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Module for input/output management with nodejs.

Build Status

Installation

To install the most recent release from npm, run:

npm install stdio

Usage

You can do many things with this module

Read standard input at once

var stdio = require('stdio');
stdio.read(function(data){
    console.log(data);
});

Parse Unix-like command line options

var stdio = require('stdio');
var ops = stdio.getopt({
    'check': {key: 'c', args: 2},
    'map': {key: 'm'},
    'kaka': {key: 'k', args: 2},
    'ooo': {key: 'o'}
});
console.log(ops);

If you run the previous example with the command

node pruebas.js -c 23 45 88 --map -k 23 44 cosa

Program output will be:

{ check: { args: [ '23', '45' ] },
  args: [ '88', 'cosa' ],
  map: true,
  kaka: { args: [ '23', '44' ] } }

So you can check options:

if(ops.map){
    // Your action
}
if(ops.kaka){
    // Your action, using ops.kaka[0] or ops.kaka[1] or...
}

This module can generate an usage message automatically. You can use it when user specifies --help option. This code:

var stdio = require('stdio');

var ops = stdio.getopt({
    una: {description: 'Sets something to some value', key: 'u', args: 2},
    otra_muy_larga: {description: 'A boolean flag', key: 'o'},
    una_sin_desc: {description: 'Another boolean flag'},
    ultima: {description: 'A description', key: 'u', args: 1}
});

// Print help
ops.printHelp({
    description: 'Description of this simple program',
    usage: 'node something.js [OPTIONS] ... FILE'
});

Will produce the following output:

USAGE: node something.js [OPTIONS] ... FILE
Description of this simple program
  -u, --una <ARG1> <ARG2> 	Sets something to some value
  -o, --otra_muy_larga    	A boolean flag
  --una_sin_desc          	Another boolean flag
  -u, --ultima <ARG1>     	A description

Printf-like output

This simple line:

stdio.printf('example %d: %s is %j\n', 2, 'any', {a: 2, b: [0, 2, 8], c: 'str'});

will produce the following output:

example 2: any is {"a":2,"b":[0,2,8],"c":"str"}

You can use %s for strings, %d for numbers (integer or floating-point ones), and %j for JSON objects.