cli-argv-util 1.4.1 → 1.5.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/README.md CHANGED
@@ -33,7 +33,7 @@ if (cli.flagOn.noSummary)
33
33
  console.info('You supplied', cli.params.length , 'CLI parameter(s).');
34
34
  ```
35
35
  For a real world example, see:
36
- [cli.js](https://github.com/center-key/copy-file-util/blob/main/bin/cli.js)
36
+ [copy-file.ts](https://github.com/center-key/copy-file-util/blob/main/src/copy-file.ts)
37
37
 
38
38
  If your CLI tool is named `my-program` and a user runs it like:
39
39
  ```shell
@@ -45,6 +45,9 @@ the resulting `cli` object will be:
45
45
  flagMap: {
46
46
  cd: 'src',
47
47
  },
48
+ flagMapRaw: {
49
+ cd: 'src',
50
+ },
48
51
  flagOn: {
49
52
  cd: true,
50
53
  find: false,
@@ -58,13 +61,18 @@ the resulting `cli` object will be:
58
61
  > [!NOTE]
59
62
  > _Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._
60
63
 
64
+ > [!NOTE]
65
+ > _CLI flag values support escaped charcters and macros._<br>
66
+ > _For documentation, see:_ https://github.com/center-key/replacer-util
67
+
61
68
  ## C) Results
62
69
  The `cliArgvUtil.parse()` returns an object of type `Result`:
63
70
  ```typescript
64
71
  export type StringFlagMap = { [flag: string]: string | undefined };
65
72
  export type BooleanFlagMap = { [flag: string]: boolean };
66
73
  export type Result = {
67
- flagMap: StringFlagMap, //map of flag values for each user supplied flag
74
+ flagMap: StringFlagMap, //map of unescaped flag values for each user supplied flag
75
+ flagMapRaw: StringFlagMap, //map of flag values for each user supplied flag
68
76
  flagOn: BooleanFlagMap, //map of the enabled status for all valid flags
69
77
  invalidFlag: string | null, //name of the first invalid flag
70
78
  invalidFlagMsg: string | null, //error message for the invalid flag
@@ -1,4 +1,4 @@
1
- //! cli-argv-util v1.4.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
1
+ //! cli-argv-util v1.5.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
2
2
 
3
3
  export type StringFlagMap = {
4
4
  [flag: string]: string | undefined;
@@ -16,13 +16,25 @@ export type Ancestor = {
16
16
  };
17
17
  export type Result = {
18
18
  flagMap: StringFlagMap;
19
+ flagMapRaw: StringFlagMap;
19
20
  flagOn: BooleanFlagMap;
20
21
  invalidFlag: string | null;
21
22
  invalidFlagMsg: string | null;
22
- params: string[];
23
23
  paramCount: number;
24
+ params: string[];
25
+ };
26
+ type Json = string | number | boolean | null | undefined | JsonObject | Json[];
27
+ type JsonObject = {
28
+ [key: string]: Json;
24
29
  };
25
30
  declare const cliArgvUtil: {
31
+ assert(ok: unknown, message: string | null): void;
32
+ readPackageJson(): JsonObject;
33
+ escapers: {
34
+ regex: RegExp;
35
+ char: string;
36
+ }[];
37
+ unescape(text: string, pkg: JsonObject): string;
26
38
  parse(validFlags: string[]): Result;
27
39
  run(packageJson: {
28
40
  [key: string]: unknown;
@@ -1,4 +1,4 @@
1
- //! cli-argv-util v1.4.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
1
+ //! cli-argv-util v1.5.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
2
2
 
3
3
  import { execSync } from 'node:child_process';
4
4
  import chalk from 'chalk';
@@ -6,20 +6,75 @@ import fs from 'fs';
6
6
  import path from 'path';
7
7
  import slash from 'slash';
8
8
  const cliArgvUtil = {
9
+ assert(ok, message) {
10
+ if (!ok)
11
+ throw new Error(`[replacer-util] ${message}`);
12
+ },
13
+ readPackageJson() {
14
+ const pkgFile = 'package.json';
15
+ const getPkg = () => JSON.parse(fs.readFileSync(pkgFile, 'utf-8'));
16
+ const pkg = fs.existsSync(pkgFile) ? getPkg() : {};
17
+ const fixHiddenKeys = (pkgObj) => {
18
+ const unhide = (key) => {
19
+ const newKey = key.replace(/[@./]/g, '-');
20
+ if (!pkgObj[newKey])
21
+ pkgObj[newKey] = pkgObj[key];
22
+ };
23
+ Object.keys(pkgObj).forEach(unhide);
24
+ };
25
+ if (pkg.dependencies)
26
+ fixHiddenKeys(pkg.dependencies);
27
+ if (pkg.devDependencies)
28
+ fixHiddenKeys(pkg.devDependencies);
29
+ if (!pkg.cliConfig && pkg.replacerConfig)
30
+ pkg.cliConfig = pkg.replacerConfig;
31
+ return pkg;
32
+ },
33
+ escapers: [
34
+ { regex: /{{apos}}/g, char: "'" },
35
+ { regex: /{{bang}}/g, char: '!' },
36
+ { regex: /{{close-curly}}/g, char: '}' },
37
+ { regex: /{{equals}}/g, char: '=' },
38
+ { regex: /{{gt}}/g, char: '>' },
39
+ { regex: /{{hash}}/g, char: '#' },
40
+ { regex: /{{lt}}/g, char: '<' },
41
+ { regex: /{{open-curly}}/g, char: '{' },
42
+ { regex: /{{pipe}}/g, char: '|' },
43
+ { regex: /{{quote}}/g, char: '"' },
44
+ { regex: /{{semi}}/g, char: ';' },
45
+ { regex: /{{space}}/g, char: ' ' },
46
+ ],
47
+ unescape(text, pkg) {
48
+ const macroPattern = /^{{macro:(.*)}}$/;
49
+ const macros = (pkg.cliConfig?.macros ?? {});
50
+ const macroName = text.match(macroPattern)?.[1];
51
+ const macroValue = macros[macroName];
52
+ const expandedText = macroName ? macroValue : text;
53
+ const missing = macroName && !macroValue;
54
+ cliArgvUtil.assert(!missing, `Macro "${macroName}" used but not defined in package.json`);
55
+ const replace = (flagValue, escaper) => flagValue.replace(escaper.regex, escaper.char);
56
+ return cliArgvUtil.escapers.reduce(replace, expandedText);
57
+ },
9
58
  parse(validFlags) {
59
+ const pkg = cliArgvUtil.readPackageJson();
10
60
  const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
11
61
  const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
12
62
  const toPair = (flag) => flag.replace(/^--/, '').split('=');
63
+ const unescape = (value) => !value ? undefined : cliArgvUtil.unescape(value, pkg);
13
64
  const args = cliArgvUtil.unquoteArgs(process.argv.slice(2));
14
65
  const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
15
- const flagMap = Object.fromEntries(pairs.map(toEntry));
16
- const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
66
+ const flagMapRaw = Object.fromEntries(pairs.map(toEntry));
67
+ const flagEntries = Object.entries(flagMapRaw);
68
+ const flagMap = Object.fromEntries(flagEntries.map(pair => [pair[0], unescape(pair[1])]));
69
+ const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMapRaw]);
17
70
  const flagOn = Object.fromEntries(onEntries);
18
71
  const invalidFlag = pairs.find(pair => !validFlags.includes(pair[0]))?.[0] ?? null;
19
72
  const helpMsg = '\nValid flags are --' + validFlags.join(' --');
20
- const params = args.filter(arg => !/^--/.test(arg));
73
+ const rawParams = args.filter(arg => !/^--/.test(arg));
74
+ const params = rawParams.map(param => cliArgvUtil.unescape(param, pkg));
21
75
  return {
22
76
  flagMap: flagMap,
77
+ flagMapRaw: flagMapRaw,
23
78
  flagOn: flagOn,
24
79
  invalidFlag: invalidFlag,
25
80
  invalidFlagMsg: invalidFlag ? 'Invalid flag: --' + invalidFlag + helpMsg : null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cli-argv-util",
3
- "version": "1.4.1",
3
+ "version": "1.5.1",
4
4
  "description": "Simple utility to parse command line parameters and flags (arguments vector)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -34,6 +34,13 @@
34
34
  "node": true,
35
35
  "mocha": true
36
36
  },
37
+ "cliConfig": {
38
+ "macros": {
39
+ "html-filename": "file.html",
40
+ "png-filename": "file.png",
41
+ "lucky-number": "777"
42
+ }
43
+ },
37
44
  "runScriptsConfig": {
38
45
  "clean": [
39
46
  "rimraf build dist"
@@ -56,17 +63,17 @@
56
63
  "slash": "~5.1"
57
64
  },
58
65
  "devDependencies": {
59
- "@eslint/js": "~9.39",
60
- "@types/node": "~25.0",
66
+ "@eslint/js": "~10.0",
67
+ "@types/node": "~25.5",
61
68
  "add-dist-header": "~1.6",
62
69
  "assert-deep-strict-equal": "~1.2",
63
70
  "copy-file-util": "~1.3",
64
- "eslint": "~9.39",
71
+ "eslint": "~10.1",
65
72
  "jshint": "~2.13",
66
73
  "mocha": "~11.7",
67
74
  "rimraf": "~6.1",
68
75
  "run-scripts-util": "~1.3",
69
76
  "typescript": "~5.9",
70
- "typescript-eslint": "~8.52"
77
+ "typescript-eslint": "~8.57"
71
78
  }
72
79
  }