Illusionist
Illusionist

Reputation: 5499

Parse JSON like dictionary from command line

I am trying to parse a JSON like dictionary from command line I want to pass something like

--myflags {'foo':'bar', 'foo2':bar2'}

When I use

var argv = require('minimist')(process.argv.slice(2));
console.dir(argv)

It reads the output as

3: --myflags
4: {'
5: foo':
6: 'bar',
7: 'foo2':
8: 'bar2'
9: }

var argv = require('minimist')(process.argv.slice(2)); console.dir(argv)

It reads the values as

 '{\'' }

How do i read the complete dicT?

Upvotes: 0

Views: 92

Answers (1)

elclanrs
elclanrs

Reputation: 94121

JSON uses double quotes and in the shell you have to enclose your string in quotes to treat it as a single word:

--myflags '{"foo": "bar", "foo2": "bar2"}'

If you don't care about it being valid JSON you can use single quotes and enclose it in double quotes in the shell:

--myflags "{'foo': 'baz', 'foo2', 'baz2'}"

For completeness, you can also escape double quotes inside double quotes.

Upvotes: 1

Related Questions