Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm install --save httpsnippet

```

Usage: httpsnippet [options] <file>
Usage: httpsnippet [options] <files ...>

Options:

Expand All @@ -32,6 +32,7 @@ npm install --save httpsnippet
-t, --target <target> target output
-c, --client [client] target client library
-o, --output <directory> write output to directory
-x, --extra [{"optionKey": "optionValue"}] provide extra options for the target/client

```

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion bin/httpsnippet
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ cmd
.option('-t, --target <target>', 'target output')
.option('-c, --client [client]', 'target client library')
.option('-o, --output <directory>', 'write output to directory')
.option('-x, --extra [{"optionKey": "optionValue"}]', 'provide extra options for the target/client')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on having this so that any argument not strictly defined on the CLI would be interpreted as an option for the targets and be automatically bundled up into extraOptions? Like:

httpsnippet example.json --target http --output ./snippets --autoHost false --autoContentLength false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had a thought about it too. But it'll take a lot more effort to handle it and parse correctly. Is there only true/false, or other options are available like strings or maybe even arrays? We'll need to parse each option and convert to to the correct type. Is it worth it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erunion what does your '+1' mean? is it "ok, really no need to spend time on parsing, passing JSON config is ok", or "yeah you better work on passing options in --autoHost false --autoContentLength false format"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's "that makes sense, leaving it as it is and passing JSON is sounds good to me!"

.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)
Expand Down Expand Up @@ -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) {
Expand Down