Skip to content

Commit

Permalink
add help command-line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
grische committed Jul 26, 2024
1 parent 3e4991c commit ed1d6db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Contributions are welcome! Open a pull request to fix a bug, or open an issue to
cat sample.json | node json-to-go.js
```

- For more options, check the help page

```sh
node json-to-go.js --help
```

### Credits

JSON-to-Go is brought to you by Matt Holt ([mholt6](https://twitter.com/mholt6)).
Expand Down
20 changes: 20 additions & 0 deletions json-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,22 @@ if (typeof module != 'undefined') {
process.stdout.write(output.go)
}

function printHelp() {
console.log(`\
Usage: node json-to-go.js [OPTION]... [FILE]
Convert json to go file and prints the result on stdout.
Optional arguments:
--help Print this help page
--all-omitempty Make all fields "omitempty" (Default: false)
--examples Add examples to go struct. Currently only works without flatten. (Default: false)
--flatten Flatten go struct (Default: true)
--typename=NAME Use a specific name for typename (Default: "AutoGenerated")
All arguments can be inverted by specifing "--no-...", for example "--no-examples".
`)
}

process.argv.forEach((val, index) => {
if (index < 2)
return
Expand Down Expand Up @@ -559,8 +575,12 @@ if (typeof module != 'undefined') {
case "all-omitempty":
options.allOmitempty = argument.value
break
case "help":
printHelp()
process.exit(0)
default:
console.error(`Unexpected argument ${val} received`)
printHelp()
process.exit(1)
}
})
Expand Down

0 comments on commit ed1d6db

Please sign in to comment.