@rickosborne/clamp 2025.1.7
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/README.md +216 -0
- package/cjs/index.js +27 -0
- package/cjs/index.js.map +1 -0
- package/cjs/package.json +6 -0
- package/cjs/ts/boolean-param.js +45 -0
- package/cjs/ts/boolean-param.js.map +1 -0
- package/cjs/ts/command-params.js +160 -0
- package/cjs/ts/command-params.js.map +1 -0
- package/cjs/ts/common.js +3 -0
- package/cjs/ts/common.js.map +1 -0
- package/cjs/ts/date-param.js +38 -0
- package/cjs/ts/date-param.js.map +1 -0
- package/cjs/ts/file-param.js +43 -0
- package/cjs/ts/file-param.js.map +1 -0
- package/cjs/ts/help-for-param.js +33 -0
- package/cjs/ts/help-for-param.js.map +1 -0
- package/cjs/ts/label-for-param.js +18 -0
- package/cjs/ts/label-for-param.js.map +1 -0
- package/cjs/ts/number-param.js +56 -0
- package/cjs/ts/number-param.js.map +1 -0
- package/cjs/ts/param-handler.js +28 -0
- package/cjs/ts/param-handler.js.map +1 -0
- package/cjs/ts/string-param.js +34 -0
- package/cjs/ts/string-param.js.map +1 -0
- package/cjs/tsconfig.cjs.tsbuildinfo +1 -0
- package/esm/index.js +11 -0
- package/esm/index.js.map +1 -0
- package/esm/package.json +6 -0
- package/esm/ts/boolean-param.js +40 -0
- package/esm/ts/boolean-param.js.map +1 -0
- package/esm/ts/command-params.js +154 -0
- package/esm/ts/command-params.js.map +1 -0
- package/esm/ts/common.js +2 -0
- package/esm/ts/common.js.map +1 -0
- package/esm/ts/date-param.js +32 -0
- package/esm/ts/date-param.js.map +1 -0
- package/esm/ts/file-param.js +37 -0
- package/esm/ts/file-param.js.map +1 -0
- package/esm/ts/help-for-param.js +29 -0
- package/esm/ts/help-for-param.js.map +1 -0
- package/esm/ts/label-for-param.js +13 -0
- package/esm/ts/label-for-param.js.map +1 -0
- package/esm/ts/number-param.js +51 -0
- package/esm/ts/number-param.js.map +1 -0
- package/esm/ts/param-handler.js +23 -0
- package/esm/ts/param-handler.js.map +1 -0
- package/esm/ts/string-param.js +28 -0
- package/esm/ts/string-param.js.map +1 -0
- package/esm/tsconfig.module.tsbuildinfo +1 -0
- package/package.json +51 -0
- package/tsdoc-metadata.json +11 -0
- package/types/index.d.ts +11 -0
- package/types/index.d.ts.map +1 -0
- package/types/index.js +27 -0
- package/types/index.js.map +1 -0
- package/types/ts/boolean-param.d.ts +50 -0
- package/types/ts/boolean-param.d.ts.map +1 -0
- package/types/ts/boolean-param.js +44 -0
- package/types/ts/boolean-param.js.map +1 -0
- package/types/ts/command-params.d.ts +44 -0
- package/types/ts/command-params.d.ts.map +1 -0
- package/types/ts/command-params.js +158 -0
- package/types/ts/command-params.js.map +1 -0
- package/types/ts/common.d.ts +63 -0
- package/types/ts/common.d.ts.map +1 -0
- package/types/ts/common.js +3 -0
- package/types/ts/common.js.map +1 -0
- package/types/ts/date-param.d.ts +34 -0
- package/types/ts/date-param.d.ts.map +1 -0
- package/types/ts/date-param.js +37 -0
- package/types/ts/date-param.js.map +1 -0
- package/types/ts/file-param.d.ts +50 -0
- package/types/ts/file-param.d.ts.map +1 -0
- package/types/ts/file-param.js +42 -0
- package/types/ts/file-param.js.map +1 -0
- package/types/ts/help-for-param.d.ts +6 -0
- package/types/ts/help-for-param.d.ts.map +1 -0
- package/types/ts/help-for-param.js +33 -0
- package/types/ts/help-for-param.js.map +1 -0
- package/types/ts/label-for-param.d.ts +6 -0
- package/types/ts/label-for-param.d.ts.map +1 -0
- package/types/ts/label-for-param.js +17 -0
- package/types/ts/label-for-param.js.map +1 -0
- package/types/ts/number-param.d.ts +43 -0
- package/types/ts/number-param.d.ts.map +1 -0
- package/types/ts/number-param.js +56 -0
- package/types/ts/number-param.js.map +1 -0
- package/types/ts/param-handler.d.ts +6 -0
- package/types/ts/param-handler.d.ts.map +1 -0
- package/types/ts/param-handler.js +27 -0
- package/types/ts/param-handler.js.map +1 -0
- package/types/ts/string-param.d.ts +30 -0
- package/types/ts/string-param.d.ts.map +1 -0
- package/types/ts/string-param.js +33 -0
- package/types/ts/string-param.js.map +1 -0
- package/types/tsconfig.types.tsbuildinfo +1 -0
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": {
|
|
3
|
+
"name": "Rick Osborne",
|
|
4
|
+
"url": "https://rickosborne.org"
|
|
5
|
+
},
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@rickosborne/foundation": "2025.1.7",
|
|
8
|
+
"@rickosborne/guard": "2025.1.7",
|
|
9
|
+
"@rickosborne/term": "2025.1.7",
|
|
10
|
+
"@rickosborne/typical": "2025.1.7"
|
|
11
|
+
},
|
|
12
|
+
"description": "Rick Osborne's command-line parameter handling tools",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=14"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./types/index.d.ts",
|
|
19
|
+
"import": "./esm/index.js",
|
|
20
|
+
"require": "./cjs/index.js",
|
|
21
|
+
"default": "./esm/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/rickosborne/es-js-ts",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"typescript",
|
|
27
|
+
"files",
|
|
28
|
+
"terminal",
|
|
29
|
+
"console",
|
|
30
|
+
"command-line",
|
|
31
|
+
"arguments",
|
|
32
|
+
"parameters"
|
|
33
|
+
],
|
|
34
|
+
"license": "CC-BY-NC-SA-4.0",
|
|
35
|
+
"main": "cjs/index.js",
|
|
36
|
+
"module": "esm/index.js",
|
|
37
|
+
"name": "@rickosborne/clamp",
|
|
38
|
+
"private": false,
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"readme": "README.md",
|
|
43
|
+
"repository": {
|
|
44
|
+
"directory": "clamp",
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/rickosborne/es-js-ts.git"
|
|
47
|
+
},
|
|
48
|
+
"types": "types/index.d.ts",
|
|
49
|
+
"typings": "types/index.d.ts",
|
|
50
|
+
"version": "2025.1.7"
|
|
51
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.48.1"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./ts/boolean-param.js";
|
|
2
|
+
export * from "./ts/command-params.js";
|
|
3
|
+
export * from "./ts/common.js";
|
|
4
|
+
export * from "./ts/date-param.js";
|
|
5
|
+
export * from "./ts/file-param.js";
|
|
6
|
+
export * from "./ts/help-for-param.js";
|
|
7
|
+
export * from "./ts/label-for-param.js";
|
|
8
|
+
export * from "./ts/number-param.js";
|
|
9
|
+
export * from "./ts/param-handler.js";
|
|
10
|
+
export * from "./ts/string-param.js";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC"}
|
package/types/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./ts/boolean-param.js"), exports);
|
|
18
|
+
__exportStar(require("./ts/command-params.js"), exports);
|
|
19
|
+
__exportStar(require("./ts/common.js"), exports);
|
|
20
|
+
__exportStar(require("./ts/date-param.js"), exports);
|
|
21
|
+
__exportStar(require("./ts/file-param.js"), exports);
|
|
22
|
+
__exportStar(require("./ts/help-for-param.js"), exports);
|
|
23
|
+
__exportStar(require("./ts/label-for-param.js"), exports);
|
|
24
|
+
__exportStar(require("./ts/number-param.js"), exports);
|
|
25
|
+
__exportStar(require("./ts/param-handler.js"), exports);
|
|
26
|
+
__exportStar(require("./ts/string-param.js"), exports);
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAsC;AACtC,yDAAuC;AACvC,iDAA+B;AAC/B,qDAAmC;AACnC,qDAAmC;AACnC,yDAAuC;AACvC,0DAAwC;AACxC,uDAAqC;AACrC,wDAAsC;AACtC,uDAAqC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { CommandParamBase, ParamHandler } from "./common.js";
|
|
2
|
+
/**
|
|
3
|
+
* Default possible values for true.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare const TRUES: readonly string[];
|
|
7
|
+
/**
|
|
8
|
+
* Default possible values for false.
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare const FALSES: readonly string[];
|
|
12
|
+
/**
|
|
13
|
+
* A command-line parameter which will be converted to a Boolean.
|
|
14
|
+
*/
|
|
15
|
+
export type BooleanCommandParam = CommandParamBase & {
|
|
16
|
+
/**
|
|
17
|
+
* Possible values for false.
|
|
18
|
+
* @defaultValue {@link FALSES}
|
|
19
|
+
*/
|
|
20
|
+
falses?: string[];
|
|
21
|
+
/**
|
|
22
|
+
* Text value passed to the parser for a negative, if not otherwise provided.
|
|
23
|
+
*/
|
|
24
|
+
falseText?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Convert the text value to its Boolean result form.
|
|
27
|
+
* @defaultValue {@link parseBooleanArg}
|
|
28
|
+
*/
|
|
29
|
+
parse?: (this: void, text: string, param: BooleanCommandParam) => boolean;
|
|
30
|
+
placeholder?: undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Possible values for true.
|
|
33
|
+
* @defaultValue {@link TRUES}
|
|
34
|
+
*/
|
|
35
|
+
trues?: string[];
|
|
36
|
+
/**
|
|
37
|
+
* Text value passed to the parser for a positive, if not otherwise provided.
|
|
38
|
+
*/
|
|
39
|
+
trueText?: string;
|
|
40
|
+
type: typeof Boolean;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
export declare const parseBooleanArg: (text: string, param: BooleanCommandParam) => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
export declare const booleanParamHandler: ParamHandler<BooleanCommandParam, boolean>;
|
|
50
|
+
//# sourceMappingURL=boolean-param.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"boolean-param.d.ts","sourceRoot":"","sources":["../../../ts/boolean-param.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAqB,MAAM,aAAa,CAAC;AAGrF;;;GAGG;AACH,eAAO,MAAM,KAAK,mBAAkD,CAAC;AAGrE;;;GAGG;AACH,eAAO,MAAM,MAAM,mBAAkD,CAAC;AAEtE;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,gBAAgB,GAAG;IAEpD;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB,KAAK,OAAO,CAAC;IAC1E,WAAW,CAAC,EAAE,SAAS,CAAC;IACxB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,OAAO,OAAO,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,SAAU,MAAM,SAAS,mBAAmB,KAAG,OAW1E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,YAAY,CAAC,mBAAmB,EAAE,OAAO,CAQ1E,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.booleanParamHandler = exports.parseBooleanArg = exports.FALSES = exports.TRUES = void 0;
|
|
4
|
+
const label_for_param_js_1 = require("./label-for-param.js");
|
|
5
|
+
/**
|
|
6
|
+
* Default possible values for true.
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
exports.TRUES = Object.freeze(["t", "true", "1", "y", "yes"]);
|
|
10
|
+
// noinspection SpellCheckingInspection
|
|
11
|
+
/**
|
|
12
|
+
* Default possible values for false.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
exports.FALSES = Object.freeze(["f", "false", "0", "n", "no"]);
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
const parseBooleanArg = (text, param) => {
|
|
20
|
+
const lower = text.toLocaleLowerCase();
|
|
21
|
+
const trueValues = param.trues ?? exports.TRUES;
|
|
22
|
+
if (trueValues.includes(lower)) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
const falseValues = param.falses ?? exports.FALSES;
|
|
26
|
+
if (falseValues.includes(lower)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
throw new RangeError(`${(0, label_for_param_js_1.labelForParam)(param)}: invalid value`);
|
|
30
|
+
};
|
|
31
|
+
exports.parseBooleanArg = parseBooleanArg;
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
exports.booleanParamHandler = {
|
|
36
|
+
matchesParam(param) {
|
|
37
|
+
return param.type === Boolean;
|
|
38
|
+
},
|
|
39
|
+
parseArg: exports.parseBooleanArg,
|
|
40
|
+
placeholderForParam() {
|
|
41
|
+
return undefined;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=boolean-param.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"boolean-param.js","sourceRoot":"","sources":["../../../ts/boolean-param.ts"],"names":[],"mappings":";;;AACA,6DAAqD;AAErD;;;GAGG;AACU,QAAA,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,CAAE,CAAC,CAAC;AAErE,uCAAuC;AACvC;;;GAGG;AACU,QAAA,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAE,CAAC,CAAC;AAkCtE;;GAEG;AACI,MAAM,eAAe,GAAG,CAAC,IAAY,EAAE,KAA0B,EAAW,EAAE;IACpF,MAAM,KAAK,GAAG,IAAI,CAAC,iBAAiB,EAAE,CAAC;IACvC,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,IAAI,aAAK,CAAC;IACxC,IAAI,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,OAAO,IAAI,CAAC;IACb,CAAC;IACD,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,IAAI,cAAM,CAAC;IAC3C,IAAI,WAAW,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACjC,OAAO,KAAK,CAAC;IACd,CAAC;IACD,MAAM,IAAI,UAAU,CAAC,GAAI,IAAA,kCAAa,EAAC,KAAK,CAAE,iBAAiB,CAAC,CAAC;AAClE,CAAC,CAAC;AAXW,QAAA,eAAe,mBAW1B;AAEF;;GAEG;AACU,QAAA,mBAAmB,GAA+C;IAC9E,YAAY,CAAC,KAAwB;QACpC,OAAO,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC;IAC/B,CAAC;IACD,QAAQ,EAAE,uBAAe;IACzB,mBAAmB;QAClB,OAAO,SAAS,CAAC;IAClB,CAAC;CACD,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type BooleanCommandParam } from "./boolean-param.js";
|
|
2
|
+
import type { IfOptional, IfRepeated } from "./common.js";
|
|
3
|
+
import { type DateCommandParam } from "./date-param.js";
|
|
4
|
+
import { type FileArg, type FileCommandParam } from "./file-param.js";
|
|
5
|
+
import { type NumberCommandParam } from "./number-param.js";
|
|
6
|
+
import { type StringCommandParam } from "./string-param.js";
|
|
7
|
+
/**
|
|
8
|
+
* One of the built-in param types.
|
|
9
|
+
*/
|
|
10
|
+
export type CommandParam = StringCommandParam | NumberCommandParam | BooleanCommandParam | FileCommandParam | DateCommandParam;
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export type CommandParamsSpec = Record<string, CommandParam>;
|
|
15
|
+
/**
|
|
16
|
+
* A parser function for a specific type of param.
|
|
17
|
+
*/
|
|
18
|
+
export type CommandParamParser<Param extends CommandParam, Return> = (text: string, param: Param) => Return;
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
export type HasCommandParamParser<Param extends CommandParam, Return> = {
|
|
23
|
+
parse?: CommandParamParser<Param, Return>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Computed return type for a param.
|
|
27
|
+
*/
|
|
28
|
+
export type CommandParamReturn<Param extends CommandParam> = Param extends HasCommandParamParser<Param, infer Return> ? IfRepeated<Param, Return> : Param["type"] extends typeof Boolean ? IfOptional<Param, IfRepeated<Param, boolean>> : Param["type"] extends typeof Date ? IfOptional<Param, IfRepeated<Param, Date>> : Param["type"] extends typeof Number ? IfOptional<Param, IfRepeated<Param, number>> : Param["type"] extends "file" ? IfOptional<Param, IfRepeated<Param, FileArg>> : Param["type"] extends typeof String ? IfOptional<Param, IfRepeated<Param, string>> : undefined extends Param["type"] ? IfOptional<Param, IfRepeated<Param, string>> : never;
|
|
29
|
+
/**
|
|
30
|
+
* Computed return (Record) type for a Record of params.
|
|
31
|
+
*/
|
|
32
|
+
export type CommandParamsReturn<Params extends object> = {
|
|
33
|
+
[K in keyof Params]: Params[K] extends CommandParam ? CommandParamReturn<Params[K]> : never;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Parse the (maybe-given) command-line arguments according to the
|
|
37
|
+
* given param specs and construct a return type of the converted values.
|
|
38
|
+
*/
|
|
39
|
+
export declare const commandParams: <Spec extends CommandParamsSpec>(spec: Spec, { args, ignoreUnknown, onError, }?: {
|
|
40
|
+
args?: string[];
|
|
41
|
+
ignoreUnknown?: boolean;
|
|
42
|
+
onError?: (error: Error) => void;
|
|
43
|
+
}) => CommandParamsReturn<Spec>;
|
|
44
|
+
//# sourceMappingURL=command-params.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-params.d.ts","sourceRoot":"","sources":["../../../ts/command-params.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,mBAAmB,EAAiB,MAAM,oBAAoB,CAAC;AAC7E,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEtE,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,kBAAkB,GAAG,kBAAkB,GAAG,mBAAmB,GAAG,gBAAgB,GAAG,gBAAgB,CAAC;AAE/H;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAE7D;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,KAAK,SAAS,YAAY,EAAE,MAAM,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC;AAE5G;;GAEG;AACH,MAAM,MAAM,qBAAqB,CAAC,KAAK,SAAS,YAAY,EAAE,MAAM,IAAI;IAAE,KAAK,CAAC,EAAE,kBAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;CAAE,CAAC;AAEtH;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,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IACxB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACjC,KACC,mBAAmB,CAAC,IAAI,CA8H1B,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.commandParams = void 0;
|
|
4
|
+
const guard_1 = require("@rickosborne/guard");
|
|
5
|
+
const boolean_param_js_1 = require("./boolean-param.js");
|
|
6
|
+
const label_for_param_js_1 = require("./label-for-param.js");
|
|
7
|
+
const param_handler_js_1 = require("./param-handler.js");
|
|
8
|
+
const toReturnType = (text, param) => {
|
|
9
|
+
const paramHandler = (0, param_handler_js_1.findParamHandler)(param);
|
|
10
|
+
const parser = (param.parse ?? paramHandler.parseArg);
|
|
11
|
+
if (parser != null) {
|
|
12
|
+
return parser(text, param);
|
|
13
|
+
}
|
|
14
|
+
throw new Error(`Unhandled param type: ${String(param.type)}`);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Parse the (maybe-given) command-line arguments according to the
|
|
18
|
+
* given param specs and construct a return type of the converted values.
|
|
19
|
+
*/
|
|
20
|
+
const commandParams = (spec, { args = process.argv.slice(2), ignoreUnknown, onError, } = {}) => {
|
|
21
|
+
const result = {};
|
|
22
|
+
const positionals = Object.entries(spec)
|
|
23
|
+
.filter(([, s]) => s.positional === true)
|
|
24
|
+
.map(([key, s]) => ({ key, spec: s }));
|
|
25
|
+
if (positionals.length > 1) {
|
|
26
|
+
throw new Error(`Too many positionals: ${positionals.map(({ key }) => key).join(" ")}`);
|
|
27
|
+
}
|
|
28
|
+
const positional = positionals.shift();
|
|
29
|
+
let onlyPositionalsRemain = false;
|
|
30
|
+
try {
|
|
31
|
+
while (args.length > 0) {
|
|
32
|
+
const arg = args.shift();
|
|
33
|
+
let param;
|
|
34
|
+
let resultKey;
|
|
35
|
+
let typedValue;
|
|
36
|
+
if (arg === "--" && !onlyPositionalsRemain) {
|
|
37
|
+
onlyPositionalsRemain = true;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (onlyPositionalsRemain || !arg.startsWith("-")) {
|
|
41
|
+
if (positional == null) {
|
|
42
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
43
|
+
throw new Error(`Unexpected positional: ${JSON.stringify(arg)}`);
|
|
44
|
+
}
|
|
45
|
+
param = positional.spec;
|
|
46
|
+
resultKey = positional.key;
|
|
47
|
+
typedValue = toReturnType(arg, positional.spec);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
let no = false;
|
|
51
|
+
let name;
|
|
52
|
+
if (arg.startsWith("--no-")) {
|
|
53
|
+
name = arg.substring(5);
|
|
54
|
+
no = true;
|
|
55
|
+
}
|
|
56
|
+
else if (arg.startsWith("--")) {
|
|
57
|
+
name = arg.substring(2);
|
|
58
|
+
}
|
|
59
|
+
else if (arg.startsWith("-no-")) {
|
|
60
|
+
name = arg.substring(4);
|
|
61
|
+
no = true;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
name = arg.substring(1);
|
|
65
|
+
}
|
|
66
|
+
let suffix;
|
|
67
|
+
if (name.includes("=")) {
|
|
68
|
+
[name, suffix] = name.split("=", 2);
|
|
69
|
+
}
|
|
70
|
+
const specs = Object.entries(spec)
|
|
71
|
+
.filter(([n, s]) => "name" in s && s.name === name || ("names" in s && (s.names?.includes(name) ?? false)) || n === name);
|
|
72
|
+
if (specs.length < 1) {
|
|
73
|
+
if (ignoreUnknown) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
77
|
+
throw new Error(`Unexpected arg: ${JSON.stringify(name)}`);
|
|
78
|
+
}
|
|
79
|
+
if (specs.length > 1) {
|
|
80
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
81
|
+
throw new Error(`More than one matching spec: ${name}`);
|
|
82
|
+
}
|
|
83
|
+
[resultKey, param] = specs[0];
|
|
84
|
+
(0, guard_1.assertDefined)(param, `param: ${name}`);
|
|
85
|
+
let stringValue;
|
|
86
|
+
if (param.type === Boolean) {
|
|
87
|
+
if (no) {
|
|
88
|
+
if (suffix != null) {
|
|
89
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
90
|
+
throw new Error(`Combo of "no" plus "=" does not make any sense for ${(0, label_for_param_js_1.labelForParam)(param)}`);
|
|
91
|
+
}
|
|
92
|
+
stringValue = param.falseText ?? param.falses?.[0] ?? boolean_param_js_1.FALSES[0];
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
stringValue = suffix ?? param.trueText ?? param.trues?.[0] ?? boolean_param_js_1.TRUES[0];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (no) {
|
|
99
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
100
|
+
throw new Error(`"no" does not make sense for ${(0, label_for_param_js_1.labelForParam)(param)}`);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
stringValue = suffix ?? args.shift();
|
|
104
|
+
}
|
|
105
|
+
(0, guard_1.assertDefined)(stringValue, () => `value for ${(0, label_for_param_js_1.labelForParam)(param)}`);
|
|
106
|
+
typedValue = toReturnType(stringValue, param);
|
|
107
|
+
}
|
|
108
|
+
if (param == null || resultKey == null) {
|
|
109
|
+
if (ignoreUnknown) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
113
|
+
throw new Error(`Unexpected arg: ${JSON.stringify(arg)}`);
|
|
114
|
+
}
|
|
115
|
+
(0, guard_1.assertDefined)(typedValue, `typedValue for ${resultKey}`);
|
|
116
|
+
const existing = result[resultKey];
|
|
117
|
+
if (param.multiple === true) {
|
|
118
|
+
const values = existing ?? [];
|
|
119
|
+
if (!Array.isArray(values)) {
|
|
120
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
121
|
+
throw new Error(`Expected an array: ${resultKey}`);
|
|
122
|
+
}
|
|
123
|
+
result[resultKey] ??= values;
|
|
124
|
+
values.push(typedValue);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
if (existing != null) {
|
|
128
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
129
|
+
throw new Error(`Too many ${(0, label_for_param_js_1.labelForParam)(param)}: ${JSON.stringify(existing)} ${JSON.stringify(arg)}`);
|
|
130
|
+
}
|
|
131
|
+
result[resultKey] = typedValue;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const requiredKeys = Object.entries(spec)
|
|
135
|
+
.filter(([, s]) => s.optional !== true)
|
|
136
|
+
.map(([key]) => key);
|
|
137
|
+
const missing = requiredKeys.filter((key) => !(key in result));
|
|
138
|
+
if (missing.length > 0) {
|
|
139
|
+
const message = missing.map((key) => (0, label_for_param_js_1.labelForParam)(spec[key])).join(" ");
|
|
140
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
141
|
+
throw new Error(`Missing required params: ${message}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
const error = err instanceof Error ? err : new Error("Unknown error");
|
|
146
|
+
if (onError != null) {
|
|
147
|
+
onError(error);
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
console.error(error.message);
|
|
152
|
+
process.exit(1);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
};
|
|
157
|
+
exports.commandParams = commandParams;
|
|
158
|
+
//# sourceMappingURL=command-params.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-params.js","sourceRoot":"","sources":["../../../ts/command-params.ts"],"names":[],"mappings":";;;AAAA,8CAAmD;AACnD,yDAA6E;AAI7E,6DAAqD;AAErD,yDAAsD;AA0CtD,MAAM,YAAY,GAAG,CAAC,IAAY,EAAE,KAAmB,EAAW,EAAE;IACnE,MAAM,YAAY,GAAG,IAAA,mCAAgB,EAAC,KAAK,CAAC,C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GAAG,EAAE,EAAE,CAAC,IAAA,kCAAa,EAAC,IAAI,CAAE,GAAG,CAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC3E,wCAAwC;YACxC,MAAM,IAAI,KAAK,CAAC,4BAA6B,OAAQ,EAAE,CAAC,CAAC;QAC1D,CAAC;IACF,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACvB,MAAM,KAAK,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;QACtE,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;YACrB,OAAO,CAAC,KAAK,CAAC,CAAC;YACf,OAAO,SAAiD,CAAC;QAC1D,CAAC;aAAM,CAAC;YACP,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC7B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjB,CAAC;IACF,CAAC;IACD,OAAO,MAAmC,CAAC;AAC5C,CAAC,CAAC;AAzIW,QAAA,aAAa,iBAyIxB"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Either } from "@rickosborne/typical";
|
|
2
|
+
/**
|
|
3
|
+
* Mixin for a parameter which is not required.
|
|
4
|
+
*/
|
|
5
|
+
type OptionalParam = {
|
|
6
|
+
optional: true;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Extend a param's return type to allow for undefined if the param
|
|
10
|
+
* is optional.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export type IfOptional<T, U> = T extends OptionalParam ? (U | undefined) : U;
|
|
14
|
+
/**
|
|
15
|
+
* Mixin for a parameter which may be repeated.
|
|
16
|
+
*/
|
|
17
|
+
type RepeatedParam = {
|
|
18
|
+
multiple: true;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Extend a parameter's return type to an array if the param may be repeated.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export type IfRepeated<T, U> = T extends RepeatedParam ? U[] : U;
|
|
25
|
+
type CommandParamWithNames = {
|
|
26
|
+
name?: string;
|
|
27
|
+
names?: string;
|
|
28
|
+
positional?: false;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* A parameter which accepts positional values.
|
|
32
|
+
*/
|
|
33
|
+
export type PositionalCommandParam = {
|
|
34
|
+
positional: true;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Mixin of properties which can be used to build `--help` text.
|
|
38
|
+
*/
|
|
39
|
+
export type DescribedParam = {
|
|
40
|
+
help?: string;
|
|
41
|
+
placeholder?: string;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Mixin for any type of command-line parameter.
|
|
45
|
+
*/
|
|
46
|
+
export type CommandParamBase = Either<CommandParamWithNames, PositionalCommandParam> & Partial<OptionalParam> & Partial<RepeatedParam> & DescribedParam;
|
|
47
|
+
/**
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
export type TypedCommandParam = CommandParamBase & {
|
|
51
|
+
type?: unknown;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Central logic for a single type of {@link CommandParam}
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
export type ParamHandler<P extends TypedCommandParam, V> = {
|
|
58
|
+
matchesParam(this: void, param: TypedCommandParam): param is P;
|
|
59
|
+
parseArg(this: void, text: string, param: P): V;
|
|
60
|
+
placeholderForParam(this: void, param: P): string | undefined;
|
|
61
|
+
};
|
|
62
|
+
export {};
|
|
63
|
+
//# sourceMappingURL=common.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../ts/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAEnD;;GAEG;AACH,KAAK,aAA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IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAChD,mBAAmB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,GAAG,SAAS,CAAC;CAC9D,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../ts/common.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type ParseLocalDateConfig } from "@rickosborne/foundation";
|
|
2
|
+
import type { CommandParamBase, ParamHandler, TypedCommandParam } from "./common.js";
|
|
3
|
+
/**
|
|
4
|
+
* Command-line parameter which can accept a date (and optionally time) value.
|
|
5
|
+
*/
|
|
6
|
+
export type DateCommandParam = CommandParamBase & ParseLocalDateConfig & {
|
|
7
|
+
/**
|
|
8
|
+
* Maximum acceptable value.
|
|
9
|
+
*/
|
|
10
|
+
maximum?: Date;
|
|
11
|
+
/**
|
|
12
|
+
* Minimum acceptable value.
|
|
13
|
+
*/
|
|
14
|
+
minimum?: Date;
|
|
15
|
+
/**
|
|
16
|
+
* Convert the text argument to the date it represents.
|
|
17
|
+
* @defaultValue {@link parseDateArg}
|
|
18
|
+
*/
|
|
19
|
+
parse?: (this: void, text: string, param: DateCommandParam) => Date;
|
|
20
|
+
type: typeof Date;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export declare const parseDateArg: (text: string, param: DateCommandParam) => Date;
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare const isDateParam: (param: TypedCommandParam) => param is DateCommandParam;
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare const dateParamHandler: ParamHandler<DateCommandParam, Date>;
|
|
34
|
+
//# sourceMappingURL=date-param.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-param.d.ts","sourceRoot":"","sources":["../../../ts/date-param.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,oBAAoB,EAAkB,MAAM,yBAAyB,CAAC;AACpF,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGrF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG,oBAAoB,GAAG;IACxE;;OAEG;IACH,OAAO,CAAC,EAAE,IAAI,CAAC;IACf;;OAEG;IACH,OAAO,CAAC,EAAE,IAAI,CAAC;IACf;;;OAGG;IACH,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAC;IACpE,IAAI,EAAE,OAAO,IAAI,CAAC;CAClB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,YAAY,SAAU,MAAM,SAAS,gBAAgB,KAAG,IASpE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,UAAW,iBAAiB,KAAG,KAAK,IAAI,gBAE/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,YAAY,CAAC,gBAAgB,EAAE,IAAI,CAMjE,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dateParamHandler = exports.isDateParam = exports.parseDateArg = void 0;
|
|
4
|
+
const foundation_1 = require("@rickosborne/foundation");
|
|
5
|
+
const label_for_param_js_1 = require("./label-for-param.js");
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
const parseDateArg = (text, param) => {
|
|
10
|
+
const date = (0, foundation_1.parseLocalDate)(text, param);
|
|
11
|
+
if (param.minimum != null && date < param.minimum) {
|
|
12
|
+
throw new RangeError(`${(0, label_for_param_js_1.labelForParam)(param)}: minimum=${param.minimum.toISOString()}`);
|
|
13
|
+
}
|
|
14
|
+
if (param.maximum != null && date > param.maximum) {
|
|
15
|
+
throw new RangeError(`${(0, label_for_param_js_1.labelForParam)(param)}: maximum=${param.maximum.toISOString()}`);
|
|
16
|
+
}
|
|
17
|
+
return date;
|
|
18
|
+
};
|
|
19
|
+
exports.parseDateArg = parseDateArg;
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
const isDateParam = (param) => {
|
|
24
|
+
return param.type === Date;
|
|
25
|
+
};
|
|
26
|
+
exports.isDateParam = isDateParam;
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
exports.dateParamHandler = {
|
|
31
|
+
matchesParam: exports.isDateParam,
|
|
32
|
+
parseArg: exports.parseDateArg,
|
|
33
|
+
placeholderForParam(param) {
|
|
34
|
+
return param.placeholder ?? param.time ? "(datetime)" : "(date)";
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=date-param.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-param.js","sourceRoot":"","sources":["../../../ts/date-param.ts"],"names":[],"mappings":";;;AAAA,wDAAoF;AAEpF,6DAAqD;AAsBrD;;GAEG;AACI,MAAM,YAAY,GAAG,CAAC,IAAY,EAAE,KAAuB,EAAQ,EAAE;IAC3E,MAAM,IAAI,GAAG,IAAA,2BAAc,EAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACzC,IAAI,KAAK,CAAC,OAAO,IAAI,IAAI,IAAI,IAAI,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CAAC,GAAI,IAAA,kCAAa,EAAC,KAAK,CAAE,aAAc,KAAK,CAAC,OAAO,CAAC,WAAW,EAAG,EAAE,CAAC,CAAC;IAC7F,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,IAAI,IAAI,IAAI,IAAI,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CAAC,GAAI,IAAA,kCAAa,EAAC,KAAK,CAAE,aAAc,KAAK,CAAC,OAAO,CAAC,WAAW,EAAG,EAAE,CAAC,CAAC;IAC7F,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC,CAAC;AATW,QAAA,YAAY,gBASvB;AAEF;;GAEG;AACI,MAAM,WAAW,GAAG,CAAC,KAAwB,EAA6B,EAAE;IAClF,OAAO,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;AAC5B,CAAC,CAAC;AAFW,QAAA,WAAW,eAEtB;AAEF;;GAEG;AACU,QAAA,gBAAgB,GAAyC;IACrE,YAAY,EAAE,mBAAW;IACzB,QAAQ,EAAE,oBAAY;IACtB,mBAAmB,CAAC,KAAuB;QAC1C,OAAO,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC;IAClE,CAAC;CACD,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Stats } from "node:fs";
|
|
2
|
+
import type { CommandParamBase, ParamHandler, TypedCommandParam } from "./common.js";
|
|
3
|
+
/**
|
|
4
|
+
* A command-line parameter which references a file.
|
|
5
|
+
*/
|
|
6
|
+
export type FileCommandParam = CommandParamBase & {
|
|
7
|
+
/**
|
|
8
|
+
* If true, the file must exist. If false, the file must
|
|
9
|
+
* not exist. If undefined, an error will be thrown if
|
|
10
|
+
* the path exists but is not a file.
|
|
11
|
+
*/
|
|
12
|
+
existing?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Validate and transform the argument before it is returned.
|
|
15
|
+
* @defaultValue {@link parseFileArg}
|
|
16
|
+
*/
|
|
17
|
+
parse?: (this: void, text: string, param: FileCommandParam) => FileArg;
|
|
18
|
+
/**
|
|
19
|
+
* Prepend this path if it is not already provided.
|
|
20
|
+
*/
|
|
21
|
+
path?: string;
|
|
22
|
+
type: "file";
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* File argument resolved from a file command-line parameter.
|
|
26
|
+
*/
|
|
27
|
+
export type FileArg = {
|
|
28
|
+
/**
|
|
29
|
+
* Path to the file.
|
|
30
|
+
*/
|
|
31
|
+
filePath: string;
|
|
32
|
+
/**
|
|
33
|
+
* If the file exists, the filesystem stats for it.
|
|
34
|
+
*/
|
|
35
|
+
stats: Stats | undefined;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Validate and transform the argument before it is returned.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export declare const parseFileArg: (text: string, param: FileCommandParam) => FileArg;
|
|
42
|
+
/**
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
export declare const isFileParam: (param: TypedCommandParam) => param is FileCommandParam;
|
|
46
|
+
/**
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
export declare const fileParamHandler: ParamHandler<FileCommandParam, FileArg>;
|
|
50
|
+
//# sourceMappingURL=file-param.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-param.d.ts","sourceRoot":"","sources":["../../../ts/file-param.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG;IACjD;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;OAGG;IACH,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB,KAAK,OAAO,CAAC;IACvE;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;CACzB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,SAAU,MAAM,SAAS,gBAAgB,KAAG,OAapE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,UAAW,iBAAiB,KAAG,KAAK,IAAI,gBAE/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,YAAY,CAAC,gBAAgB,EAAE,OAAO,CAMpE,CAAC"}
|