feat(phoenix): Add --dump and --file options to sed

This commit is contained in:
Sam Atkins 2024-05-31 11:15:37 +01:00
parent 4067c82486
commit f250f86446

View File

@ -32,7 +32,13 @@ export default {
args: { args: {
$: 'simple-parser', $: 'simple-parser',
allowPositionals: true, allowPositionals: true,
tokens: true,
options: { options: {
dump: {
description: 'Dump a representation of the parsed script, for debugging.',
type: 'boolean',
default: false,
},
expression: { expression: {
description: 'Specify an additional script to execute. May be specified multiple times.', description: 'Specify an additional script to execute. May be specified multiple times.',
type: 'string', type: 'string',
@ -40,6 +46,13 @@ export default {
multiple: true, multiple: true,
default: [], default: [],
}, },
file: {
description: 'Specify a script file to execute. May be specified multiple times.',
type: 'string',
short: 'f',
multiple: true,
default: [],
},
quiet: { quiet: {
description: 'Suppress default printing of selected lines.', description: 'Suppress default printing of selected lines.',
type: 'boolean', type: 'boolean',
@ -50,7 +63,7 @@ export default {
}, },
execute: async ctx => { execute: async ctx => {
const { out, err } = ctx.externs; const { out, err } = ctx.externs;
const { positionals, values } = ctx.locals; const { positionals, values, tokens } = ctx.locals;
if (positionals.length < 1) { if (positionals.length < 1) {
await err.write('sed: No inputs given\n'); await err.write('sed: No inputs given\n');
@ -62,16 +75,34 @@ export default {
// made, if the previous addition (if any) was from a -e option, a <newline> shall be inserted before the new // made, if the previous addition (if any) was from a -e option, a <newline> shall be inserted before the new
// addition. The resulting script shall have the same properties as the script operand, described in the // addition. The resulting script shall have the same properties as the script operand, described in the
// OPERANDS section." // OPERANDS section."
// TODO: -f loads scripts from a file
let scriptString = ''; let scriptString = '';
if (values.expression.length > 0) { if (values.expression.length + values.file.length > 0) {
scriptString = values.expression.join('\n'); // These have to be in order, and -e and -f could be intermixed, so iterate the tokens
for (let token of tokens) {
if (token.kind !== 'option') continue;
if (token.name === 'expression') {
scriptString += token.value + '\n';
continue;
}
if (token.name === 'file') {
for await (const line of fileLines(ctx, token.value)) {
scriptString += line;
}
continue;
}
}
} else { } else {
scriptString = positionals.shift(); scriptString = positionals.shift();
} }
const script = parseScript(scriptString); try {
await out.write(script.dump()); const script = parseScript(scriptString, values);
await script.run(ctx); if (values.dump)
await out.write(script.dump());
await script.run(ctx);
} catch (e) {
console.error(e);
await err.write(`sed: ${e.message}\n`);
}
} }
}; };