From 0fa4579c72832b92f26f1812bac5282f4d0fd4af Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 14:15:11 +0200 Subject: [PATCH] add all jsonToGo options as program arguments --- json-to-go-v2.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/json-to-go-v2.js b/json-to-go-v2.js index afe8753..0150fca 100644 --- a/json-to-go-v2.js +++ b/json-to-go-v2.js @@ -498,9 +498,10 @@ function jsonToGo(json, options = {}) { if (typeof module === 'undefined' || !module.parent) { let filename = null + let options = {} - function jsonToGoWithErrorHandling(json) { - const output = jsonToGo(json) + function jsonToGoWithErrorHandling(json, options) { + const output = jsonToGo(json, options) if (output.error) { console.error(output.error) process.exitCode = 1 @@ -517,11 +518,32 @@ if (typeof module === 'undefined' || !module.parent) { return } - const argument = val.replace(/-/g, '') - switch (argument) { + let argument = { + arg: val.split("=")[0].replace(/^-+/, ''), + value: val.split("=")[1] || true, + } + + if (argument.arg.startsWith("no-")) { + argument.arg = argument.arg.replace(/^no-/, '') + argument.value = !argument.value + } + + switch (argument.arg) { case "big": console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`) break + case "typename": + options.typename = argument.value + break + case "flatten": + options.flatten = argument.value + break + case "examples": + options.example = argument.value + break + case "all-omitempty": + options.allOmitempty = argument.value + break default: console.error(`Unexpected argument ${val} received`) process.exit(1) @@ -531,7 +553,7 @@ if (typeof module === 'undefined' || !module.parent) { if (filename) { const fs = require('fs'); const json = fs.readFileSync(filename, 'utf8'); - jsonToGoWithErrorHandling(json) + jsonToGoWithErrorHandling(json, options) } if (!filename) { @@ -541,7 +563,7 @@ if (typeof module === 'undefined' || !module.parent) { }) process.stdin.on('end', function () { const json = Buffer.concat(bufs).toString('utf8') - jsonToGoWithErrorHandling(json) + jsonToGoWithErrorHandling(json, options) }) } }