@ztimson/utils 0.23.4 → 0.23.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/dist/arg-parser.d.ts +0 -1
- package/dist/index.cjs +13 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +13 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2;
|
|
3
3
|
var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value2);
|
|
4
|
-
|
|
4
|
+
class ArgParser {
|
|
5
5
|
/**
|
|
6
6
|
* Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
|
|
7
7
|
*
|
|
@@ -19,12 +19,15 @@ const _ArgParser = class _ArgParser {
|
|
|
19
19
|
this.desc = desc;
|
|
20
20
|
this.argList = argList;
|
|
21
21
|
this.examples = examples;
|
|
22
|
-
this.commands = argList.filter((arg) => arg instanceof
|
|
22
|
+
this.commands = argList.filter((arg) => arg instanceof ArgParser);
|
|
23
23
|
this.args = argList.filter((arg) => {
|
|
24
24
|
var _a;
|
|
25
|
-
return !(arg instanceof
|
|
25
|
+
return !(arg instanceof ArgParser) && !((_a = arg.flags) == null ? void 0 : _a.length);
|
|
26
26
|
});
|
|
27
|
-
this.flags = [
|
|
27
|
+
this.flags = [
|
|
28
|
+
...argList.filter((arg) => !(arg instanceof ArgParser) && arg.flags && arg.flags.length),
|
|
29
|
+
{ name: "help", desc: "Display command's help message", flags: ["-h", "--help"], default: false }
|
|
30
|
+
];
|
|
28
31
|
this.defaults = argList.reduce((acc, arg) => ({ ...acc, [arg.name]: arg["extras"] ? [] : arg.default ?? null }), {});
|
|
29
32
|
this.examples = [
|
|
30
33
|
...examples,
|
|
@@ -101,21 +104,19 @@ const _ArgParser = class _ArgParser {
|
|
|
101
104
|
let msg = `
|
|
102
105
|
|
|
103
106
|
${opts.message || this.desc}`;
|
|
104
|
-
msg += "\n\nUsage: " + this.examples.map((ex) =>
|
|
105
|
-
if (this.args.length) msg += "\n\n
|
|
106
|
-
msg += "\n\nOptions:\n
|
|
107
|
+
msg += "\n\nUsage: " + this.examples.map((ex) => `${this.name} ${ex}`).join("\n ");
|
|
108
|
+
if (this.args.length) msg += "\n\n " + this.args.map((arg) => `${arg.name.toUpperCase()}${spacer(arg.name)}${arg.desc}`).join("\n ");
|
|
109
|
+
msg += "\n\nOptions:\n " + this.flags.map((flag) => {
|
|
107
110
|
var _a;
|
|
108
111
|
const flags = ((_a = flag.flags) == null ? void 0 : _a.join(", ")) || "";
|
|
109
112
|
return `${flags}${spacer(flags)}${flag.desc}`;
|
|
110
|
-
}).join("\n
|
|
111
|
-
if (this.commands.length) msg += "\n\nCommands:\n
|
|
113
|
+
}).join("\n ");
|
|
114
|
+
if (this.commands.length) msg += "\n\nCommands:\n " + this.commands.map((command) => `${command.name}${spacer(command.name)}${command.desc}`).join("\n ");
|
|
112
115
|
return `${msg}
|
|
113
116
|
|
|
114
117
|
`;
|
|
115
118
|
}
|
|
116
|
-
}
|
|
117
|
-
__publicField(_ArgParser, "helpArg", { name: "help", desc: "Display command's help message", flags: ["-h", "--help"], default: false });
|
|
118
|
-
let ArgParser = _ArgParser;
|
|
119
|
+
}
|
|
119
120
|
function clean(obj, undefinedOnly = false) {
|
|
120
121
|
if (obj == null) throw new Error("Cannot clean a NULL value");
|
|
121
122
|
if (Array.isArray(obj)) {
|