@thi.ng/args 2.4.0 → 2.6.0

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2025-06-27T11:09:55Z
3
+ - **Last updated**: 2025-06-28T06:35:49Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -11,6 +11,22 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
11
11
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
12
12
  and/or version bumps of transitive dependencies.
13
13
 
14
+ ## [2.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.6.0) (2025-06-28)
15
+
16
+ #### 🚀 Features
17
+
18
+ - emit colorized error messages in cliApp() ([36f66b6](https://github.com/thi-ng/umbrella/commit/36f66b6))
19
+
20
+ ## [2.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.5.0) (2025-06-27)
21
+
22
+ #### 🚀 Features
23
+
24
+ - update ColorTheme, add color support for cmd list ([75b1ef1](https://github.com/thi-ng/umbrella/commit/75b1ef1))
25
+ - add `ColorTheme.command` option
26
+ - migrate internal formatting helpers to own file
27
+ - refactor cliApp() & usage() internals
28
+ - add thi.ng logo header tpl ([838e417](https://github.com/thi-ng/umbrella/commit/838e417))
29
+
14
30
  ## [2.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.4.0) (2025-06-27)
15
31
 
16
32
  #### 🚀 Features
package/api.d.ts CHANGED
@@ -164,6 +164,7 @@ export interface UsageOpts {
164
164
  */
165
165
  export interface ColorTheme {
166
166
  default: number;
167
+ command: number;
167
168
  hint: number;
168
169
  multi: number;
169
170
  param: number;
package/api.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const DEFAULT_THEME = {
2
2
  default: 95,
3
+ command: 92,
3
4
  hint: 90,
4
5
  multi: 90,
5
6
  param: 96,
package/cli.js CHANGED
@@ -1,12 +1,18 @@
1
1
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
2
2
  import { StreamLogger } from "@thi.ng/logger/stream";
3
- import { padRight } from "@thi.ng/strings/pad-right";
4
3
  import { PRESET_ANSI16, PRESET_NONE } from "@thi.ng/text-format/presets";
5
4
  import { parse } from "./parse.js";
6
- import { __wrapWithIndent, usage } from "./usage.js";
5
+ import { usage } from "./usage.js";
6
+ import {
7
+ __ansi,
8
+ __colorTheme,
9
+ __padRightAnsi,
10
+ __wrapWithIndent
11
+ } from "./utils.js";
7
12
  const cliApp = async (config) => {
8
13
  const argv = config.argv || process.argv;
9
14
  const isColor = !process.env.NO_COLOR;
15
+ const format = isColor ? PRESET_ANSI16 : PRESET_NONE;
10
16
  const usageOpts = {
11
17
  prefix: "",
12
18
  color: isColor,
@@ -23,26 +29,28 @@ const cliApp = async (config) => {
23
29
  } else {
24
30
  cmdID = argv[start];
25
31
  cmd = config.commands[cmdID];
26
- usageOpts.prefix += __descriptions(
27
- config.commands,
28
- config.usage.lineWidth
29
- );
30
- if (!cmd) __usageAndExit(config, usageOpts);
32
+ if (!cmd) {
33
+ usageOpts.prefix += __descriptions(config.commands, usageOpts);
34
+ __usageAndExit(config, usageOpts);
35
+ }
31
36
  start++;
32
37
  }
33
38
  let parsed;
34
39
  try {
35
40
  parsed = parse({ ...config.opts, ...cmd.opts }, argv, {
36
- showUsage: true,
41
+ showUsage: false,
37
42
  usageOpts,
38
43
  start
39
44
  });
40
- } catch (_) {
45
+ } catch (e) {
46
+ __printError(e.message, format);
47
+ }
48
+ if (!parsed) {
49
+ __usageAndExit(config, usageOpts);
50
+ process.exit(1);
41
51
  }
42
- if (!parsed) process.exit(1);
43
52
  if (cmd.inputs !== void 0 && cmd.inputs !== parsed.rest.length) {
44
- process.stderr.write(`expected ${cmd.inputs || 0} input(s)
45
- `);
53
+ __printError(`expected ${cmd.inputs || 0} input(s)`, format);
46
54
  __usageAndExit(config, usageOpts);
47
55
  }
48
56
  const ctx = await config.ctx(
@@ -57,7 +65,7 @@ const cliApp = async (config) => {
57
65
  await cmd.fn(ctx);
58
66
  if (config.post) await config.post(ctx, cmd);
59
67
  } catch (e) {
60
- process.stderr.write(e.message + "\n\n");
68
+ __printError(e.message, format);
61
69
  process.exit(1);
62
70
  }
63
71
  };
@@ -65,13 +73,17 @@ const __usageAndExit = (config, usageOpts) => {
65
73
  process.stderr.write(usage(config.opts, usageOpts));
66
74
  process.exit(1);
67
75
  };
68
- const __descriptions = (commands, lineWidth = 80) => {
76
+ const __descriptions = (commands, { color, lineWidth = 80 } = {}) => {
69
77
  const names = Object.keys(commands);
70
78
  const maxLength = Math.max(...names.map((x) => x.length));
79
+ const theme = __colorTheme(color);
71
80
  return [
72
81
  "\nAvailable commands:\n",
73
82
  ...names.map(
74
- (x) => `${padRight(maxLength)(x)} : ${__wrapWithIndent(
83
+ (x) => `${__padRightAnsi(
84
+ __ansi(x, theme.command),
85
+ maxLength
86
+ )} : ${__wrapWithIndent(
75
87
  commands[x].desc,
76
88
  maxLength + 3,
77
89
  lineWidth
@@ -80,6 +92,7 @@ const __descriptions = (commands, lineWidth = 80) => {
80
92
  "\n"
81
93
  ].join("\n");
82
94
  };
95
+ const __printError = (msg, fmt) => process.stderr.write(fmt.red(msg) + "\n\n");
83
96
  export {
84
97
  cliApp
85
98
  };
package/header.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Re-usable logo header for various thi.ng CLI tools.
3
+ *
4
+ * @param name
5
+ * @param version
6
+ * @param desc
7
+ *
8
+ * @internal
9
+ */
10
+ export declare const THING_HEADER: (name: string, version: string, desc: string) => string;
11
+ //# sourceMappingURL=header.d.ts.map
package/header.js ADDED
@@ -0,0 +1,10 @@
1
+ const THING_HEADER = (name, version, desc) => `
2
+ \u2588 \u2588 \u2588 \u2502
3
+ \u2588\u2588 \u2588 \u2502
4
+ \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2502 ${name} v${version}
5
+ \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2502 ${desc}
6
+ \u2588 \u2502
7
+ \u2588 \u2588 \u2502`;
8
+ export {
9
+ THING_HEADER
10
+ };
package/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./api.js";
2
2
  export * from "./args.js";
3
3
  export * from "./cli.js";
4
4
  export * from "./coerce.js";
5
+ export * from "./header.js";
5
6
  export * from "./parse.js";
6
7
  export * from "./usage.js";
7
8
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -2,5 +2,6 @@ export * from "./api.js";
2
2
  export * from "./args.js";
3
3
  export * from "./cli.js";
4
4
  export * from "./coerce.js";
5
+ export * from "./header.js";
5
6
  export * from "./parse.js";
6
7
  export * from "./usage.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/args",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "Declarative, functional CLI argument/options parser, value coercions, sub-commands etc.",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -96,6 +96,9 @@
96
96
  "./coerce": {
97
97
  "default": "./coerce.js"
98
98
  },
99
+ "./header": {
100
+ "default": "./header.js"
101
+ },
99
102
  "./parse": {
100
103
  "default": "./parse.js"
101
104
  },
@@ -107,5 +110,5 @@
107
110
  "tag": "cli",
108
111
  "year": 2018
109
112
  },
110
- "gitHead": "7fa97c11435e6f616bac62b731050784694ee08d\n"
113
+ "gitHead": "42ee16fdd391d5e04daeae9817f6c7dc378e371e\n"
111
114
  }
package/usage.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  import type { IObjectOf } from "@thi.ng/api";
2
2
  import { type Args, type UsageOpts } from "./api.js";
3
3
  export declare const usage: <T extends IObjectOf<any>>(specs: Args<T>, opts?: Partial<UsageOpts>) => string;
4
- /** @internal */
5
- export declare const __wrapWithIndent: (body: string, indent: number, width: number) => string;
6
4
  //# sourceMappingURL=usage.d.ts.map
package/usage.js CHANGED
@@ -1,13 +1,14 @@
1
- import { isPlainObject } from "@thi.ng/checks/is-plain-object";
2
- import { lengthAnsi } from "@thi.ng/strings/ansi";
3
1
  import { capitalize, kebab } from "@thi.ng/strings/case";
4
- import { padRight } from "@thi.ng/strings/pad-right";
5
- import { repeat } from "@thi.ng/strings/repeat";
6
2
  import { stringify } from "@thi.ng/strings/stringify";
7
- import { SPLIT_ANSI, wordWrapLines } from "@thi.ng/strings/word-wrap";
8
3
  import {
9
- DEFAULT_THEME
10
4
  } from "./api.js";
5
+ import {
6
+ __ansi,
7
+ __colorTheme,
8
+ __padRightAnsi,
9
+ __wrap,
10
+ __wrapWithIndent
11
+ } from "./utils.js";
11
12
  const usage = (specs, opts = {}) => {
12
13
  opts = {
13
14
  lineWidth: 80,
@@ -18,7 +19,7 @@ const usage = (specs, opts = {}) => {
18
19
  groups: ["flags", "main"],
19
20
  ...opts
20
21
  };
21
- const theme = isPlainObject(opts.color) ? { ...DEFAULT_THEME, ...opts.color } : opts.color ? DEFAULT_THEME : {};
22
+ const theme = __colorTheme(opts.color);
22
23
  const format = (ids) => ids.map(
23
24
  (id) => __argUsage(id, specs[id], opts, theme, opts.paramWidth)
24
25
  );
@@ -52,7 +53,7 @@ const __argUsage = (id, spec, opts, theme, indent) => {
52
53
  isRequired && prefixes.push("required");
53
54
  spec.multi && prefixes.push("multiple");
54
55
  const body = __argPrefix(prefixes, theme, isRequired) + (spec.desc || "") + __argDefault(spec, opts, theme);
55
- return padRight(opts.paramWidth)(params, lengthAnsi(params)) + __wrapWithIndent(body, indent, opts.lineWidth);
56
+ return __padRightAnsi(params, opts.paramWidth) + __wrapWithIndent(body, indent, opts.lineWidth);
56
57
  };
57
58
  const __argHint = (spec, theme) => spec.hint ? __ansi(" " + spec.hint, theme.hint) : "";
58
59
  const __argAlias = (spec, theme, hint) => spec.alias ? `${__ansi("-" + spec.alias, theme.param)}${hint}, ` : "";
@@ -66,17 +67,6 @@ const __argDefault = (spec, opts, theme) => opts.showDefaults && spec.default !=
66
67
  )})`,
67
68
  theme.default
68
69
  ) : "";
69
- const __ansi = (x, col) => col != null ? `\x1B[${col}m${x}\x1B[0m` : x;
70
- const __wrap = (str, width) => str ? wordWrapLines(str, {
71
- width,
72
- splitter: SPLIT_ANSI,
73
- hard: false
74
- }) : [];
75
- const __wrapWithIndent = (body, indent, width) => {
76
- const prefix = repeat(" ", indent);
77
- return __wrap(body, width - indent).map((l, i) => i ? prefix + l : l).join("\n");
78
- };
79
70
  export {
80
- __wrapWithIndent,
81
71
  usage
82
72
  };
package/utils.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { Maybe } from "@thi.ng/api";
2
+ import { type ColorTheme } from "./api.js";
3
+ /** @internal */
4
+ export declare const __ansi: (x: string, col: number) => string;
5
+ /** @internal */
6
+ export declare const __padRightAnsi: (x: string, width: number) => string;
7
+ /** @internal */
8
+ export declare const __wrap: (str: Maybe<string>, width: number) => import("@thi.ng/strings/word-wrap").Line[];
9
+ /** @internal */
10
+ export declare const __wrapWithIndent: (body: string, indent: number, width: number) => string;
11
+ /** @internal */
12
+ export declare const __colorTheme: (color?: boolean | Partial<ColorTheme>) => ColorTheme;
13
+ //# sourceMappingURL=utils.d.ts.map
package/utils.js ADDED
@@ -0,0 +1,25 @@
1
+ import { isPlainObject } from "@thi.ng/checks/is-plain-object";
2
+ import { repeat } from "@thi.ng/strings/repeat";
3
+ import { SPLIT_ANSI, wordWrapLines } from "@thi.ng/strings/word-wrap";
4
+ import { DEFAULT_THEME } from "./api.js";
5
+ import { lengthAnsi } from "@thi.ng/strings/ansi";
6
+ import { padRight } from "@thi.ng/strings/pad-right";
7
+ const __ansi = (x, col) => col != null ? `\x1B[${col}m${x}\x1B[0m` : x;
8
+ const __padRightAnsi = (x, width) => padRight(width)(x, lengthAnsi(x));
9
+ const __wrap = (str, width) => str ? wordWrapLines(str, {
10
+ width,
11
+ splitter: SPLIT_ANSI,
12
+ hard: false
13
+ }) : [];
14
+ const __wrapWithIndent = (body, indent, width) => {
15
+ const prefix = repeat(" ", indent);
16
+ return __wrap(body, width - indent).map((l, i) => i ? prefix + l : l).join("\n");
17
+ };
18
+ const __colorTheme = (color) => isPlainObject(color) ? { ...DEFAULT_THEME, ...color } : color ? DEFAULT_THEME : {};
19
+ export {
20
+ __ansi,
21
+ __colorTheme,
22
+ __padRightAnsi,
23
+ __wrap,
24
+ __wrapWithIndent
25
+ };