|
| 1 | +import ansiStyles from 'ansi-styles' |
| 2 | +import wrapAnsi from 'wrap-ansi' |
| 3 | +import { Logger } from '../logger/logger.mjs' |
| 4 | + |
| 5 | +const PRETTY_PRINT_TERMINAL_WIDTH = process.stdout.columns |
| 6 | +const PRETTY_PRINT_TITLE_WIDTH = 24 |
| 7 | + |
| 8 | +const logger = new Logger('cli') |
| 9 | + |
| 10 | +export interface cliOptions { |
| 11 | + '--help'?: string |
| 12 | + '--log-level'?: string |
| 13 | + '--log-stack'?: string |
| 14 | + '--log-colorful'?: string |
| 15 | + '--log-stringify'?: string |
| 16 | +} |
| 17 | + |
| 18 | +// adhere to this and the manual writes itself |
| 19 | +interface CommandLineArg { |
| 20 | + argument: keyof cliOptions |
| 21 | + alias?: string |
| 22 | + description: string |
| 23 | + type?: string |
| 24 | + default?: string |
| 25 | + // optional evaluator - default simply returns val |
| 26 | + evaluator?: (val?: string) => string | undefined |
| 27 | +} |
| 28 | + |
| 29 | +// known command line arguments - the order here defines order of evaluation |
| 30 | +const commandLineArgs: CommandLineArg[] = [ |
| 31 | + { |
| 32 | + argument: '--help', |
| 33 | + alias: '-h', |
| 34 | + description: 'Print this message.', |
| 35 | + evaluator: () => { |
| 36 | + // extract the argument manual from argument definition |
| 37 | + commandLineArgs.forEach((cla) => { |
| 38 | + let title = cla.argument |
| 39 | + if (cla.alias) title += ', ' + cla.alias |
| 40 | + prettyPrintArgDef({ |
| 41 | + [title]: cla.description, |
| 42 | + ...(cla.type ? { type: cla.type } : {}), |
| 43 | + ...(cla.default ? { default: cla.default } : {}), |
| 44 | + }) |
| 45 | + }) |
| 46 | + return undefined |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + argument: '--log-level', |
| 51 | + description: 'Set log output level.', |
| 52 | + type: 'ALL | TRACE | DEBUG | INFO | WARN | ERROR | FATAL | OFF', |
| 53 | + default: 'INFO', |
| 54 | + }, |
| 55 | + { |
| 56 | + argument: '--log-stack', |
| 57 | + description: 'Include logging source (method and file path) in log.', |
| 58 | + type: 'false | <any other value>', |
| 59 | + default: 'false', |
| 60 | + evaluator: (val) => (val === 'false' ? 'false' : 'true'), |
| 61 | + }, |
| 62 | + { |
| 63 | + argument: '--log-colorful', |
| 64 | + description: 'Enable colorful terminal output for log.', |
| 65 | + type: 'false | <any other value>', |
| 66 | + default: 'false', |
| 67 | + evaluator: (val) => (val === 'false' ? 'false' : 'true'), |
| 68 | + }, |
| 69 | + { |
| 70 | + argument: '--log-stringify', |
| 71 | + description: 'Output log messages as stringified JSON.', |
| 72 | + type: 'false | <any other value>', |
| 73 | + default: 'false', |
| 74 | + evaluator: (val) => (val === 'false' ? 'false' : 'true'), |
| 75 | + }, |
| 76 | +] as const |
| 77 | + |
| 78 | +// read command line arguments and pass them to their respective evaluators |
| 79 | +export function parseCommandLine() { |
| 80 | + // turn into map first i.o.t. remove duplicates and let the order be defined |
| 81 | + // by consumer (that is, this file) |
| 82 | + const args = new Map( |
| 83 | + process.argv.slice(2).map((arg) => { |
| 84 | + const [key, value] = arg.split('=') |
| 85 | + return [key, value] as const |
| 86 | + }), |
| 87 | + ) |
| 88 | + const options: cliOptions = {} |
| 89 | + commandLineArgs.forEach((cla) => { |
| 90 | + let has = false |
| 91 | + let val: string | undefined |
| 92 | + // check for argument (full) first |
| 93 | + if (args.has(cla.argument)) { |
| 94 | + has = true |
| 95 | + val = args.get(cla.argument) |
| 96 | + } |
| 97 | + //... and only then check for alias. Otherwise de-duplicating could be circumvented. |
| 98 | + else if (cla.alias && args.has(cla.alias)) { |
| 99 | + has = true |
| 100 | + val = args.get(cla.alias) |
| 101 | + } |
| 102 | + if (has) { |
| 103 | + // evaluate |
| 104 | + if (cla.evaluator) { |
| 105 | + options[cla.argument] = cla.evaluator(val) |
| 106 | + } else { |
| 107 | + options[cla.argument] = val |
| 108 | + } |
| 109 | + // dispose |
| 110 | + args.delete(cla.argument) |
| 111 | + if (cla.alias) args.delete(cla.alias) |
| 112 | + } |
| 113 | + }) |
| 114 | + // if args are left (undeleted) it means undefined args were given |
| 115 | + Array.from(args.keys()).forEach((key) => { |
| 116 | + logger.error('Invalid command line argument', key) |
| 117 | + }) |
| 118 | + return options |
| 119 | +} |
| 120 | + |
| 121 | +function prettyPrintArgDef(def: Record<string, string>) { |
| 122 | + // minus 2 for the space between title and definition |
| 123 | + const maxTextWidth = Math.max(PRETTY_PRINT_TERMINAL_WIDTH - PRETTY_PRINT_TITLE_WIDTH - 2, 24) |
| 124 | + Object.entries(def).forEach(([key, value], idx) => { |
| 125 | + const textLines = wrapAnsi(value, maxTextWidth, { hard: true }).split('\n') |
| 126 | + // for first pair (argument), color title |
| 127 | + const colorStart = idx == 0 ? ansiStyles.color.blue.open : '' |
| 128 | + const colorEnd = idx == 0 ? ansiStyles.reset.close : '' |
| 129 | + // print property definition |
| 130 | + console.log(`${colorStart}${key.padStart(PRETTY_PRINT_TITLE_WIDTH)}${colorEnd} ${textLines[0]}`) |
| 131 | + textLines.slice(1).forEach((line) => { |
| 132 | + console.log(`${' '.padStart(PRETTY_PRINT_TITLE_WIDTH)} ${line}`) |
| 133 | + }) |
| 134 | + }) |
| 135 | + // terminate with blank line |
| 136 | + console.log() |
| 137 | +} |
0 commit comments