From bfc29a7db4c7146bbdf1296615c829c28c1b9d72 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-v2.js | 23 +++++++++++++++++++++++ 2 files changed, 29 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-v2.js b/json-to-go-v2.js index 0150fca..eb2d551 100644 --- a/json-to-go-v2.js +++ b/json-to-go-v2.js @@ -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 @@ -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) } })