-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli-options.js
More file actions
70 lines (67 loc) · 2.48 KB
/
Copy pathcli-options.js
File metadata and controls
70 lines (67 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { SUPPORTED_FORMATS } from '@anywaydata/core';
export function parseCliOptions(argvInput = process.argv) {
const parsed = yargs(hideBin(argvInput))
.usage('Usage: anywaydata generate -i input.txt -n 10 -f csv [-o output.txt]')
.command('generate', 'Generate test data from an input spec')
.option('i', { alias: 'inputfile', type: 'string', demandOption: true, describe: 'Text file with a data spec' })
.option('n', {
alias: 'numberOfLines',
type: 'number',
demandOption: true,
default: 1,
describe: 'Number of lines to generate',
})
.option('f', {
alias: 'format',
type: 'string',
default: 'csv',
describe: `Output format e.g. ${SUPPORTED_FORMATS.join(',')}`,
})
.option('o', { alias: 'outputfile', type: 'string', describe: 'Output file' })
.option('t', {
alias: 'testMode',
type: 'boolean',
default: false,
describe: 'Generate one line, report diagnostics and include example output',
})
.option('show-progress', {
type: 'boolean',
default: undefined,
describe: 'Show progress logs even when writing to file',
})
.option('stream', {
type: 'boolean',
default: false,
describe: 'Use streaming generation path when available (exporting CSV or JSONL)',
})
.option('stream-threshold', {
type: 'number',
default: 5000,
describe: 'Auto-enable stream mode when rows >= threshold and output file is provided',
})
.option('unsafe-faker-expressions', {
type: 'boolean',
default: false,
describe: 'Allow expression-style faker arguments (unsafe for untrusted input)',
})
.help('h')
.alias('h', 'help')
.parseSync();
const rowCount = parsed.testMode ? 1 : parsed.numberOfLines;
const streamThreshold = Number.isFinite(parsed.streamThreshold) ? Math.max(0, parsed.streamThreshold) : 5000;
const shouldStream = parsed.stream === true || (!!parsed.outputfile && rowCount >= streamThreshold);
const showProgress =
parsed.showProgress !== undefined ? parsed.showProgress === true : parsed.testMode || !parsed.outputfile;
return {
inputFile: parsed.inputfile,
outputFile: parsed.outputfile,
format: String(parsed.format || 'csv').toLowerCase(),
rowCount,
testMode: parsed.testMode === true,
showProgress,
shouldStream,
unsafeFakerExpressions: parsed['unsafe-faker-expressions'] === true,
};
}