Skip to content

Commit

Permalink
[cli] Option -f, --form for output to Query string.
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtree committed Apr 15, 2015
1 parent aa766fc commit da0b2ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down Expand Up @@ -503,6 +504,7 @@ $ uson --h
-i, --input <file> Load data from file
--output <file> 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 <usonrc> Use <usonrc> instead of any .usonrc.js
Expand Down
13 changes: 11 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ program
.option('-j, --json', '"json" mode')
.option('-i, --input <file>', 'Load data from file')
.option(' --output <file>', '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 <usonrc>', 'Use <usonrc> 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() || {};
}
Expand All @@ -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]); }
Expand Down Expand Up @@ -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';
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit da0b2ce

Please sign in to comment.