From 1052268eba03f492553da20e78fd3944aa6279cd Mon Sep 17 00:00:00 2001 From: Pavlo Ivashkov Date: Sun, 10 Nov 2024 13:54:37 +0200 Subject: [PATCH] js cli read file --- .github/workflows/test-builds.yml | 4 +++ js/cli.js | 44 ++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test-builds.yml b/.github/workflows/test-builds.yml index d0f1c25..ea14b3b 100644 --- a/.github/workflows/test-builds.yml +++ b/.github/workflows/test-builds.yml @@ -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 diff --git a/js/cli.js b/js/cli.js index 5f0dd1a..703b6e9 100755 --- a/js/cli.js +++ b/js/cli.js @@ -1,14 +1,14 @@ #!/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 @@ -16,6 +16,7 @@ options: 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 `; @@ -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; } @@ -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; @@ -77,6 +73,10 @@ function parse_args(argv) { return args; } break; + case 2: + args.file = arg; + state = 0; + break; } } @@ -84,10 +84,13 @@ function parse_args(argv) { 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; @@ -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; }