Skip to content

Commit d523615

Browse files
committed
Used minimist parser
1 parent 78f15f4 commit d523615

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

cli-index.js

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,42 @@
22
'use strict';
33

44
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+
});
615

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\
1022
-e, --exact Find an exact match \n\
1123
-d, --declared Find a DOI with a 'doi:' prefix\n\
1224
-m, --match Find all matches within the given string");
1325
process.exit(-1)
1426
}
1527

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])
3829

39-
if (flags.match === true) {
30+
if (argv.m) {
4031
console.log(doi.match(doiRegex()))
41-
} else if (flags.groups === true) {
32+
} else if (argv.g) {
4233
console.log(doiRegex.groups(doi));
4334
} else {
44-
if (flags.exact && flags.declared) {
35+
if (argv.e && argv.d) {
4536
console.log('Is this a declared DOI',
4637
doiRegex.declared({exact: true}).test(doi))
47-
} else if (flags.exact && !flags.declared) {
38+
} else if (argv.e && !argv.d) {
4839
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) {
5041
console.log('Is the DOI declared?', doiRegex.declared().test(doi))
5142
} else {
5243
console.log('Does a DOI exist?', doiRegex().test(doi))

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "doi-regex",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Regular expression for matching DOIs",
55
"main": "index.js",
66
"scripts": {
@@ -40,5 +40,6 @@
4040
"devDependencies": {
4141
"ava": "0.0.4",
4242
"lodash": "^2.4.1"
43+
"minimist": "^1.1.0"
4344
}
4445
}

0 commit comments

Comments
 (0)