cli-argv-util 0.0.1
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 +21 -0
- package/README.md +85 -0
- package/dist/cli-argv-util.d.ts +19 -0
- package/dist/cli-argv-util.js +24 -0
- package/dist/cli-argv-util.umd.cjs +37 -0
- package/package.json +89 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Individual contributors to cli-argv-util
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# cli-argv-util
|
|
2
|
+
<img src=https://centerkey.com/graphics/center-key-logo.svg align=right width=200 alt=logo>
|
|
3
|
+
|
|
4
|
+
_Simple utility to parse command line parameters and flags (arguments vector)_
|
|
5
|
+
|
|
6
|
+
[](https://github.com/center-key/cli-argv-util/blob/main/LICENSE.txt)
|
|
7
|
+
[](https://www.npmjs.com/package/cli-argv-util)
|
|
8
|
+
[](https://snyk.io/test/github/center-key/cli-argv-util)
|
|
9
|
+
[](https://github.com/center-key/cli-argv-util/actions/workflows/run-spec-on-push.yaml)
|
|
10
|
+
|
|
11
|
+
**cli-argv-util** is called from your `bin/cli.js` file in order to read user
|
|
12
|
+
supplied information on the command line and return the flags and parameters
|
|
13
|
+
in an easy-to-use structure.
|
|
14
|
+
|
|
15
|
+
## A) Setup
|
|
16
|
+
Install package for node:
|
|
17
|
+
```shell
|
|
18
|
+
$ npm install cli-argv-util
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## B) Usage
|
|
22
|
+
Enter the following code in your **bin/cli.js** file
|
|
23
|
+
```javascript
|
|
24
|
+
import { cliArgvUtil } from 'cli-argv-util';
|
|
25
|
+
|
|
26
|
+
const validFlags = ['cd', 'find', 'no-summary'];
|
|
27
|
+
const cli = cliArgvUtil.parse(validFlags);
|
|
28
|
+
if (cliArgvUtil.invalidFlag)
|
|
29
|
+
throw Error(cliArgvUtil.invalidFlagMsg);
|
|
30
|
+
```
|
|
31
|
+
For a real world example, see: [cli.js](https://github.com/center-key/copy-file-util/blob/main/bin/cli.js)
|
|
32
|
+
|
|
33
|
+
If your CLI tool is named `my-program` and a user runs it like:
|
|
34
|
+
```shell
|
|
35
|
+
$ my-program file.html --cd=src --no-summary file.png
|
|
36
|
+
```
|
|
37
|
+
the resulting `cli` object will be:
|
|
38
|
+
```javascript
|
|
39
|
+
{
|
|
40
|
+
flagMap: {
|
|
41
|
+
cd: 'src',
|
|
42
|
+
noSummary: undefined,
|
|
43
|
+
},
|
|
44
|
+
flagOn: {
|
|
45
|
+
cd: true,
|
|
46
|
+
find: false,
|
|
47
|
+
noSummary: true,
|
|
48
|
+
},
|
|
49
|
+
invalidFlag: null,
|
|
50
|
+
invalidFlagMsg: null,
|
|
51
|
+
params: ['file.html', 'file.png'],
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## C) Results
|
|
56
|
+
The `cliArgvUtil.parse()` returns an object of type `Result`:
|
|
57
|
+
```typescript
|
|
58
|
+
export type StringFlagMap = { [flag: string]: string | undefined };
|
|
59
|
+
export type BooleanFlagMap = { [flag: string]: boolean };
|
|
60
|
+
export type Result = {
|
|
61
|
+
flagMap: StringFlagMap, //map of flag values for each user supplied flag
|
|
62
|
+
flagOn: BooleanFlagMap, //map of the enabled status for all valid flags
|
|
63
|
+
invalidFlag: string | null, //name of the first invalid flag
|
|
64
|
+
invalidFlagMsg: string | null, //error message for the invalid flag
|
|
65
|
+
params: string[], //array of parameter values supplied by the user
|
|
66
|
+
};
|
|
67
|
+
```
|
|
68
|
+
See the **TypeScript Declarations** at the top of [cli-argv-util.ts](cli-argv-util.ts) for documentation.
|
|
69
|
+
|
|
70
|
+
<br>
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
**CLI Build Tools**
|
|
74
|
+
- 🎋 [add-dist-header](https://github.com/center-key/add-dist-header): _Prepend a one-line banner comment (with license notice) to distribution files_
|
|
75
|
+
- 📄 [cli-argv-util](https://github.com/center-key/cli-argv-util): _Copy or rename a file with optional package version number_
|
|
76
|
+
- 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util): _Recursively copy files from one folder to another folder_
|
|
77
|
+
- 🔍 [replacer-util](https://github.com/center-key/replacer-util): _Find and replace strings or template outputs in text files_
|
|
78
|
+
- 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets): _Revision web asset filenames with cache busting content hash fingerprints_
|
|
79
|
+
- 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util): _Organize npm scripts into named groups of easy to manage commands_
|
|
80
|
+
- 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator): _Check the markup validity of HTML files using the W3C validator_
|
|
81
|
+
|
|
82
|
+
Feel free to submit questions at:<br>
|
|
83
|
+
[github.com/center-key/cli-argv-util/issues](https://github.com/center-key/cli-argv-util/issues)
|
|
84
|
+
|
|
85
|
+
[MIT License](LICENSE.txt)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//! cli-argv-util v0.0.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
|
|
2
|
+
|
|
3
|
+
export type StringFlagMap = {
|
|
4
|
+
[flag: string]: string | undefined;
|
|
5
|
+
};
|
|
6
|
+
export type BooleanFlagMap = {
|
|
7
|
+
[flag: string]: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type Result = {
|
|
10
|
+
flagMap: StringFlagMap;
|
|
11
|
+
flagOn: BooleanFlagMap;
|
|
12
|
+
invalidFlag: string | null;
|
|
13
|
+
invalidFlagMsg: string | null;
|
|
14
|
+
params: string[];
|
|
15
|
+
};
|
|
16
|
+
declare const cliArgvUtil: {
|
|
17
|
+
parse(validFlags: string[]): Result;
|
|
18
|
+
};
|
|
19
|
+
export { cliArgvUtil };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//! cli-argv-util v0.0.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
|
|
2
|
+
|
|
3
|
+
const cliArgvUtil = {
|
|
4
|
+
parse(validFlags) {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
|
|
7
|
+
const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
|
|
8
|
+
const toPair = (flag) => flag.replace(/^--/, '').split('=');
|
|
9
|
+
const args = process.argv.slice(2);
|
|
10
|
+
const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
|
|
11
|
+
const flagMap = Object.fromEntries(pairs.map(toEntry));
|
|
12
|
+
const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
|
|
13
|
+
const invalidFlag = (_b = (_a = pairs.find(pair => !validFlags.includes(pair[0]))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
|
|
14
|
+
const helpMsg = '\nValid flags are --' + validFlags.join(' --');
|
|
15
|
+
return {
|
|
16
|
+
flagMap: flagMap,
|
|
17
|
+
flagOn: Object.fromEntries(onEntries),
|
|
18
|
+
invalidFlag: invalidFlag,
|
|
19
|
+
invalidFlagMsg: invalidFlag ? 'Invalid flag: --' + invalidFlag + helpMsg : null,
|
|
20
|
+
params: args.filter((arg) => !/^--/.test(arg)),
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
export { cliArgvUtil };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//! cli-argv-util v0.0.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
|
|
2
|
+
|
|
3
|
+
(function (factory) {
|
|
4
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
5
|
+
var v = factory(require, exports);
|
|
6
|
+
if (v !== undefined) module.exports = v;
|
|
7
|
+
}
|
|
8
|
+
else if (typeof define === "function" && define.amd) {
|
|
9
|
+
define(["require", "exports"], factory);
|
|
10
|
+
}
|
|
11
|
+
})(function (require, exports) {
|
|
12
|
+
"use strict";
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.cliArgvUtil = void 0;
|
|
15
|
+
const cliArgvUtil = {
|
|
16
|
+
parse(validFlags) {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
|
|
19
|
+
const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
|
|
20
|
+
const toPair = (flag) => flag.replace(/^--/, '').split('=');
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
|
|
23
|
+
const flagMap = Object.fromEntries(pairs.map(toEntry));
|
|
24
|
+
const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
|
|
25
|
+
const invalidFlag = (_b = (_a = pairs.find(pair => !validFlags.includes(pair[0]))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
|
|
26
|
+
const helpMsg = '\nValid flags are --' + validFlags.join(' --');
|
|
27
|
+
return {
|
|
28
|
+
flagMap: flagMap,
|
|
29
|
+
flagOn: Object.fromEntries(onEntries),
|
|
30
|
+
invalidFlag: invalidFlag,
|
|
31
|
+
invalidFlagMsg: invalidFlag ? 'Invalid flag: --' + invalidFlag + helpMsg : null,
|
|
32
|
+
params: args.filter((arg) => !/^--/.test(arg)),
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
exports.cliArgvUtil = cliArgvUtil;
|
|
37
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cli-argv-util",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Simple utility to parse command line parameters and flags (arguments vector)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"module": "dist/cli-argv-util.js",
|
|
8
|
+
"main": "dist/cli-argv-util.umd.cjs",
|
|
9
|
+
"types": "dist/cli-argv-util.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/cli-argv-util.js",
|
|
16
|
+
"require": "./dist/cli-argv-util.umd.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./": "./dist/"
|
|
19
|
+
},
|
|
20
|
+
"repository": "github:center-key/cli-argv-util",
|
|
21
|
+
"homepage": "https://github.com/center-key/cli-argv-util",
|
|
22
|
+
"bugs": "https://github.com/center-key/cli-argv-util/issues",
|
|
23
|
+
"docs": "https://github.com/center-key/cli-argv-util#readme",
|
|
24
|
+
"author": "Center Key (https://centerkey.com)",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"cli",
|
|
27
|
+
"argv",
|
|
28
|
+
"params",
|
|
29
|
+
"flags"
|
|
30
|
+
],
|
|
31
|
+
"jshintConfig": {
|
|
32
|
+
"esversion": 11,
|
|
33
|
+
"strict": "implied",
|
|
34
|
+
"eqeqeq": true,
|
|
35
|
+
"undef": true,
|
|
36
|
+
"unused": true,
|
|
37
|
+
"varstmt": true,
|
|
38
|
+
"node": true,
|
|
39
|
+
"mocha": true
|
|
40
|
+
},
|
|
41
|
+
"eslintConfig": {
|
|
42
|
+
"ignorePatterns": [
|
|
43
|
+
"build",
|
|
44
|
+
"dist",
|
|
45
|
+
"node_modules"
|
|
46
|
+
],
|
|
47
|
+
"root": true,
|
|
48
|
+
"parser": "@typescript-eslint/parser",
|
|
49
|
+
"plugins": [
|
|
50
|
+
"@typescript-eslint"
|
|
51
|
+
],
|
|
52
|
+
"extends": [
|
|
53
|
+
"eslint:recommended",
|
|
54
|
+
"plugin:@typescript-eslint/recommended"
|
|
55
|
+
],
|
|
56
|
+
"rules": {
|
|
57
|
+
"@typescript-eslint/no-non-null-assertion": "off"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"runScriptsConfig": {
|
|
61
|
+
"build": [
|
|
62
|
+
"rimraf build dist **/.DS_Store",
|
|
63
|
+
"jshint . --exclude-path .gitignore",
|
|
64
|
+
"eslint --max-warnings 0 . --ext .ts",
|
|
65
|
+
"tsc",
|
|
66
|
+
"tsc --module UMD --outDir build/umd",
|
|
67
|
+
"copy-file build/umd/cli-argv-util.js build/cli-argv-util.umd.cjs",
|
|
68
|
+
"add-dist-header build dist"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"pretest": "run-scripts build",
|
|
73
|
+
"test": "mocha spec/*.spec.js"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@types/node": "~18.11",
|
|
77
|
+
"@typescript-eslint/eslint-plugin": "~5.43",
|
|
78
|
+
"@typescript-eslint/parser": "~5.43",
|
|
79
|
+
"add-dist-header": "~0.3",
|
|
80
|
+
"assert-deep-strict-equal": "~1.0",
|
|
81
|
+
"copy-file-util": "~0.1",
|
|
82
|
+
"eslint": "~8.27",
|
|
83
|
+
"jshint": "~2.13",
|
|
84
|
+
"mocha": "~10.1",
|
|
85
|
+
"rimraf": "~3.0",
|
|
86
|
+
"run-scripts-util": "~0.1",
|
|
87
|
+
"typescript": "~4.9"
|
|
88
|
+
}
|
|
89
|
+
}
|