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 0fa4579 commit bfc29a7
Show file tree
Hide file tree
Showing 2 changed files with 29 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
23 changes: 23 additions & 0 deletions json-to-go-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,25 @@ if (typeof module === 'undefined' || !module.parent) {
process.stdout.write(output.go)
}

function printHelp() {
const path = require('path');
const scriptname = path.basename(process.argv[1])

console.log(`\
Usage: node ${scriptname} [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 @@ -544,8 +563,12 @@ if (typeof module === 'undefined' || !module.parent) {
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 bfc29a7

Please sign in to comment.