Skip to content

Commit 1271d2a

Browse files
committed
remove dependency: yargs
1 parent ef0fe96 commit 1271d2a

6 files changed

Lines changed: 160 additions & 148 deletions

File tree

lib/p2jcmd.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,17 @@ const nodeUtil = require("util"),
77

88
const _PRO_TIMER = `${pkInfo.name}@${pkInfo.version} [${pkInfo.homepage}]`;
99

10-
const yargs = require('yargs')
11-
.usage("\n" + _PRO_TIMER + "\n\nUsage: $0 -f|--file [-o|output_dir]")
12-
.alias('v', 'version')
13-
.describe('v', 'Display version.\n')
14-
.alias('h', 'help')
15-
.describe('h', 'Display brief help information.\n')
16-
.alias('f', 'file')
17-
.describe('f', '(required) Full path of input PDF file or a directory to scan for all PDF files. When specifying a PDF file name, it must end with .PDF, otherwise it would be treated as a input directory.\n')
18-
.alias('o', 'output_dir')
19-
.describe('o', '(optional) Full path of output directory, must already exist. Current JSON file in the output folder will be replaced when file name is same.\n')
20-
.alias('s', 'silent')
21-
.describe('s', '(optional) when specified, will only log errors, otherwise verbose.\n')
22-
.alias('t', 'fieldTypes')
23-
.describe('t', '(optional) when specified, will generate .fields.json that includes fields ids and types.\n')
24-
.alias('c', 'content')
25-
.describe('c', '(optional) when specified, will generate .content.txt that includes text content from PDF.\n')
26-
.alias('m', 'merge')
27-
.describe('m', '(optional) when specified, will generate .merged.json that includes auto-merged broken text blocks from PDF (Experimental).\n')
28-
.alias('r', 'stream')
29-
.describe('r', '(optional) when specified, will process and parse with buffer/object transform stream rather than file system (Experimental).\n');
10+
const yargs = require('./p2jcmdarg')
11+
.usage(`\n${_PRO_TIMER}\n\nUsage: ${pkInfo.name} -f|--file [-o|output_dir]`)
12+
.alias('v', 'version', 'Display version.')
13+
.alias('h', 'help', 'Display brief help information.')
14+
.alias('f', 'file', '(required) Full path of input PDF file or a directory to scan for all PDF files.\n\t\t When specifying a PDF file name, it must end with .PDF, otherwise it would be treated as a input directory.')
15+
.alias('o', 'output', '(optional) Full path of output directory, must already exist.\n\t\t Current JSON file in the output folder will be replaced when file name is same.')
16+
.alias('s', 'silent', '(optional) when specified, will only log errors, otherwise verbose.')
17+
.alias('t', 'fieldTypes', '(optional) when specified, will generate .fields.json that includes fields ids and types.')
18+
.alias('c', 'content', '(optional) when specified, will generate .content.txt that includes text content from PDF.')
19+
.alias('m', 'merge', '(optional) when specified, will generate .merged.json that includes auto-merged broken text blocks from PDF.')
20+
.alias('r', 'stream', '(optional) when specified, will process and parse with buffer/object transform stream rather than file system.');
3021

3122
const argv = yargs.argv;
3223
const ONLY_SHOW_VERSION = ('v' in argv);

lib/p2jcmdarg.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
class CLIArgParser {
2+
args = [];
3+
#aliases = {};
4+
5+
#usage = "";
6+
#argv = null;
7+
8+
// constructor
9+
constructor(args) {
10+
if (Array.isArray(args))
11+
this.args = args;
12+
}
13+
14+
usage(usageMsg) {
15+
this.#usage = usageMsg + '\n\nOptions:\n';
16+
return this;
17+
}
18+
19+
alias(key, name, description) {
20+
this.#aliases[key] = {name, description};
21+
return this;
22+
}
23+
24+
showHelp() {
25+
let helpMsg = this.#usage;
26+
for (const [key, value] of Object.entries(this.#aliases)) {
27+
helpMsg += `-${key},--${value.name}\t ${value.description}\n`;
28+
}
29+
console.log(helpMsg);
30+
}
31+
32+
get argv() {
33+
return this.#argv ? this.#argv : this.#parseArgv();
34+
}
35+
36+
static isNumber (x) {
37+
if (typeof x === 'number')
38+
return true;
39+
if (/^0x[0-9a-f]+$/i.test(x))
40+
return true;
41+
return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
42+
}
43+
44+
#setArg(key, val, argv) {
45+
const value = CLIArgParser.isNumber(val) ? Number(val) : val;
46+
this.#setKey(argv, key.split('.'), value);
47+
48+
const aliasKey = (key in this.#aliases) ? [this.#aliases[key].name] : [];
49+
if (aliasKey.length < 1) {
50+
for (const [akey, avalue] of Object.entries(this.#aliases)) {
51+
if (key === avalue.name) {
52+
aliasKey.push(akey);
53+
break;
54+
}
55+
}
56+
}
57+
aliasKey.forEach(x => this.#setKey(argv, x.split('.'), value));
58+
}
59+
60+
#setKey(obj, keys, value) {
61+
let o = obj;
62+
for (let i = 0; i < keys.length-1; i++) {
63+
let key = keys[i];
64+
if (key === '__proto__') return;
65+
if (o[key] === undefined) o[key] = {};
66+
if (o[key] === Object.prototype || o[key] === Number.prototype
67+
|| o[key] === String.prototype) o[key] = {};
68+
if (o[key] === Array.prototype) o[key] = [];
69+
o = o[key];
70+
}
71+
72+
let key = keys[keys.length - 1];
73+
if (key === '__proto__') return;
74+
if (o === Object.prototype || o === Number.prototype
75+
|| o === String.prototype) o = {};
76+
if (o === Array.prototype) o = [];
77+
if (o[key] === undefined) {
78+
o[key] = value;
79+
}
80+
else if (Array.isArray(o[key])) {
81+
o[key].push(value);
82+
}
83+
else {
84+
o[key] = [ o[key], value ];
85+
}
86+
}
87+
88+
#parseArgv() {
89+
let aliases=this.#aliases, args = this.args;
90+
let argv = {};
91+
92+
for (let i = 0; i < args.length; i++) {
93+
let arg = args[i];
94+
95+
if (/^--.+/.test(arg)) {
96+
let key = arg.match(/^--(.+)/)[1];
97+
let next = args[i + 1];
98+
if (next !== undefined && !/^-/.test(next)) {
99+
this.#setArg(key, next, argv);
100+
i++;
101+
}
102+
else if (/^(true|false)$/.test(next)) {
103+
this.#setArg(key, next === 'true', argv);
104+
i++;
105+
}
106+
else {
107+
this.#setArg(key, true, argv);
108+
}
109+
}
110+
else if (/^-[^-]+/.test(arg)) {
111+
let key = arg.slice(-1)[0];
112+
if (key !== '-') {
113+
if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])) {
114+
this.#setArg(key, args[i+1], argv);
115+
i++;
116+
}
117+
else if (args[i+1] && /^(true|false)$/.test(args[i+1])) {
118+
this.#setArg(key, args[i+1] === 'true', argv);
119+
i++;
120+
}
121+
else {
122+
this.#setArg(key, true, argv);
123+
}
124+
}
125+
}
126+
else {
127+
console.warn("Unknow CLI options:", arg);
128+
}
129+
}
130+
131+
this.#argv = argv;
132+
return argv;
133+
}
134+
}
135+
136+
module.exports = new CLIArgParser(process.argv.slice(2));

package-lock.json

Lines changed: 0 additions & 113 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
"scripts": {
3030
"test": "cd ./test && sh p2j.forms.sh",
3131
"test-misc": "cd ./test && sh p2j.one.sh misc . \"Expected: 5 success, 2 exception with stack trace\" ",
32-
"parse": "node --trace-deprecation pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form",
33-
"parse-s": "node --trace-deprecation pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s",
34-
"parse-t": "node --trace-deprecation pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t",
35-
"parse-c": "node --trace-deprecation pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t -c",
36-
"parse-m": "node --trace-deprecation pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t -c -m",
37-
"parse-r": "node --trace-deprecation pdf2json.js -f ./test/pdf/fd/form -o ./test/target/fd/form -t -c -m -r",
38-
"parse-242": "node --trace-deprecation pdf2json.js -f ./test/pdf/misc/i242_testingWithTable.pdf -o ./test/target/misc",
39-
"parse-e": "node --trace-deprecation pdf2json.js -f ./test/pdf/misc/i43_encrypted.pdf -o ./test/target/misc",
40-
"parse-e2": "node --trace-deprecation pdf2json.js -f ./test/pdf/misc/i243_problem_file_anon.pdf -o ./test/target/misc"
32+
"parse": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form",
33+
"parse-s": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s",
34+
"parse-t": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t",
35+
"parse-c": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t -c",
36+
"parse-m": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t -c -m",
37+
"parse-r": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/fd/form -o ./test/target/fd/form -t -c -m -r",
38+
"parse-242": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/misc/i242_testingWithTable.pdf -o ./test/target/misc",
39+
"parse-e": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/misc/i43_encrypted.pdf -o ./test/target/misc",
40+
"parse-e2": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/misc/i243_problem_file_anon.pdf -o ./test/target/misc"
4141
},
4242
"engines": {
4343
"node": ">=14.18.0",
@@ -47,13 +47,11 @@
4747
"pdf2json": "./bin/pdf2json"
4848
},
4949
"dependencies": {
50-
"@xmldom/xmldom": "^0.7.5",
51-
"yargs": "^17.2.1"
50+
"@xmldom/xmldom": "^0.7.5"
5251
},
5352
"devDependencies": {},
5453
"bundledDependencies": [
55-
"@xmldom/xmldom",
56-
"yargs"
54+
"@xmldom/xmldom"
5755
],
5856
"maintainers": [
5957
{

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ In order to support this auto merging capability, text block objects have an add
888888
* More test coverage, 4 more test scripts added, see _package.json_ for details
889889
* Easier access to dictionaries, including color, font face and font style, see Dictionary reference section for details
890890
* Refactor to ES6 class for major entry modules
891-
* Dependencies removed: lodash, async
891+
* Dependencies removed: lodash, async and yargs
892892
* Upgrade to Node v14.18.0 LTSs
893893
894894
### Install on Ubuntu

test/p2j.one.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ echo "-----------------------------------------------------"
1616
echo "Update $AGENCY_NAME PDF"
1717
echo "-----------------------------------------------------"
1818
mkdir -p $OUT_DIR_BASE/$AGENCY_NAME/$FORM_BASE
19-
node --trace-deprecation $PDF2JSON -f $IN_DIR_BASE/$AGENCY_NAME/$FORM_BASE -o $OUT_DIR_BASE/$AGENCY_NAME/$FORM_BASE -s -t -c -m
19+
node --trace-deprecation --trace-warnings $PDF2JSON -f $IN_DIR_BASE/$AGENCY_NAME/$FORM_BASE -o $OUT_DIR_BASE/$AGENCY_NAME/$FORM_BASE -s -t -c -m
2020
# diff -rq $OUT_DIR_BASE$AGENCY_NAME/$FORM_BASE/ $DATA_DIR_BASE$AGENCY_NAME/$FORM_BASE/
2121

2222
echo "-----------------------------------------------------"

0 commit comments

Comments
 (0)