Skip to content
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

add JSON.parse support for arrayOfX #28

Open
ORESoftware opened this issue Nov 13, 2016 · 1 comment
Open

add JSON.parse support for arrayOfX #28

ORESoftware opened this issue Nov 13, 2016 · 1 comment

Comments

@ORESoftware
Copy link
Contributor

ORESoftware commented Nov 13, 2016

I have this code which might allow me to use JSON strings as parseable option values

the syntax that works is:

--match='["a","b","c"]'

the patchwork code that I have added to my project looks like:

var parser = dashdash.createParser({ options: options });

var opts = parser.parse(process.argv);

console.log('opts before => ', util.inspect(opts));

options.filter(function (opt) {
  return String(opt.type).startsWith('arrayOf');  
}).forEach(function (opt) {

  const n = String(opt.name || opt.names[ 0 ]).replace('-', '_');
  if (n in opts) {
    opts[ n ] = _.flattenDeep(opts[ n ].map(function (item) {
      console.log('item => ', util.inspect(item));
      try {
        return _.flatten([ JSON.parse(item) ]);
      }
      catch (err) {
        return item;
      }
    }));
  }
});

console.log('opts after => ', util.inspect(opts));

but what I get is:

opts before =>  { match_any: [ '["dog","cat"]' ],
  errors_only: false,
  _order: [ { key: 'match_any', value: '["dog","cat"]', from: 'argv' } ],
  _args: [] }
item =>  '["dog","cat"]'
opts after =>  { match_any: [ 'dog', 'cat' ],
  errors_only: false,
  _order: [ { key: 'match_any', value: '["dog","cat"]', from: 'argv' } ],
  _args: [] }

looks like to make sure this goes bug free, that I would need library support for this, because the _order property needs to match I assume.

My best guess is for this type of support is to do:

  {
    names: [ 'match-any' ],
    type: 'arrayOfString',
    useJSON: true,
    help: 'Use this to filter input to match the given JS regex',
  },

if useJSON was true, then JSON.parse would be called for each item

@karfau
Copy link

karfau commented Jan 31, 2020

JSON.parse would allow any content inside the string, be it undefined, null, a number, a single string, an object or an array (of any even mixed values of the above).

Since it's more then trivial to support a custom type json:

dashdash.addOptionType({
    name: 'json',
    takesArg: true,
    helpArg: 'JSON',
    parseArg: JSON.parse
});

or even arrayOfJson

dashdash.addOptionType({
    name: 'arrayOfJson',
    takesArg: true,
    array: true,
    //arrayFlatten: true, // depending on the needs
    helpArg: 'JSON[]',
    parseArg: JSON.parse
});

(Note: This code has not been tested!)

It makes more sense to leave related type/content checking to the specific script that uses dashdash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants