Projekt

Obecné

Profil

Stáhnout (1.34 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
#!/usr/bin/env node
2
(function() {
3
4
  var fs = require('fs');
5
  var parse = require('../parser').parse;
6
  var jsesc = require('jsesc');
7
  var regexes = process.argv.splice(2);
8
  var first = regexes[0];
9
  var data;
10
  var log = console.log;
11
  var main = function() {
12
    if (/^(?:-h|--help|undefined)$/.test(first)) {
13
      log([
14
        '\nUsage:\n',
15
        '\tregjsparser [regex ...]',
16
        '\tregjsparser [-h | --help]',
17
        '\nExamples:\n',
18
        '\tregjsparser \'^foo.bar$\'',
19
        '\tregjsparser \'[a-zA-Z0-9]\''
20
      ].join('\n'));
21
      return process.exit(1);
22
    }
23
24
    regexes.forEach(function(snippet) {
25
      var result;
26
      try {
27
        result = parse(snippet);
28
        log(jsesc(result, {
29
          'json': true,
30
          'compact': false,
31
          'indent': '\t'
32
        }));
33
      } catch(error) {
34
        log(error.message + '\n');
35
        log('Error: failed to parse. Make sure the regular expression is valid.');
36
        log('If you think this is a bug in regjsparser, please report it:');
37
        log('\thttps://github.com/jviereck/regjsparser/issues/new');
38
        log('\nStack trace:\n');
39
        log(error.stack);
40
        return process.exit(1);
41
      }
42
    });
43
    // Return with exit status 0 outside of the `forEach` loop, in case
44
    // multiple regular expressions were passed in.
45
    return process.exit(0);
46
  };
47
48
  main();
49
50
}());