@thi.ng/args 2.6.4 → 2.7.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2025-07-10T14:20:23Z
3
+ - **Last updated**: 2025-07-11T08:24:44Z
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,24 @@ 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.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.7.1) (2025-07-11)
15
+
16
+ #### 🩹 Bug fixes
17
+
18
+ - update arg names in parse errors ([5e82c16](https://github.com/thi-ng/umbrella/commit/5e82c16))
19
+ - use kebab-case arg names
20
+
21
+ ## [2.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.7.0) (2025-07-10)
22
+
23
+ #### 🚀 Features
24
+
25
+ - add `terminalLineWidth()` helper ([e8fdd27](https://github.com/thi-ng/umbrella/commit/e8fdd27))
26
+ - update ColorTheme and error msg output ([c2bca13](https://github.com/thi-ng/umbrella/commit/c2bca13))
27
+
28
+ #### ♻️ Refactoring
29
+
30
+ - simplify error handling in cliApp() ([9973cf0](https://github.com/thi-ng/umbrella/commit/9973cf0))
31
+
14
32
  ### [2.6.3](https://github.com/thi-ng/umbrella/tree/@thi.ng/args@2.6.3) (2025-07-09)
15
33
 
16
34
  #### 🩹 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.4",
3
+ "version": "2.7.1",
4
4
  "description": "Declarative, functional CLI argument/options parser, value coercions, sub-commands etc.",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -110,5 +110,5 @@
110
110
  "tag": "cli",
111
111
  "year": 2018
112
112
  },
113
- "gitHead": "56d8f088389b22192a06e9a395b5eecebf47697a\n"
113
+ "gitHead": "cc6ab918acdac66990d04d6751d19c573a09730d\n"
114
114
  }
package/parse.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { isArray } from "@thi.ng/checks/is-array";
2
2
  import { defError } from "@thi.ng/errors/deferror";
3
3
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
4
- import { camel } from "@thi.ng/strings/case";
4
+ import { camel, kebab } 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);
@@ -41,7 +45,7 @@ const __parseOpts = (specs, argv, opts) => {
41
45
  i++;
42
46
  }
43
47
  }
44
- id && illegalArgs(`missing value for: --${id}`);
48
+ id && illegalArgs(`missing value for: --${kebab(id)}`);
45
49
  return {
46
50
  result: __processResults(specs, acc),
47
51
  index: i,
@@ -90,7 +94,7 @@ const __processResults = (specs, acc) => {
90
94
  if (spec.default !== void 0) {
91
95
  acc[id] = spec.default;
92
96
  } else if (spec.optional === false) {
93
- illegalArgs(`missing arg: --${id}`);
97
+ illegalArgs(`missing arg: --${kebab(id)}`);
94
98
  }
95
99
  } else if (spec.coerce) {
96
100
  __coerceValue(spec, acc, id);
@@ -108,7 +112,7 @@ const __coerceValue = (spec, acc, id) => {
108
112
  }
109
113
  acc[id] = spec.coerce(acc[id]);
110
114
  } catch (e) {
111
- throw new Error(`arg --${id}: ${e.message}`);
115
+ throw new Error(`arg --${kebab(id)}: ${e.message}`);
112
116
  }
113
117
  };
114
118
  export {
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,