Skip to content

Commit

Permalink
js cli read file
Browse files Browse the repository at this point in the history
  • Loading branch information
paiv committed Nov 10, 2024
1 parent 8792bd2 commit 1052268
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ jobs:
run: |
sh -c '[ "$(npm exec -- uklatn -t DSTU_9112_B мрії мої)" = "mriji moji" ]'
- name: Test CLI pipe
run: |
sh -c '[ "$(echo мрії мої | npm exec -- uklatn -t DSTU_9112_B -f -)" = "mriji moji" ]'
build-py:
runs-on: ubuntu-latest
Expand Down
44 changes: 28 additions & 16 deletions js/cli.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/usr/bin/env node
import { open } from 'node:fs/promises';
import path from 'node:path';
import * as uklatn from './uklatn.js';


const _Usage = '[-h] [-t TABLE] [-c] [-l] text [text ...]\n';
const _Usage = '[-h] [-t TABLE] [-c] [-l] [-f FILE] [text ...]\n';

const _HelpPage = _Usage + `
arguments:
text text to transliterate
- read text from stdin
options:
-h, --help show this help message and exit
-t, --table {DSTU_9112_A,DSTU_9112_B,KMU_55}
transliteration system (default: DSTU_9112_A)
-l, --lat, --latin convert to Latin script (default)
-c, --cyr, --cyrillic convert to Cyrillic script
-f, --file FILE read text from file
`;


Expand All @@ -31,15 +32,7 @@ function parse_args(argv) {
switch (state) {
case 0:
if (arg[0] === '-') {
if (arg === '-') {
if (args.text) {
args.text.push(arg);
}
else {
args.text = [arg];
}
}
else if (arg === '-h' || arg === '-help' || arg === '--help') {
if (arg === '-h' || arg === '-help' || arg === '--help') {
args.print_help = true;
return args;
}
Expand All @@ -52,6 +45,9 @@ function parse_args(argv) {
else if (arg === '-t' || arg === '--table') {
state = 1;
}
else if (arg === '-f' || arg === '--file') {
state = 2;
}
else {
args.parse_error = `unrecognized arguments: ${arg}`;
return args;
Expand All @@ -77,17 +73,24 @@ function parse_args(argv) {
return args;
}
break;
case 2:
args.file = arg;
state = 0;
break;
}
}

switch (state) {
case 1:
args.parse_error = `argument -t/--table expected table name`;
return args;
case 2:
args.parse_error = `argument -f/--file expected file name`;
return args;
}

if (!args.text || !args.text.length) {
args.parse_error = 'missing required arguments: text';
if ((!args.text || !args.text.length) && (!args.file)) {
args.parse_error = 'missing required arguments: text or file';
return args;
}
return args;
Expand All @@ -113,14 +116,23 @@ async function main(argv) {
tr = uklatn.decode;
}

if (args.text.length === 1 && args.text[0] === '-') {
for await (const buf of process.stdin) {
if (args.file) {
let fp = undefined;
if (args.file === '-') {
fp = process.stdin;
}
else {
const fd = await open(args.file);
fp = fd.createReadStream();
}
for await (const buf of fp) {
const text = buf.toString('utf-8');
const s = tr(text, args.table_name);
process.stdout.write(s);
}
}
else {

if (args.text) {
for (const [i, text] of args.text.entries()) {
let s = tr(text, args.table_name);
if (i) { s = ' ' + s; }
Expand Down

0 comments on commit 1052268

Please sign in to comment.