Skip to content

Commit bfc29a7

Browse files
committed
add help command-line argument
1 parent 0fa4579 commit bfc29a7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Contributions are welcome! Open a pull request to fix a bug, or open an issue to
2727
cat sample.json | node json-to-go.js
2828
```
2929

30+
- For more options, check the help page
31+
32+
```sh
33+
node json-to-go.js --help
34+
```
35+
3036
### Credits
3137

3238
JSON-to-Go is brought to you by Matt Holt ([mholt6](https://twitter.com/mholt6)).

json-to-go-v2.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,25 @@ if (typeof module === 'undefined' || !module.parent) {
509509
process.stdout.write(output.go)
510510
}
511511

512+
function printHelp() {
513+
const path = require('path');
514+
const scriptname = path.basename(process.argv[1])
515+
516+
console.log(`\
517+
Usage: node ${scriptname} [OPTION]... [FILE]
518+
Convert json to go file and prints the result on stdout.
519+
520+
Optional arguments:
521+
--help Print this help page
522+
--all-omitempty Make all fields "omitempty" (Default: false)
523+
--examples Add examples to go struct. Currently only works without flatten. (Default: false)
524+
--flatten Flatten go struct (Default: true)
525+
--typename=NAME Use a specific name for typename (Default: "AutoGenerated")
526+
527+
All arguments can be inverted by specifing "--no-...", for example "--no-examples".
528+
`)
529+
}
530+
512531
process.argv.forEach((val, index) => {
513532
if (index < 2)
514533
return
@@ -544,8 +563,12 @@ if (typeof module === 'undefined' || !module.parent) {
544563
case "all-omitempty":
545564
options.allOmitempty = argument.value
546565
break
566+
case "help":
567+
printHelp()
568+
process.exit(0)
547569
default:
548570
console.error(`Unexpected argument ${val} received`)
571+
printHelp()
549572
process.exit(1)
550573
}
551574
})

0 commit comments

Comments
 (0)