cli-argv-util 1.2.3 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +1 -1
- package/README.md +12 -7
- package/dist/cli-argv-util.d.ts +2 -1
- package/dist/cli-argv-util.js +15 -12
- package/package.json +10 -7
package/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022-
|
|
3
|
+
Copyright (c) 2022-2024 Individual contributors to cli-argv-util
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -23,22 +23,26 @@ Place the following code in your **bin/cli.js** file
|
|
|
23
23
|
import { cliArgvUtil } from 'cli-argv-util';
|
|
24
24
|
|
|
25
25
|
const validFlags = ['cd', 'find', 'no-summary'];
|
|
26
|
-
const cli =
|
|
27
|
-
if (
|
|
28
|
-
throw Error(
|
|
26
|
+
const cli = cliArgvUtil.parse(validFlags);
|
|
27
|
+
if (cli.invalidFlag)
|
|
28
|
+
throw Error(cli.invalidFlagMsg);
|
|
29
|
+
if (cli.flagOn.find)
|
|
30
|
+
console.log('You set the --find CLI flag to:', cli.flagMap.find);
|
|
31
|
+
if (cli.flagOn.noSummary)
|
|
32
|
+
console.log('You enabled the --no-summary CLI option.');
|
|
33
|
+
console.log('You supplied', cli.params.length , 'CLI parameter(s).');
|
|
29
34
|
```
|
|
30
35
|
For a real world example, see: [cli.js](https://github.com/center-key/copy-file-util/blob/main/bin/cli.js)
|
|
31
36
|
|
|
32
37
|
If your CLI tool is named `my-program` and a user runs it like:
|
|
33
38
|
```shell
|
|
34
|
-
$ my-program
|
|
39
|
+
$ my-program about.html --cd=src --no-summary 'Hello World' 777
|
|
35
40
|
```
|
|
36
41
|
the resulting `cli` object will be:
|
|
37
42
|
```javascript
|
|
38
43
|
{
|
|
39
44
|
flagMap: {
|
|
40
|
-
cd:
|
|
41
|
-
noSummary: undefined,
|
|
45
|
+
cd: 'src',
|
|
42
46
|
},
|
|
43
47
|
flagOn: {
|
|
44
48
|
cd: true,
|
|
@@ -47,9 +51,10 @@ the resulting `cli` object will be:
|
|
|
47
51
|
},
|
|
48
52
|
invalidFlag: null,
|
|
49
53
|
invalidFlagMsg: null,
|
|
50
|
-
params: ['
|
|
54
|
+
params: ['about.html', 'Hello World', '777'],
|
|
51
55
|
}
|
|
52
56
|
```
|
|
57
|
+
_**Note:** Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._
|
|
53
58
|
|
|
54
59
|
## C) Results
|
|
55
60
|
The `cliArgvUtil.parse()` returns an object of type `Result`:
|
package/dist/cli-argv-util.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! cli-argv-util v1.2.
|
|
1
|
+
//! cli-argv-util v1.2.5 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
|
|
2
2
|
|
|
3
3
|
export type StringFlagMap = {
|
|
4
4
|
[flag: string]: string | undefined;
|
|
@@ -20,5 +20,6 @@ declare const cliArgvUtil: {
|
|
|
20
20
|
[key: string]: unknown;
|
|
21
21
|
}, posix: string): Buffer;
|
|
22
22
|
readFolder(folder: string): string[];
|
|
23
|
+
unquoteArgs(args: string[]): string[];
|
|
23
24
|
};
|
|
24
25
|
export { cliArgvUtil };
|
package/dist/cli-argv-util.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! cli-argv-util v1.2.
|
|
1
|
+
//! cli-argv-util v1.2.5 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
|
|
2
2
|
|
|
3
3
|
import { execSync } from 'node:child_process';
|
|
4
4
|
import fs from 'fs';
|
|
@@ -8,17 +8,7 @@ const cliArgvUtil = {
|
|
|
8
8
|
const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
|
|
9
9
|
const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
|
|
10
10
|
const toPair = (flag) => flag.replace(/^--/, '').split('=');
|
|
11
|
-
const
|
|
12
|
-
const arg = nextArg.replace(/^'/, '').replace(/'$/, '');
|
|
13
|
-
const last = builder[1].length - 1;
|
|
14
|
-
if (builder[0])
|
|
15
|
-
builder[1][last] = builder[1][last] + ' ' + arg;
|
|
16
|
-
else
|
|
17
|
-
builder[1].push(arg);
|
|
18
|
-
const quoteMode = (/^'/.test(nextArg) || builder[0]) && !/'$/.test(nextArg);
|
|
19
|
-
return [quoteMode, builder[1]];
|
|
20
|
-
};
|
|
21
|
-
const args = process.argv.slice(2).reduce(unquote, [false, []])[1];
|
|
11
|
+
const args = cliArgvUtil.unquoteArgs(process.argv.slice(2));
|
|
22
12
|
const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
|
|
23
13
|
const flagMap = Object.fromEntries(pairs.map(toEntry));
|
|
24
14
|
const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
|
|
@@ -42,5 +32,18 @@ const cliArgvUtil = {
|
|
|
42
32
|
readFolder(folder) {
|
|
43
33
|
return fs.readdirSync(folder, { recursive: true }).map(file => slash(String(file))).sort();
|
|
44
34
|
},
|
|
35
|
+
unquoteArgs(args) {
|
|
36
|
+
const unquote = (builder, nextArg) => {
|
|
37
|
+
const arg = nextArg.replace(/^'/, '').replace(/'$/, '');
|
|
38
|
+
const last = builder[1].length - 1;
|
|
39
|
+
if (builder[0])
|
|
40
|
+
builder[1][last] = builder[1][last] + ' ' + arg;
|
|
41
|
+
else
|
|
42
|
+
builder[1].push(arg);
|
|
43
|
+
const quoteMode = (/^'/.test(nextArg) || builder[0]) && !/'$/.test(nextArg);
|
|
44
|
+
return [quoteMode, builder[1]];
|
|
45
|
+
};
|
|
46
|
+
return args.reduce(unquote, [false, []])[1];
|
|
47
|
+
},
|
|
45
48
|
};
|
|
46
49
|
export { cliArgvUtil };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-argv-util",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"description": "Simple utility to parse command line parameters and flags (arguments vector)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
},
|
|
17
17
|
"./": "./dist/"
|
|
18
18
|
},
|
|
19
|
-
"repository":
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/center-key/cli-argv-util.git"
|
|
22
|
+
},
|
|
20
23
|
"homepage": "https://github.com/center-key/cli-argv-util",
|
|
21
24
|
"bugs": "https://github.com/center-key/cli-argv-util/issues",
|
|
22
25
|
"docs": "https://github.com/center-key/cli-argv-util#readme",
|
|
@@ -77,17 +80,17 @@
|
|
|
77
80
|
"slash": "~5.1"
|
|
78
81
|
},
|
|
79
82
|
"devDependencies": {
|
|
80
|
-
"@types/node": "~20.
|
|
81
|
-
"@typescript-eslint/eslint-plugin": "~6.
|
|
82
|
-
"@typescript-eslint/parser": "~6.
|
|
83
|
+
"@types/node": "~20.10",
|
|
84
|
+
"@typescript-eslint/eslint-plugin": "~6.17",
|
|
85
|
+
"@typescript-eslint/parser": "~6.17",
|
|
83
86
|
"add-dist-header": "~1.3",
|
|
84
87
|
"assert-deep-strict-equal": "~1.1",
|
|
85
88
|
"copy-file-util": "~1.1",
|
|
86
|
-
"eslint": "~8.
|
|
89
|
+
"eslint": "~8.56",
|
|
87
90
|
"jshint": "~2.13",
|
|
88
91
|
"mocha": "~10.2",
|
|
89
92
|
"rimraf": "~5.0",
|
|
90
93
|
"run-scripts-util": "~1.2",
|
|
91
|
-
"typescript": "~5.
|
|
94
|
+
"typescript": "~5.3"
|
|
92
95
|
}
|
|
93
96
|
}
|