diff --git a/lib/bin.js b/lib/bin.js index d461304..d6ae72b 100755 --- a/lib/bin.js +++ b/lib/bin.js @@ -11,17 +11,17 @@ import { fromHex as _fromHex, toHex } from './byte-utils.js' function usage (code) { console.error('Usage: cborg ') console.error('Valid commands:') - console.error('\tbin2diag [binary input]') + console.error('\tbin2diag [--width ] [binary input]') console.error('\tbin2hex [binary input]') console.error('\tbin2json [--pretty] [binary input]') console.error('\tdiag2bin [diagnostic input]') console.error('\tdiag2hex [diagnostic input]') console.error('\tdiag2json [--pretty] [diagnostic input]') console.error('\thex2bin [hex input]') - console.error('\thex2diag [hex input]') + console.error('\thex2diag [--width ] [hex input]') console.error('\thex2json [--pretty] [hex input]') console.error('\tjson2bin \'[json input]\'') - console.error('\tjson2diag \'[json input]\'') + console.error('\tjson2diag [--width ] \'[json input]\'') console.error('\tjson2hex \'[json input]\'') console.error('Input may either be supplied as an argument or piped via stdin') process.exit(code || 0) @@ -54,6 +54,20 @@ function argvPretty () { return { argv, pretty } } +function argvWidth () { + const widthIndex = process.argv.findIndex((s) => s === '--width') + if (widthIndex <= 0 || widthIndex + 1 >= process.argv.length) { + return { argv: process.argv.filter((s) => s !== '--width'), width: undefined } + } + const width = parseInt(process.argv[widthIndex + 1], 10) + if (!width || width < 20) { + return { argv: process.argv.filter((s) => s !== '--width'), width: undefined } + } + const argv = process.argv + argv.splice(widthIndex, 2) + return { argv, width } +} + async function run () { const cmd = process.argv[2] @@ -64,8 +78,9 @@ async function run () { case 'bin2diag': { /* c8 ignore next 1 */ - const bin = process.argv.length < 4 ? (await fromStdin()) : new TextEncoder().encode(process.argv[3]) - for (const line of tokensToDiagnostic(bin)) { + const { argv, width } = argvWidth() + const bin = argv.length < 4 ? (await fromStdin()) : new TextEncoder().encode(argv[3]) + for (const line of tokensToDiagnostic(bin, width)) { console.log(line) } return @@ -114,8 +129,9 @@ async function run () { } case 'hex2diag': { - const bin = fromHex(process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3]) - for (const line of tokensToDiagnostic(bin)) { + const { argv, width } = argvWidth() + const bin = fromHex(argv.length < 4 ? (await fromStdin()).toString() : argv[3]) + for (const line of tokensToDiagnostic(bin, width)) { console.log(line) } return @@ -134,9 +150,10 @@ async function run () { } case 'json2diag': { - const inp = process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3] + const { argv, width } = argvWidth() + const inp = argv.length < 4 ? (await fromStdin()).toString() : argv[3] const obj = JSON.parse(inp) - for (const line of tokensToDiagnostic(encode(obj))) { + for (const line of tokensToDiagnostic(encode(obj), width)) { console.log(line) } return