@node-cli/parser 1.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 +21 -0
- package/README.md +73 -0
- package/dist/index.d.ts +6 -0
- package/dist/parser.d.ts +31 -0
- package/dist/parser.js +19 -0
- package/dist/parser.js.map +1 -0
- package/dist/utilities.d.ts +34 -0
- package/dist/utilities.js +166 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Arno Versini
|
|
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,73 @@
|
|
|
1
|
+
# Parser
|
|
2
|
+
|
|
3
|
+
> Simple, non-interactive, CLI app arguments parser
|
|
4
|
+
|
|
5
|
+
# API
|
|
6
|
+
|
|
7
|
+
**parser(options, defaultFlags, defaultParameters) ⇒ { flags, parameters }**
|
|
8
|
+
|
|
9
|
+
## Arguments
|
|
10
|
+
|
|
11
|
+
| Argument | Type |
|
|
12
|
+
| ------------------------- | ----------------- |
|
|
13
|
+
| options | Object |
|
|
14
|
+
| options.examples | Array of Object |
|
|
15
|
+
| options.flags | Object |
|
|
16
|
+
| options.parameters | Object |
|
|
17
|
+
| options.usage | String or Boolean |
|
|
18
|
+
| options.defaultFlags | Object |
|
|
19
|
+
| options.defaultParameters | Object |
|
|
20
|
+
|
|
21
|
+
## Example
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { parser } from "@node-cli/parser";
|
|
25
|
+
const { flags, parameters } = parser({
|
|
26
|
+
examples: [
|
|
27
|
+
{
|
|
28
|
+
command: 'my-cli --verbose --command "chmod +x" bin',
|
|
29
|
+
comment: '## Make all files executable in the "bin" folder',
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
flags: {
|
|
33
|
+
verbose: {
|
|
34
|
+
shortFlag: "V",
|
|
35
|
+
description: "Enable extra logging",
|
|
36
|
+
type: "boolean",
|
|
37
|
+
},
|
|
38
|
+
command: {
|
|
39
|
+
shortFlag: "c",
|
|
40
|
+
description: "Command to execute over each node (ex: chmod +x)",
|
|
41
|
+
type: "string",
|
|
42
|
+
},
|
|
43
|
+
help: {
|
|
44
|
+
shortFlag: "h",
|
|
45
|
+
description: "Display help instructions",
|
|
46
|
+
type: "boolean",
|
|
47
|
+
},
|
|
48
|
+
version: {
|
|
49
|
+
shortFlag: "v",
|
|
50
|
+
description: "Output the current version",
|
|
51
|
+
type: "boolean",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
parameters: {
|
|
55
|
+
src: {
|
|
56
|
+
default: "current folder",
|
|
57
|
+
description: "the source",
|
|
58
|
+
},
|
|
59
|
+
dest: {
|
|
60
|
+
description: "the destination",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
// use usage:true is equivalent to the following line
|
|
64
|
+
usage: "my-cli [options] [src] [dest]",
|
|
65
|
+
defaultFlags: {
|
|
66
|
+
verbose: false,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Note
|
|
72
|
+
|
|
73
|
+
If options `--version` or `--help` are used, they will automatically print version or help, respectively, and exit with 0 (`process.exit(0)`).
|
package/dist/index.d.ts
ADDED
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type Flags = {
|
|
2
|
+
[key: string]: {
|
|
3
|
+
shortFlag?: string;
|
|
4
|
+
default?: string | number | boolean;
|
|
5
|
+
description: string;
|
|
6
|
+
type: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
type Parameters = {
|
|
10
|
+
[key: string]: {
|
|
11
|
+
default?: string | number | boolean;
|
|
12
|
+
description: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export type ParserConfiguration = {
|
|
16
|
+
flags?: Flags;
|
|
17
|
+
parameters?: Parameters;
|
|
18
|
+
usage?: boolean | string;
|
|
19
|
+
examples?: string | {
|
|
20
|
+
command?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
comment?: string;
|
|
23
|
+
}[];
|
|
24
|
+
defaultFlags?: any;
|
|
25
|
+
defaultParameters?: any;
|
|
26
|
+
};
|
|
27
|
+
export declare const parser: (configuration: ParserConfiguration) => {
|
|
28
|
+
flags: object;
|
|
29
|
+
parameters: object;
|
|
30
|
+
};
|
|
31
|
+
export {};
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { meowOptionsHelper, meowParserHelper, shallowMerge } from "./utilities.js";
|
|
2
|
+
import meow from "meow";
|
|
3
|
+
export const parser = (configuration)=>{
|
|
4
|
+
const { defaultFlags ={} , defaultParameters ={} , ...others } = configuration;
|
|
5
|
+
const { helpText , options } = meowOptionsHelper(others);
|
|
6
|
+
const cli = meow(helpText, {
|
|
7
|
+
...options,
|
|
8
|
+
importMeta: import.meta
|
|
9
|
+
});
|
|
10
|
+
meowParserHelper({
|
|
11
|
+
cli
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
flags: shallowMerge(defaultFlags, cli.flags),
|
|
15
|
+
parameters: shallowMerge(defaultParameters, cli.input)
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/parser.ts"],"sourcesContent":["import {\n\tmeowOptionsHelper,\n\tmeowParserHelper,\n\tshallowMerge,\n} from \"./utilities.js\";\n\nimport meow from \"meow\";\n\ntype Flags = {\n\t[key: string]: {\n\t\tshortFlag?: string;\n\t\tdefault?: string | number | boolean;\n\t\tdescription: string;\n\t\ttype: string;\n\t};\n};\ntype Parameters = {\n\t[key: string]: {\n\t\tdefault?: string | number | boolean;\n\t\tdescription: string;\n\t};\n};\nexport type ParserConfiguration = {\n\tflags?: Flags;\n\tparameters?: Parameters;\n\tusage?: boolean | string;\n\texamples?:\n\t\t| string\n\t\t| { command?: string; description?: string; comment?: string }[];\n\tdefaultFlags?: any;\n\tdefaultParameters?: any;\n};\n\nexport const parser = (configuration: ParserConfiguration) => {\n\tconst {\n\t\tdefaultFlags = {},\n\t\tdefaultParameters = {},\n\t\t...others\n\t} = configuration;\n\tconst { helpText, options } = meowOptionsHelper(others);\n\tconst cli = meow(helpText, { ...options, importMeta: import.meta });\n\tmeowParserHelper({ cli });\n\n\treturn {\n\t\tflags: shallowMerge(defaultFlags, cli.flags),\n\t\tparameters: shallowMerge(defaultParameters, cli.input),\n\t};\n};\n"],"names":["meowOptionsHelper","meowParserHelper","shallowMerge","meow","parser","configuration","defaultFlags","defaultParameters","others","helpText","options","cli","importMeta","flags","parameters","input"],"mappings":"AAAA,SACCA,iBAAiB,EACjBC,gBAAgB,EAChBC,YAAY,QACN,iBAAiB;AAExB,OAAOC,UAAU,OAAO;AA2BxB,OAAO,MAAMC,SAAS,CAACC,gBAAuC;IAC7D,MAAM,EACLC,cAAe,CAAC,EAAC,EACjBC,mBAAoB,CAAC,EAAC,EACtB,GAAGC,QACH,GAAGH;IACJ,MAAM,EAAEI,SAAQ,EAAEC,QAAO,EAAE,GAAGV,kBAAkBQ;IAChD,MAAMG,MAAMR,KAAKM,UAAU;QAAE,GAAGC,OAAO;QAAEE,YAAY;IAAY;IACjEX,iBAAiB;QAAEU;IAAI;IAEvB,OAAO;QACNE,OAAOX,aAAaI,cAAcK,IAAIE,KAAK;QAC3CC,YAAYZ,aAAaK,mBAAmBI,IAAII,KAAK;IACtD;AACD,EAAE"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrapper method for lodash `merge()` and `mergeWith()` methods.
|
|
3
|
+
*
|
|
4
|
+
* Without the `customizer` function, this method recursively merges own and inherited
|
|
5
|
+
* enumerable string keyed properties of source objects into the destination object.
|
|
6
|
+
* Source properties that resolve to undefined are skipped if a destination value exists.
|
|
7
|
+
* Array and plain object properties are merged recursively. Other objects and value
|
|
8
|
+
* types are overridden by assignment. Source objects are applied from left to right.
|
|
9
|
+
* Subsequent sources overwrite property assignments of previous sources.
|
|
10
|
+
*
|
|
11
|
+
* With the `customizer` function, the behavior is the same except that `customizer` is
|
|
12
|
+
* invoked to produce the merged values of the destination and source properties.
|
|
13
|
+
* If customizer returns undefined, merging is handled by the `shallowMerge` instead.
|
|
14
|
+
* The customizer is invoked with six arguments: `(objValue, srcValue, key, object,
|
|
15
|
+
* source, stack)`
|
|
16
|
+
* @param {object} objectA
|
|
17
|
+
* @param {object} objectB
|
|
18
|
+
* @param {function} customizer
|
|
19
|
+
* @returns {object} !! WARNING: this method will mutate objectA
|
|
20
|
+
*/
|
|
21
|
+
export declare const shallowMerge: (objectA: any, objectB: any, customizer?: any) => object;
|
|
22
|
+
export declare const meowOptionsHelper: (parameters_: {
|
|
23
|
+
flags?: any;
|
|
24
|
+
parameters?: any;
|
|
25
|
+
usage?: any;
|
|
26
|
+
examples?: any;
|
|
27
|
+
}) => {
|
|
28
|
+
helpText: string;
|
|
29
|
+
options: any;
|
|
30
|
+
};
|
|
31
|
+
export declare const meowParserHelper: (parameters: {
|
|
32
|
+
cli: any;
|
|
33
|
+
restrictions?: any;
|
|
34
|
+
}) => void;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/* eslint-disable unicorn/no-process-exit */ import { Logger } from "@node-cli/logger";
|
|
2
|
+
import Table from "cli-table3";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
import kleur from "kleur";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
const logger = new Logger();
|
|
7
|
+
/**
|
|
8
|
+
* Converts the first character of string to upper case
|
|
9
|
+
* @param {string} string_ the string to convert
|
|
10
|
+
* @returns {string} the converted string
|
|
11
|
+
*/ const upperFirst = (string_)=>string_[0].toUpperCase() + string_.slice(1);
|
|
12
|
+
/**
|
|
13
|
+
* Wrapper method for lodash `merge()` and `mergeWith()` methods.
|
|
14
|
+
*
|
|
15
|
+
* Without the `customizer` function, this method recursively merges own and inherited
|
|
16
|
+
* enumerable string keyed properties of source objects into the destination object.
|
|
17
|
+
* Source properties that resolve to undefined are skipped if a destination value exists.
|
|
18
|
+
* Array and plain object properties are merged recursively. Other objects and value
|
|
19
|
+
* types are overridden by assignment. Source objects are applied from left to right.
|
|
20
|
+
* Subsequent sources overwrite property assignments of previous sources.
|
|
21
|
+
*
|
|
22
|
+
* With the `customizer` function, the behavior is the same except that `customizer` is
|
|
23
|
+
* invoked to produce the merged values of the destination and source properties.
|
|
24
|
+
* If customizer returns undefined, merging is handled by the `shallowMerge` instead.
|
|
25
|
+
* The customizer is invoked with six arguments: `(objValue, srcValue, key, object,
|
|
26
|
+
* source, stack)`
|
|
27
|
+
* @param {object} objectA
|
|
28
|
+
* @param {object} objectB
|
|
29
|
+
* @param {function} customizer
|
|
30
|
+
* @returns {object} !! WARNING: this method will mutate objectA
|
|
31
|
+
*/ export const shallowMerge = (objectA, objectB, customizer)=>{
|
|
32
|
+
return typeof customizer === "function" ? _.mergeWith(objectA, objectB, customizer) : _.merge(objectA, objectB);
|
|
33
|
+
};
|
|
34
|
+
export const meowOptionsHelper = (parameters_)=>{
|
|
35
|
+
let { usage , flags , parameters , examples } = parameters_;
|
|
36
|
+
let helpText = "", usageText = "";
|
|
37
|
+
const commandPrefix = "> ";
|
|
38
|
+
const options = {
|
|
39
|
+
allowUnknownFlags: false,
|
|
40
|
+
autoHelp: false,
|
|
41
|
+
autoVersion: false,
|
|
42
|
+
description: false,
|
|
43
|
+
flags
|
|
44
|
+
};
|
|
45
|
+
const commonTableConfiguration = {
|
|
46
|
+
chars: {
|
|
47
|
+
bottom: "",
|
|
48
|
+
"bottom-left": "",
|
|
49
|
+
"bottom-mid": "",
|
|
50
|
+
"bottom-right": "",
|
|
51
|
+
left: "",
|
|
52
|
+
"left-mid": "",
|
|
53
|
+
mid: "",
|
|
54
|
+
"mid-mid": "",
|
|
55
|
+
middle: "",
|
|
56
|
+
right: "",
|
|
57
|
+
"right-mid": "",
|
|
58
|
+
top: "",
|
|
59
|
+
"top-left": "",
|
|
60
|
+
"top-mid": "",
|
|
61
|
+
"top-right": ""
|
|
62
|
+
},
|
|
63
|
+
style: {
|
|
64
|
+
"padding-left": 0,
|
|
65
|
+
"padding-right": 2
|
|
66
|
+
},
|
|
67
|
+
wordWrap: true
|
|
68
|
+
};
|
|
69
|
+
if (usage) {
|
|
70
|
+
if (typeof usage === "string") {
|
|
71
|
+
const optionalParameters = usage.match(/\[(.*?)]/g);
|
|
72
|
+
const requiredParameters = usage.match(/<(.*?)>/g);
|
|
73
|
+
if (optionalParameters) {
|
|
74
|
+
for (const item of optionalParameters){
|
|
75
|
+
usage = usage.replace(item, `${kleur.green(item)}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (requiredParameters) {
|
|
79
|
+
for (const item of requiredParameters){
|
|
80
|
+
usage = usage.replace(item, `${kleur.red(item)}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
usageText = ` Usage:\n ${commandPrefix}${usage}`;
|
|
84
|
+
}
|
|
85
|
+
if (typeof usage === "boolean") {
|
|
86
|
+
const processName = path.basename(process.argv[1]);
|
|
87
|
+
usageText = ` Usage:\n ${commandPrefix}${processName}`;
|
|
88
|
+
if (flags) {
|
|
89
|
+
usageText += kleur.green(" [options]");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (flags) {
|
|
94
|
+
const flagsTable = new Table(commonTableConfiguration);
|
|
95
|
+
helpText += "\n\n Options:\n";
|
|
96
|
+
for (const item of Object.keys(flags).sort()){
|
|
97
|
+
const flag = flags[item];
|
|
98
|
+
const aliasCell = flag.shortFlag ? ` ${kleur.blue(`-${flag.shortFlag}, --${item}`)}` : ` ${kleur.blue(` --${item}`)}`;
|
|
99
|
+
const defaultCell = flag.default === undefined ? "" : `${kleur.grey(`(default: ${flag.default})`)}`;
|
|
100
|
+
flagsTable.push([
|
|
101
|
+
aliasCell,
|
|
102
|
+
flag.description,
|
|
103
|
+
defaultCell
|
|
104
|
+
]);
|
|
105
|
+
}
|
|
106
|
+
helpText += flagsTable.toString();
|
|
107
|
+
}
|
|
108
|
+
if (parameters) {
|
|
109
|
+
const parametersTable = new Table(commonTableConfiguration);
|
|
110
|
+
helpText += "\n\n";
|
|
111
|
+
for (const item of Object.keys(parameters).sort()){
|
|
112
|
+
const parameter = parameters[item];
|
|
113
|
+
const headerCell = ` ${upperFirst(item)}:`;
|
|
114
|
+
const defaultCell = parameter.default === undefined ? "" : `${kleur.grey(`(default: ${parameter.default})`)}`;
|
|
115
|
+
parametersTable.push([
|
|
116
|
+
headerCell,
|
|
117
|
+
parameter.description,
|
|
118
|
+
defaultCell
|
|
119
|
+
]);
|
|
120
|
+
if (typeof usage === "boolean") {
|
|
121
|
+
usageText += ` ${kleur.green(`[${item}]`)}`;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
helpText += parametersTable.toString();
|
|
125
|
+
}
|
|
126
|
+
if (examples) {
|
|
127
|
+
helpText += "\n\n Examples:\n";
|
|
128
|
+
for (const item of examples){
|
|
129
|
+
helpText += `\n ${kleur.grey(`${item.comment}`)}\n`;
|
|
130
|
+
helpText += ` ${kleur.blue(`${commandPrefix}${item.command}`)}\n`;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
helpText: `\n${usageText}${helpText}`,
|
|
135
|
+
options
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
export const meowParserHelper = (parameters)=>{
|
|
139
|
+
const { cli , restrictions } = parameters;
|
|
140
|
+
try {
|
|
141
|
+
if (cli.flags.help) {
|
|
142
|
+
cli.showHelp();
|
|
143
|
+
process.exit(0);
|
|
144
|
+
}
|
|
145
|
+
} catch {
|
|
146
|
+
// nothing to declare officer
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
if (cli.flags.version) {
|
|
150
|
+
cli.showVersion();
|
|
151
|
+
process.exit(0);
|
|
152
|
+
}
|
|
153
|
+
} catch {
|
|
154
|
+
// nothing to declare officer
|
|
155
|
+
}
|
|
156
|
+
if (restrictions && restrictions.length > 0) {
|
|
157
|
+
for (const rule of restrictions){
|
|
158
|
+
if (rule.test(cli.flags)) {
|
|
159
|
+
logger.error(rule.message(cli.flags));
|
|
160
|
+
process.exit(rule.exit);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
//# sourceMappingURL=utilities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utilities.ts"],"sourcesContent":["/* eslint-disable unicorn/no-process-exit */\n\nimport { Logger } from \"@node-cli/logger\";\nimport Table from \"cli-table3\";\nimport _ from \"lodash\";\nimport kleur from \"kleur\";\nimport path from \"node:path\";\nconst logger = new Logger();\n\n/**\n * Converts the first character of string to upper case\n * @param {string} string_ the string to convert\n * @returns {string} the converted string\n */\nconst upperFirst = (string_: string): string =>\n\tstring_[0].toUpperCase() + string_.slice(1);\n\n/**\n * Wrapper method for lodash `merge()` and `mergeWith()` methods.\n *\n * Without the `customizer` function, this method recursively merges own and inherited\n * enumerable string keyed properties of source objects into the destination object.\n * Source properties that resolve to undefined are skipped if a destination value exists.\n * Array and plain object properties are merged recursively. Other objects and value\n * types are overridden by assignment. Source objects are applied from left to right.\n * Subsequent sources overwrite property assignments of previous sources.\n *\n * With the `customizer` function, the behavior is the same except that `customizer` is\n * invoked to produce the merged values of the destination and source properties.\n * If customizer returns undefined, merging is handled by the `shallowMerge` instead.\n * The customizer is invoked with six arguments: `(objValue, srcValue, key, object,\n * source, stack)`\n * @param {object} objectA\n * @param {object} objectB\n * @param {function} customizer\n * @returns {object} !! WARNING: this method will mutate objectA\n */\nexport const shallowMerge = (\n\tobjectA: any,\n\tobjectB: any,\n\tcustomizer?: any\n): object => {\n\treturn typeof customizer === \"function\"\n\t\t? _.mergeWith(objectA, objectB, customizer)\n\t\t: _.merge(objectA, objectB);\n};\n\nexport const meowOptionsHelper = (parameters_: {\n\tflags?: any;\n\tparameters?: any;\n\tusage?: any;\n\texamples?: any;\n}) => {\n\tlet { usage, flags, parameters, examples } = parameters_;\n\tlet helpText = \"\",\n\t\tusageText = \"\";\n\tconst commandPrefix = \"> \";\n\tconst options: any = {\n\t\tallowUnknownFlags: false,\n\t\tautoHelp: false,\n\t\tautoVersion: false,\n\t\tdescription: false,\n\t\tflags,\n\t};\n\tconst commonTableConfiguration = {\n\t\tchars: {\n\t\t\tbottom: \"\",\n\t\t\t\"bottom-left\": \"\",\n\t\t\t\"bottom-mid\": \"\",\n\t\t\t\"bottom-right\": \"\",\n\t\t\tleft: \"\",\n\t\t\t\"left-mid\": \"\",\n\t\t\tmid: \"\",\n\t\t\t\"mid-mid\": \"\",\n\t\t\tmiddle: \"\",\n\t\t\tright: \"\",\n\t\t\t\"right-mid\": \"\",\n\t\t\ttop: \"\",\n\t\t\t\"top-left\": \"\",\n\t\t\t\"top-mid\": \"\",\n\t\t\t\"top-right\": \"\",\n\t\t},\n\t\tstyle: {\n\t\t\t\"padding-left\": 0,\n\t\t\t\"padding-right\": 2,\n\t\t},\n\t\twordWrap: true,\n\t};\n\n\tif (usage) {\n\t\tif (typeof usage === \"string\") {\n\t\t\tconst optionalParameters = usage.match(/\\[(.*?)]/g);\n\t\t\tconst requiredParameters = usage.match(/<(.*?)>/g);\n\t\t\tif (optionalParameters) {\n\t\t\t\tfor (const item of optionalParameters) {\n\t\t\t\t\tusage = usage.replace(item, `${kleur.green(item)}`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (requiredParameters) {\n\t\t\t\tfor (const item of requiredParameters) {\n\t\t\t\t\tusage = usage.replace(item, `${kleur.red(item)}`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tusageText = ` Usage:\\n ${commandPrefix}${usage}`;\n\t\t}\n\t\tif (typeof usage === \"boolean\") {\n\t\t\tconst processName = path.basename(process.argv[1]);\n\t\t\tusageText = ` Usage:\\n ${commandPrefix}${processName}`;\n\t\t\tif (flags) {\n\t\t\t\tusageText += kleur.green(\" [options]\");\n\t\t\t}\n\t\t}\n\t}\n\n\tif (flags) {\n\t\tconst flagsTable = new Table(commonTableConfiguration);\n\t\thelpText += \"\\n\\n Options:\\n\";\n\n\t\tfor (const item of Object.keys(flags).sort()) {\n\t\t\tconst flag = flags[item];\n\t\t\tconst aliasCell = flag.shortFlag\n\t\t\t\t? ` ${kleur.blue(`-${flag.shortFlag}, --${item}`)}`\n\t\t\t\t: ` ${kleur.blue(` --${item}`)}`;\n\t\t\tconst defaultCell =\n\t\t\t\tflag.default === undefined\n\t\t\t\t\t? \"\"\n\t\t\t\t\t: `${kleur.grey(`(default: ${flag.default})`)}`;\n\t\t\tflagsTable.push([aliasCell, flag.description, defaultCell]);\n\t\t}\n\t\thelpText += flagsTable.toString();\n\t}\n\n\tif (parameters) {\n\t\tconst parametersTable = new Table(commonTableConfiguration);\n\t\thelpText += \"\\n\\n\";\n\n\t\tfor (const item of Object.keys(parameters).sort()) {\n\t\t\tconst parameter = parameters[item];\n\t\t\tconst headerCell = ` ${upperFirst(item)}:`;\n\t\t\tconst defaultCell =\n\t\t\t\tparameter.default === undefined\n\t\t\t\t\t? \"\"\n\t\t\t\t\t: `${kleur.grey(`(default: ${parameter.default})`)}`;\n\t\t\tparametersTable.push([headerCell, parameter.description, defaultCell]);\n\t\t\tif (typeof usage === \"boolean\") {\n\t\t\t\tusageText += ` ${kleur.green(`[${item}]`)}`;\n\t\t\t}\n\t\t}\n\t\thelpText += parametersTable.toString();\n\t}\n\n\tif (examples) {\n\t\thelpText += \"\\n\\n Examples:\\n\";\n\t\tfor (const item of examples) {\n\t\t\thelpText += `\\n ${kleur.grey(`${item.comment}`)}\\n`;\n\t\t\thelpText += ` ${kleur.blue(`${commandPrefix}${item.command}`)}\\n`;\n\t\t}\n\t}\n\n\treturn {\n\t\thelpText: `\\n${usageText}${helpText}`,\n\t\toptions,\n\t};\n};\n\nexport const meowParserHelper = (parameters: {\n\tcli: any;\n\trestrictions?: any;\n}) => {\n\tconst { cli, restrictions } = parameters;\n\ttry {\n\t\tif (cli.flags.help) {\n\t\t\tcli.showHelp();\n\t\t\tprocess.exit(0);\n\t\t}\n\t} catch {\n\t\t// nothing to declare officer\n\t}\n\n\ttry {\n\t\tif (cli.flags.version) {\n\t\t\tcli.showVersion();\n\t\t\tprocess.exit(0);\n\t\t}\n\t} catch {\n\t\t// nothing to declare officer\n\t}\n\n\tif (restrictions && restrictions.length > 0) {\n\t\tfor (const rule of restrictions) {\n\t\t\tif (rule.test(cli.flags)) {\n\t\t\t\tlogger.error(rule.message(cli.flags));\n\t\t\t\tprocess.exit(rule.exit);\n\t\t\t}\n\t\t}\n\t}\n};\n"],"names":["Logger","Table","_","kleur","path","logger","upperFirst","string_","toUpperCase","slice","shallowMerge","objectA","objectB","customizer","mergeWith","merge","meowOptionsHelper","parameters_","usage","flags","parameters","examples","helpText","usageText","commandPrefix","options","allowUnknownFlags","autoHelp","autoVersion","description","commonTableConfiguration","chars","bottom","left","mid","middle","right","top","style","wordWrap","optionalParameters","match","requiredParameters","item","replace","green","red","processName","basename","process","argv","flagsTable","Object","keys","sort","flag","aliasCell","shortFlag","blue","defaultCell","default","undefined","grey","push","toString","parametersTable","parameter","headerCell","comment","command","meowParserHelper","cli","restrictions","help","showHelp","exit","version","showVersion","length","rule","test","error","message"],"mappings":"AAAA,0CAA0C,GAE1C,SAASA,MAAM,QAAQ,mBAAmB;AAC1C,OAAOC,WAAW,aAAa;AAC/B,OAAOC,OAAO,SAAS;AACvB,OAAOC,WAAW,QAAQ;AAC1B,OAAOC,UAAU,YAAY;AAC7B,MAAMC,SAAS,IAAIL;AAEnB;;;;CAIC,GACD,MAAMM,aAAa,CAACC,UACnBA,OAAO,CAAC,EAAE,CAACC,WAAW,KAAKD,QAAQE,KAAK,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,MAAMC,eAAe,CAC3BC,SACAC,SACAC,aACY;IACZ,OAAO,OAAOA,eAAe,aAC1BX,EAAEY,SAAS,CAACH,SAASC,SAASC,cAC9BX,EAAEa,KAAK,CAACJ,SAASC,QAAQ;AAC7B,EAAE;AAEF,OAAO,MAAMI,oBAAoB,CAACC,cAK5B;IACL,IAAI,EAAEC,MAAK,EAAEC,MAAK,EAAEC,WAAU,EAAEC,SAAQ,EAAE,GAAGJ;IAC7C,IAAIK,WAAW,IACdC,YAAY;IACb,MAAMC,gBAAgB;IACtB,MAAMC,UAAe;QACpBC,mBAAmB,KAAK;QACxBC,UAAU,KAAK;QACfC,aAAa,KAAK;QAClBC,aAAa,KAAK;QAClBV;IACD;IACA,MAAMW,2BAA2B;QA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|
+
"description": "A simple CLI parser helper",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": "./dist/parser.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"node": ">=16",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@node-cli/logger": "^0.0.8",
|
|
16
|
+
"cli-table3": "0.6.3",
|
|
17
|
+
"kleur": "4.1.5",
|
|
18
|
+
"lodash": "4.17.21",
|
|
19
|
+
"meow": "12.0.1"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "yarn run clean && yarn run build:types && yarn run build:js && yarn run build:barrel",
|
|
23
|
+
"build:barrel": "barrelsby --delete --directory dist --pattern \"**/*.d.ts\" --name \"index.d\"",
|
|
24
|
+
"build:js": "swc --source-maps --out-dir dist src",
|
|
25
|
+
"build:types": "tsc",
|
|
26
|
+
"clean": "rimraf dist types coverage",
|
|
27
|
+
"lint": "eslint \"src/*.ts\"",
|
|
28
|
+
"test": "cross-env-shell NODE_OPTIONS=--experimental-vm-modules jest",
|
|
29
|
+
"test:coverage": "npm run test -- --coverage",
|
|
30
|
+
"watch": "swc --watch --out-dir dist src"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "fe6401830d153ab91166243c2cafd96f6cc0d9b5"
|
|
36
|
+
}
|