Tofu/scripts/import-recipes/cli/parseCliArguments.js
2026-09-18 01:41:02 +02:00

28 lines
1.2 KiB
JavaScript

const DEFAULT_OUT = 'out'
// Reads the command line. Anything unexpected lands in `problems` rather than
// being ignored: a mistyped flag that silently does nothing is worse than a
// refusal, since the run costs money.
export function parseCliArguments(argv) {
const options = { source: null, out: DEFAULT_OUT, dryRun: false, limit: null, problems: [] }
for (let index = 0; index < argv.length; index += 1) {
const argument = argv[index]
if (argument === '--dry-run') {
options.dryRun = true
} else if (argument === '--source' || argument === '--out') {
index += 1
const value = argv[index]
if (value === undefined || value.startsWith('--')) options.problems.push(`${argument} needs a value`)
else if (argument === '--source') options.source = value
else options.out = value
} else if (argument === '--limit') {
index += 1
const count = Number.parseInt(argv[index] ?? '', 10)
if (Number.isInteger(count) && count > 0) options.limit = count
else options.problems.push('--limit needs a positive whole number')
} else {
options.problems.push(`unknown argument: ${argument}`)
}
}
return options
}