Skip to content

Commit

Permalink
add all jsonToGo options as program arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
grische committed Jul 26, 2024
1 parent 68b6963 commit 0fa4579
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions json-to-go-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
})
}
}
Expand Down

0 comments on commit 0fa4579

Please sign in to comment.