Do you want simplely shell script input which have formats and sync return?
Do you want a sscanf like function to parse format strings?
scanf is a C like scanf/sscanf module for node.js which can help you with that.
ExampleQuickly startC like formatReturnDirectly returnArray returnJson returnDetect end of inputsscanf
npm install scanf
The project is tested on GitHub Actions across the following platforms:
| OS | Node.js versions |
|---|---|
| Ubuntu | 4.x ~ 22.x |
| Windows | 4.x ~ 22.x |
| macOS | 16.x ~ 22.x (arm64) |
macOS CI only covers Node.js 16+ because the latest macOS runner (
macos-latest) runs on Apple Silicon (arm64), which does not provide prebuilt binaries for older Node.js versions. The Intel-basedmacos-13runner is no longer available.
%d- integer%f- float%s- string%S- string of line%x- hex%a- hex float%o- octal
var scanf = require('scanf');
console.log('Pleas input your name');
var name = scanf('%s');
console.log('Pleas input your age');
var age = scanf('%d');
console.log('your name [%s] type: [%s]', name, typeof name);
console.log('your age [%s] type: [%s]', age, typeof age);REPL
Pleas input your name
> Barack\ Obama
Pleas input your age
> 24
your name [Barack Obama] type: [string]
your age [24] type: [number]
var scanf = require('scanf');
console.log('when are you born? \(Year-month-day\)');
var date = scanf('%d-%d-%d');
console.log('your birthday [%s]', date);REPL
when are you born? (Year-month-day)
> 1990-01-01
your birthday [1990,1,1]
var scanf = require('scanf');
var number = scanf('%d');
console.log('number', number);REPL
>> 2015
number 2015
var scanf = require('scanf');
var result = scanf('%s%d%d');
console.log('result', result);REPL
>> Alan 24 180
result [ 'Alan', 24, 180 ]
var scanf = require('scanf');
var result = scanf('%d %f %s %x %o', 'integer', 'float', 'string', 'hex', 'octal');
console.log('result', result);REPL
>> 12 3.1415926 hello 1F 10
result {
integer: 12,
float: 3.1415926,
string: 'hello',
hex: 31,
octal: 8
}
After calling scanf(), use scanf.isEOF() to check whether the read reached
the end of standard input. This is different from an empty line or input that
does not match the requested format.
var scanf = require('scanf');
while (true) {
var values = scanf('%d %d %d');
if (scanf.isEOF()) {
break;
}
console.log(values[0], values[1], values[2]);
}EOF is reported only after a read attempts to go past the available input. In
a terminal, send EOF with Ctrl+D on macOS/Linux, or Ctrl+Z followed by
Enter on Windows.
REPL
>> var sscanf = require('scanf').sscanf;
undefined
>> sscanf('12 34', '%d');
12
>> sscanf('Alan 20 180', '%s%d%d')
[ 'Alan', 20, 180 ]
>> sscanf('12 3.1415926 hello', '%d %f %s', 'month', 'pi', 'world');
{ month: 12, pi: 3.1415926, world: 'hello' }
>> sscanf(' 14 ?? Ss 0:07.59 /usr/sbin/securityd -i', '%d %s %s %s %s %s', 'pid', 'tty', 'stat', 'time', 'exec', 'param');
{ pid: 14,
tty: '??',
stat: 'Ss',
time: '0:07.59',
exec: '/usr/sbin/securityd',
param: '-i' }you can see the ./tests files for more detail.
If there are some formats not support or go broken, you can contact author with email lellansin@gmail.com.
MIT