From da0b2cea81cf9abf65d40d1837ebb99688deefd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Str=C3=A1nsk=C3=BD?= Date: Wed, 15 Apr 2015 19:40:16 +0200 Subject: [PATCH] [cli] Option `-f, --form` for output to Query string. --- README.md | 10 ++++++---- bin/cli.js | 13 +++++++++++-- package.json | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0cd26b6..2004a0a 100644 --- a/README.md +++ b/README.md @@ -401,12 +401,13 @@ For `json` mode use option `-j, --json`. If you want prettyfied output, use option `-p, --pretty`. -#### Result format (optional) +#### Result format You can use this output formats: -- JSON (default) -- [YAML](http://yaml.org): `-y, --yaml` -- [MessagePack](http://msgpack.org/): `-m, --msgpack` +- JSON (default): `-j, --json` +- [Query string](http://en.wikipedia.org/wiki/Query_string): `-f, --form` (optional) +- [YAML](http://yaml.org): `-y, --yaml` (optional) +- [MessagePack](http://msgpack.org/): `-m, --msgpack` (optional) For example, this returns YAML in Object mode: ``` @@ -503,6 +504,7 @@ $ uson --h -i, --input Load data from file --output Write output to file -p, --pretty Pretty print output (only JSON) + -f, --form Return output in form query-string -y, --yaml Return output in YAML (optional) -m, --msgpack Return output in msgpack (optional) -u, --usonrc Use instead of any .usonrc.js diff --git a/bin/cli.js b/bin/cli.js index 9e74e5b..d2e6f5b 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -17,15 +17,21 @@ program .option('-j, --json', '"json" mode') .option('-i, --input ', 'Load data from file') .option(' --output ', 'Write output to file') - .option('-p, --pretty', 'Pretty print output (only JSON)') + .option('-f, --form', 'Return output in form query-string (optional)') .option('-y, --yaml', 'Return output in YAML (optional)') .option('-m, --msgpack', 'Return output in msgpack (optional)') + .option('-p, --pretty', 'Pretty print output (only JSON output)') .option('-u, --usonrc ', 'Use instead of any .usonrc.js') .option(' --hex', 'Output in hex encoding') .option(' --base64', 'Output in base64 encoding'); function Runtime(program) { this.program = program; + this.availablePlugins = { + yaml: 'js-yaml', + msgpack: 'msgpack', + form: 'qs' + }; this.plugins = this.loadPlugins() || {}; this.rc = this.loadRc() || {}; } @@ -38,7 +44,7 @@ Runtime.prototype.error = function(str) { Runtime.prototype.loadPlugins = function() { var rt = this; var plugins = {}; - var config = { yaml: 'js-yaml', msgpack: 'msgpack' }; + var config = this.availablePlugins; Object.keys(config).forEach(function(k) { if(rt.program[k]) { try { plugins[k] = require(config[k]); } @@ -82,6 +88,9 @@ Runtime.prototype.parse = function(input) { if(this.program.yaml) { return this.plugins.yaml.dump(output); } + if(this.program.form) { + return this.plugins.form.stringify(output); + } return JSON.stringify(output, null, space)+'\n'; }; diff --git a/package.json b/package.json index 6d17cf4..5db91c7 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "mocha": "^2.2.4", "msgpack": "^0.2.6", "pegjs": "^0.8.0", + "qs": "^2.4.1", "uglify-js": "^2.4.19" }, "spm": {