From ed1d6db73047348430c1836d492bbf018531759b Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 18:52:06 +0200 Subject: [PATCH] add help command-line argument --- README.md | 6 ++++++ json-to-go.js | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 35e85c9..3bf4b8a 100644 --- a/README.md +++ b/README.md @@ -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)). diff --git a/json-to-go.js b/json-to-go.js index 5dc0030..633adea 100644 --- a/json-to-go.js +++ b/json-to-go.js @@ -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 @@ -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) } })