Get stdin as a string or buffer
// example.ts
import { getStdin, getStdinBuffer } from 'https://deno.land/x/get_stdin/mod.ts';
console.log(await getStdin());
//=> 'unicorns'
console.log(await getStdinBuffer());
//=> Uint8Array(8) [
// 117, 110, 105,
// 99, 111, 114,
// 110, 115
// ]
// example.ts
import { getStdinSync, getStdinBufferSync } from 'https://deno.land/x/get_stdin/mod.ts';
console.log(getStdinSync());
//=> 'unicorns'
console.log(await getStdinBufferSync());
//=> Uint8Array(8) [
// 117, 110, 105,
// 99, 111, 114,
// 110, 115
// ]
$ echo unicorns | deno run example.ts
unicorns
// example.ts
import { getStdin } from 'https://deno.land/x/get_stdin/mod.ts';
console.log("¿What's your name?");
const guest = await getStdin();
console.log(`Hellou ${guest || 'Stranger'}`);
//=> 'Hellou Deno'
// example.ts
import { getStdin } from 'https://deno.land/x/get_stdin/mod.ts';
const input = await getStdin({ exitOnEnter: false });
console.log(`Received a bunch of (possibly) multiline text from stdin:\n${input}`);
$ echo lots\nof\nstuff\nhere > example.txt
$ cat example.txt | deno run example.ts
lots
of
stuff
here
Get stdin
as a string
.
Get stdin
as a Buffer
.
Get stdin
as a string
in sync mode.
Get stdin
as a Buffer
in sync mode.
exitOnEnter
(optional) - Iftrue
, stop reading the stdin once a newline char is reached. Defaults totrue
.
- Inspired by get-stdin - Get stdin as a string or buffer