|
2 | 2 | 'use strict';
|
3 | 3 |
|
4 | 4 | var doiRegex = require('./')
|
5 |
| -var _ = require('lodash') |
| 5 | +var argv = require('minimist')(process.argv.slice(2), { |
| 6 | + alias: { |
| 7 | + e: 'exact', |
| 8 | + d: 'declared', |
| 9 | + m: 'match', |
| 10 | + g: 'groups', |
| 11 | + h: 'help' |
| 12 | + }, |
| 13 | + boolean: ['e', 'd', 'm', 'g'] |
| 14 | +}); |
6 | 15 |
|
7 |
| -if (process.argv.length <= 2) { |
8 |
| - console.error("Usage: " + process.argv[1] + " <doi>") |
9 |
| - console.error("Flags: \n\ |
| 16 | +console.log(argv); |
| 17 | + |
| 18 | +if (argv.h) { |
| 19 | + console.error( |
| 20 | + "Usage: " + process.argv[1] + " <doi> \n\ |
| 21 | + Options: \n\ |
10 | 22 | -e, --exact Find an exact match \n\
|
11 | 23 | -d, --declared Find a DOI with a 'doi:' prefix\n\
|
12 | 24 | -m, --match Find all matches within the given string");
|
13 | 25 | process.exit(-1)
|
14 | 26 | }
|
15 | 27 |
|
16 |
| -// parse input |
17 |
| -var flags = { |
18 |
| - "exact": ['-e', '--exact'], |
19 |
| - "declared": ['-d', '--declared'], |
20 |
| - "match": ['-m', '--match'], |
21 |
| - "groups": ['-g', '--groups'] |
22 |
| -}; |
23 |
| - |
24 |
| -for (var index in flags) { |
25 |
| - _(flags[index]).each(function(arg) { |
26 |
| - if (process.argv.indexOf(arg) != -1) { |
27 |
| - process.argv.splice(process.argv.indexOf(arg), 1) |
28 |
| - flags[index] = true |
29 |
| - } |
30 |
| - }) |
31 |
| - |
32 |
| - if (flags[index] !== true) { |
33 |
| - flags[index] = null |
34 |
| - } |
35 |
| -} |
36 |
| - |
37 |
| -var doi = process.argv[2]; |
| 28 | +var doi = (argv.doi || argv._[0]) |
38 | 29 |
|
39 |
| -if (flags.match === true) { |
| 30 | +if (argv.m) { |
40 | 31 | console.log(doi.match(doiRegex()))
|
41 |
| -} else if (flags.groups === true) { |
| 32 | +} else if (argv.g) { |
42 | 33 | console.log(doiRegex.groups(doi));
|
43 | 34 | } else {
|
44 |
| - if (flags.exact && flags.declared) { |
| 35 | + if (argv.e && argv.d) { |
45 | 36 | console.log('Is this a declared DOI',
|
46 | 37 | doiRegex.declared({exact: true}).test(doi))
|
47 |
| - } else if (flags.exact && !flags.declared) { |
| 38 | + } else if (argv.e && !argv.d) { |
48 | 39 | console.log('Is this a DOI?', doiRegex({exact: true}).test(doi))
|
49 |
| - } else if (!flags.exact && flags.declared) { |
| 40 | + } else if (!argv.e && argv.d) { |
50 | 41 | console.log('Is the DOI declared?', doiRegex.declared().test(doi))
|
51 | 42 | } else {
|
52 | 43 | console.log('Does a DOI exist?', doiRegex().test(doi))
|
|
0 commit comments