From 9530d5b5e91c37e055aa98f6a1ad2a5223ab8401 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Fri, 3 Sep 2021 23:30:01 +0300 Subject: [PATCH] pass extra options to httpsnippet (pass through to the target) (#222) Co-authored-by: Eric Reynolds --- README.md | 10 +++++++++- bin/httpsnippet | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cb59c19ba..acb7dd9c3 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ npm install --save httpsnippet ``` - Usage: httpsnippet [options] + Usage: httpsnippet [options] Options: @@ -32,6 +32,7 @@ npm install --save httpsnippet -t, --target target output -c, --client [client] target client library -o, --output write output to directory + -x, --extra [{"optionKey": "optionValue"}] provide extra options for the target/client ``` @@ -63,6 +64,13 @@ snippets/ └── endpoint-3.js ``` +provide extra options: + +```shell +httpsnippet example.json --target http --output ./snippets -x '{"autoHost": false, "autoContentLength": false}' +``` + + ## API ### HTTPSnippet(source) diff --git a/bin/httpsnippet b/bin/httpsnippet index 3747f6695..49410f8e4 100755 --- a/bin/httpsnippet +++ b/bin/httpsnippet @@ -17,12 +17,23 @@ cmd .option('-t, --target ', 'target output') .option('-c, --client [client]', 'target client library') .option('-o, --output ', 'write output to directory') + .option('-x, --extra [{"optionKey": "optionValue"}]', 'provide extra options for the target/client') .parse(process.argv) if (!cmd.args.length || !cmd.target) { cmd.help() } +let extraOptions +if (cmd.extra) { + try { + extraOptions = JSON.parse(cmd.extra) + } catch (e) { + console.error('%s failed to parse options %s (should be JSON)', chalk.red('✖'), chalk.cyan.bold(cmd.extra)) + process.exit() + } +} + let dir if (cmd.output) { dir = path.resolve(cmd.output) @@ -53,7 +64,7 @@ cmd.args.forEach(function (fileName) { }) .then(function (snippet) { - return snippet.convert(cmd.target, cmd.client) + return snippet.convert(cmd.target, cmd.client, extraOptions) }) .then(function (output) {