index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var minimist = require('minimist');
  2. module.exports = function parse (args, opts) {
  3. var level = 0, index;
  4. var args_ = [];
  5. for (var i = 0; i < args.length; i++) {
  6. if (typeof args[i] === 'string' && /^\[/.test(args[i])) {
  7. if (level ++ === 0) {
  8. index = i;
  9. }
  10. }
  11. if (typeof args[i] === 'string' && /\]$/.test(args[i])) {
  12. if (-- level > 0) continue;
  13. var sub = args.slice(index, i + 1);
  14. if (typeof sub[0] === 'string') {
  15. sub[0] = sub[0].replace(/^\[/, '');
  16. }
  17. if (sub[0] === '') sub.shift();
  18. var n = sub.length - 1;
  19. if (typeof sub[n] === 'string') {
  20. sub[n] = sub[n].replace(/\]$/, '');
  21. }
  22. if (sub[n] === '') sub.pop();
  23. args_.push(parse(sub));
  24. }
  25. else if (level === 0) args_.push(args[i]);
  26. }
  27. var argv = minimist(args_, opts);
  28. return argv;
  29. };