@thi.ng/args 2.6.3 → 2.7.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-07-09T09:42:08Z
3
+ - **Last updated**: 2025-07-10T20:04:18Z
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,17 @@ 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.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.7.0) (2025-07-10)
15
+
16
+ #### 🚀 Features
17
+
18
+ - add `terminalLineWidth()` helper ([e8fdd27](https://github.com/thi-ng/umbrella/commit/e8fdd27))
19
+ - update ColorTheme and error msg output ([c2bca13](https://github.com/thi-ng/umbrella/commit/c2bca13))
20
+
21
+ #### ♻️ Refactoring
22
+
23
+ - simplify error handling in cliApp() ([9973cf0](https://github.com/thi-ng/umbrella/commit/9973cf0))
24
+
14
25
  ### [2.6.3](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.6.3) (2025-07-09)
15
26
 
16
27
  #### 🩹 Bug fixes
package/api.d.ts CHANGED
@@ -165,6 +165,7 @@ export interface UsageOpts {
165
165
  export interface ColorTheme {
166
166
  default: number;
167
167
  command: number;
168
+ error: number;
168
169
  hint: number;
169
170
  multi: number;
170
171
  param: number;
package/api.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const DEFAULT_THEME = {
2
2
  default: 95,
3
3
  command: 92,
4
+ error: 91,
4
5
  hint: 90,
5
6
  multi: 90,
6
7
  param: 96,
package/cli.d.ts CHANGED
@@ -1,3 +1,10 @@
1
1
  import type { CLIAppConfig, CommandCtx } from "./api.js";
2
2
  export declare const cliApp: <OPTS extends object, CTX extends CommandCtx<OPTS, OPTS>>(config: CLIAppConfig<OPTS, CTX>) => Promise<void>;
3
+ /**
4
+ * Calls `tput cols` to obtain the number of columns in the current
5
+ * terminal. Returns `fallback` in case of error.
6
+ *
7
+ * @param fallback
8
+ */
9
+ export declare const terminalLineWidth: (fallback?: number) => number;
3
10
  //# sourceMappingURL=cli.d.ts.map
package/cli.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
2
2
  import { StreamLogger } from "@thi.ng/logger/stream";
3
3
  import { PRESET_ANSI16, PRESET_NONE } from "@thi.ng/text-format/presets";
4
+ import { execFileSync } from "node:child_process";
4
5
  import { parse } from "./parse.js";
5
6
  import { usage } from "./usage.js";
6
7
  import {
@@ -12,7 +13,7 @@ import {
12
13
  const cliApp = async (config) => {
13
14
  const argv = config.argv || process.argv;
14
15
  const isColor = !process.env.NO_COLOR;
15
- const format = isColor ? PRESET_ANSI16 : PRESET_NONE;
16
+ const theme = __colorTheme(isColor);
16
17
  const usageOpts = {
17
18
  prefix: "",
18
19
  color: isColor,
@@ -38,19 +39,15 @@ const cliApp = async (config) => {
38
39
  let parsed;
39
40
  try {
40
41
  parsed = parse({ ...config.opts, ...cmd.opts }, argv, {
41
- showUsage: false,
42
42
  usageOpts,
43
43
  start
44
44
  });
45
45
  } catch (e) {
46
- __printError(e.message, format);
47
- }
48
- if (!parsed) {
49
- __usageAndExit(config, usageOpts);
50
46
  process.exit(1);
51
47
  }
48
+ if (!parsed) process.exit(0);
52
49
  if (cmd.inputs !== void 0 && cmd.inputs !== parsed.rest.length) {
53
- __printError(`expected ${cmd.inputs || 0} input(s)`, format);
50
+ __printError(`expected ${cmd.inputs || 0} input(s)`, theme);
54
51
  __usageAndExit(config, usageOpts);
55
52
  }
56
53
  const ctx = await config.ctx(
@@ -65,7 +62,7 @@ const cliApp = async (config) => {
65
62
  await cmd.fn(ctx);
66
63
  if (config.post) await config.post(ctx, cmd);
67
64
  } catch (e) {
68
- __printError(e.message, format);
65
+ __printError(e.message, theme);
69
66
  process.exit(1);
70
67
  }
71
68
  };
@@ -92,7 +89,15 @@ const __descriptions = (commands, { color, lineWidth = 80 } = {}) => {
92
89
  "\n"
93
90
  ].join("\n");
94
91
  };
95
- const __printError = (msg, fmt) => process.stderr.write(fmt.red(msg) + "\n\n");
92
+ const __printError = (msg, theme) => process.stderr.write(__ansi(msg, theme.error) + "\n\n");
93
+ const terminalLineWidth = (fallback = 80) => {
94
+ try {
95
+ return +execFileSync("tput", ["cols"], { encoding: "ascii" });
96
+ } catch (e) {
97
+ return fallback;
98
+ }
99
+ };
96
100
  export {
97
- cliApp
101
+ cliApp,
102
+ terminalLineWidth
98
103
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/args",
3
- "version": "2.6.3",
3
+ "version": "2.7.0",
4
4
  "description": "Declarative, functional CLI argument/options parser, value coercions, sub-commands etc.",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -39,17 +39,17 @@
39
39
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
40
40
  },
41
41
  "dependencies": {
42
- "@thi.ng/api": "^8.11.29",
43
- "@thi.ng/checks": "^3.7.9",
44
- "@thi.ng/errors": "^2.5.35",
45
- "@thi.ng/logger": "^3.1.10",
46
- "@thi.ng/strings": "^3.9.15",
47
- "@thi.ng/text-format": "^2.2.34"
42
+ "@thi.ng/api": "^8.11.30",
43
+ "@thi.ng/checks": "^3.7.10",
44
+ "@thi.ng/errors": "^2.5.36",
45
+ "@thi.ng/logger": "^3.1.11",
46
+ "@thi.ng/strings": "^3.9.16",
47
+ "@thi.ng/text-format": "^2.2.35"
48
48
  },
49
49
  "devDependencies": {
50
- "@types/node": "^22.15.21",
51
- "esbuild": "^0.25.5",
52
- "typedoc": "^0.28.5",
50
+ "@types/node": "^24.0.12",
51
+ "esbuild": "^0.25.6",
52
+ "typedoc": "^0.28.7",
53
53
  "typescript": "^5.8.3"
54
54
  },
55
55
  "keywords": [
@@ -110,5 +110,5 @@
110
110
  "tag": "cli",
111
111
  "year": 2018
112
112
  },
113
- "gitHead": "e657c2d66574c18343a6797aef4585945729093e\n"
113
+ "gitHead": "c6e23f650c9c5b35e1abb9c450a7eb67c5ab6a8e\n"
114
114
  }
package/parse.js CHANGED
@@ -3,6 +3,7 @@ import { defError } from "@thi.ng/errors/deferror";
3
3
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
4
4
  import { camel } from "@thi.ng/strings/case";
5
5
  import { usage } from "./usage.js";
6
+ import { __ansi, __colorTheme } from "./utils.js";
6
7
  const ParseError = defError(() => "parse error");
7
8
  const parse = (specs, argv, opts) => {
8
9
  opts = { start: 2, showUsage: true, help: ["--help", "-h"], ...opts };
@@ -11,7 +12,10 @@ const parse = (specs, argv, opts) => {
11
12
  } catch (e) {
12
13
  if (opts.showUsage) {
13
14
  console.log(
14
- e.message + "\n\n" + usage(specs, opts.usageOpts)
15
+ __ansi(
16
+ e.message,
17
+ __colorTheme(opts.usageOpts?.color).error
18
+ ) + "\n\n" + usage(specs, opts.usageOpts)
15
19
  );
16
20
  }
17
21
  throw new ParseError(e.message);
package/utils.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Maybe } from "@thi.ng/api";
2
2
  import { type ColorTheme } from "./api.js";
3
3
  /** @internal */
4
- export declare const __ansi: (x: string, col: number) => string;
4
+ export declare const __ansi: (x: string, col?: number) => string;
5
5
  /** @internal */
6
6
  export declare const __padRightAnsi: (x: string, width: number) => string;
7
7
  /** @internal */
package/utils.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { isPlainObject } from "@thi.ng/checks/is-plain-object";
2
+ import { lengthAnsi } from "@thi.ng/strings/ansi";
3
+ import { padRight } from "@thi.ng/strings/pad-right";
2
4
  import { repeat } from "@thi.ng/strings/repeat";
3
5
  import { SPLIT_ANSI, wordWrapLines } from "@thi.ng/strings/word-wrap";
4
6
  import { DEFAULT_THEME } from "./api.js";
5
- import { lengthAnsi } from "@thi.ng/strings/ansi";
6
- import { padRight } from "@thi.ng/strings/pad-right";
7
7
  const __ansi = (x, col) => col != null ? `\x1B[${col}m${x}\x1B[0m` : x;
8
8
  const __padRightAnsi = (x, width) => padRight(width)(x, lengthAnsi(x));
9
9
  const __wrap = (str, width) => str ? wordWrapLines(str, {
@@ -15,7 +15,7 @@ const __wrapWithIndent = (body, indent, width) => {
15
15
  const prefix = repeat(" ", indent);
16
16
  return __wrap(body, width - indent).map((l, i) => i ? prefix + l : l).join("\n");
17
17
  };
18
- const __colorTheme = (color) => isPlainObject(color) ? { ...DEFAULT_THEME, ...color } : color ? DEFAULT_THEME : {};
18
+ const __colorTheme = (color) => isPlainObject(color) ? { ...DEFAULT_THEME, ...color } : color !== false ? DEFAULT_THEME : {};
19
19
  export {
20
20
  __ansi,
21
21
  __colorTheme,