Skip to content

cli: parse json params #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2016
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
4 changes: 3 additions & 1 deletion lib/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var program = require('commander');
var path = require('path');
var bitcorenode = require('..');
var utils = require('../utils');

function main(servicesPath, additionalServices) {
/* jshint maxstatements: 100 */
Expand Down Expand Up @@ -124,7 +125,8 @@ function main(servicesPath, additionalServices) {
program
.command('call <method> [params...]')
.description('Call an API method')
.action(function(method, params) {
.action(function(method, paramsArg) {
var params = utils.parseParamsWithJSON(paramsArg);
var configInfo = findConfig(process.cwd());
if (!configInfo) {
configInfo = defaultConfig();
Expand Down
13 changes: 13 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ if (!utils.isAbsolutePath) {
utils.isAbsolutePath = require('path-is-absolute');
}

utils.parseParamsWithJSON = function parseParamsWithJSON(paramsArg) {
var params = paramsArg.map(function(paramArg) {
var param;
try {
param = JSON.parse(paramArg);
} catch(err) {
param = paramArg;
}
return param;
});
return params;
};

module.exports = utils;
18 changes: 18 additions & 0 deletions test/utils.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,22 @@ describe('Utils', function() {

});

describe('#parseParamsWithJSON', function() {
it('will parse object', function() {
var paramsArg = ['3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou', '{"start": 100, "end": 1}'];
var params = utils.parseParamsWithJSON(paramsArg);
params.should.deep.equal(['3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou', {start: 100, end: 1}]);
});
it('will parse array', function() {
var paramsArg = ['3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou', '[0, 1]'];
var params = utils.parseParamsWithJSON(paramsArg);
params.should.deep.equal(['3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou', [0, 1]]);
});
it('will parse numbers', function() {
var paramsArg = ['3', 0, 'b', '0', 0x12, '0.0001'];
var params = utils.parseParamsWithJSON(paramsArg);
params.should.deep.equal([3, 0, 'b', 0, 0x12, 0.0001]);
});
});

});