@nan0web/ui-cli 1.0.1 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nan0web/ui-cli",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "NaN•Web UI CLI. Command line interface for One application logic (algorithm) and many UI.",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -40,9 +40,9 @@
40
40
  "@nan0web/i18n": "^1.0.1",
41
41
  "@nan0web/log": "1.0.1",
42
42
  "@nan0web/test": "1.1.0",
43
- "@nan0web/ui": "1.0.2"
43
+ "@nan0web/ui": "1.0.3"
44
44
  },
45
45
  "dependencies": {
46
- "@nan0web/co": "^1.1.2"
46
+ "@nan0web/co": "^1.1.3"
47
47
  }
48
48
  }
@@ -2,10 +2,14 @@ import { Message } from "@nan0web/co"
2
2
  import Logger from "@nan0web/log"
3
3
 
4
4
  /**
5
- * @typedef {Object} CommandHelpField
6
- * @property {string} [help] - Human readable description.
7
- * @property {string} [placeholder] - Placeholder for usage (e.g. "<user>").
8
- * @property {string} [alias] - Short alias (single‑letter).
5
+ * @typedef {Object} CommandHelpField MessageBodySchema
6
+ * @property {string} [help] - Human readable description.
7
+ * @property {string} [placeholder] - Placeholder for usage (e.g. "<user>").
8
+ * @property {string} [alias] - Short alias (single‑letter).
9
+ * @property {any} [defaultValue] - Default value.
10
+ * @property {any} [type] - Data type.
11
+ * @property {boolean} [required] - Is field required or not.
12
+ * @property {RegExp} [pattern] - Regular expression pattern for validation.
9
13
  */
10
14
 
11
15
  /**
@@ -96,10 +100,12 @@ export default class CommandHelp {
96
100
  const flagParts = []
97
101
 
98
102
  bodyProps.forEach(prop => {
103
+ /** @type {CommandHelpField} */
99
104
  const schema = this.BodyClass[prop] || {}
100
105
  const alias = schema.alias ? `-${schema.alias}, ` : ""
101
- if (schema.placeholder) {
102
- placeholderParts.push(`[${alias}--${prop}=${schema.placeholder}]`)
106
+ const placeholder = schema.placeholder || schema.defaultValue
107
+ if (placeholder) {
108
+ placeholderParts.push(`[${alias}--${prop}=${placeholder}]`)
103
109
  } else {
104
110
  flagParts.push(`[${alias}--${prop}]`)
105
111
  }
@@ -140,6 +146,7 @@ export default class CommandHelp {
140
146
 
141
147
  lines.push("Options:")
142
148
  bodyProps.forEach(prop => {
149
+ /** @type {CommandHelpField} */
143
150
  const schema = this.BodyClass[prop] || {}
144
151
  if (typeof schema !== "object") return
145
152
 
@@ -147,16 +154,36 @@ export default class CommandHelp {
147
154
  ? `--${prop}, -${schema.alias}`
148
155
  : `--${prop}`
149
156
 
150
- const type = schema.placeholder !== undefined ? "string" : "boolean"
151
- const required = schema.default === undefined ? " *" : ""
157
+ const type = undefined !== schema.type ? String(schema.type)
158
+ : undefined !== schema.defaultValue ? typeof schema.defaultValue
159
+ : undefined !== schema.placeholder ? typeof schema.placeholder
160
+ : "any"
161
+ const required = schema.required || schema.pattern || schema.defaultValue === undefined ? " *" : " "
152
162
  const description = schema.help || "No description"
153
163
 
154
164
  // Pad flags to align the type column with the expectations.
155
- lines.push(` ${flags.padEnd(30)} ${type.padEnd(8)}${required} ${description}`)
165
+ lines.push(` ${flags.padEnd(30)} ${type.padEnd(9)}${required} ${description}`)
156
166
  })
157
167
  lines.push("")
158
168
  }
159
169
 
170
+ /**
171
+ * @param {object} body
172
+ * @returns {Map<string, any>} A map of errors, empty map if no errors.
173
+ */
174
+ validate(body) {
175
+ const Class = /** @type {typeof this.BodyClass} */ (body.constructor)
176
+ const result = new Map()
177
+ for (const [name, schema] of Object.entries(Class)) {
178
+ const fn = schema?.validate
179
+ if ("function" !== typeof fn) continue
180
+ const ok = fn.apply(body, [body[name]])
181
+ if (true === ok) continue
182
+ result.set(name, ok)
183
+ }
184
+ return result
185
+ }
186
+
160
187
  /**
161
188
  * Renders Subcommands, if any.
162
189
  *
package/src/index.js CHANGED
@@ -17,6 +17,8 @@ export {
17
17
  Input,
18
18
  } from "./ui/index.js"
19
19
 
20
+ /** @typedef {import("./CommandHelp.js").CommandHelpField} CommandHelpField */
21
+
20
22
  export {
21
23
  CLI,
22
24
  CLIInputAdapter,
@@ -1,8 +1,12 @@
1
1
  /**
2
- * @typedef {Object} CommandHelpField
3
- * @property {string} [help] - Human readable description.
4
- * @property {string} [placeholder] - Placeholder for usage (e.g. "<user>").
5
- * @property {string} [alias] - Short alias (single‑letter).
2
+ * @typedef {Object} CommandHelpField MessageBodySchema
3
+ * @property {string} [help] - Human readable description.
4
+ * @property {string} [placeholder] - Placeholder for usage (e.g. "<user>").
5
+ * @property {string} [alias] - Short alias (single‑letter).
6
+ * @property {any} [defaultValue] - Default value.
7
+ * @property {any} [type] - Data type.
8
+ * @property {boolean} [required] - Is field required or not.
9
+ * @property {RegExp} [pattern] - Regular expression pattern for validation.
6
10
  */
7
11
  /**
8
12
  * CommandHelp – generates CLI help from a Message body schema.
@@ -37,8 +41,16 @@ export default class CommandHelp {
37
41
  * Prints the generated help to the logger.
38
42
  */
39
43
  print(): void;
44
+ /**
45
+ * @param {object} body
46
+ * @returns {Map<string, any>} A map of errors, empty map if no errors.
47
+ */
48
+ validate(body: object): Map<string, any>;
40
49
  #private;
41
50
  }
51
+ /**
52
+ * MessageBodySchema
53
+ */
42
54
  export type CommandHelpField = {
43
55
  /**
44
56
  * - Human readable description.
@@ -52,6 +64,22 @@ export type CommandHelpField = {
52
64
  * - Short alias (single‑letter).
53
65
  */
54
66
  alias?: string | undefined;
67
+ /**
68
+ * - Default value.
69
+ */
70
+ defaultValue?: any;
71
+ /**
72
+ * - Data type.
73
+ */
74
+ type?: any;
75
+ /**
76
+ * - Is field required or not.
77
+ */
78
+ required?: boolean | undefined;
79
+ /**
80
+ * - Regular expression pattern for validation.
81
+ */
82
+ pattern?: RegExp | undefined;
55
83
  };
56
84
  import { Message } from "@nan0web/co";
57
85
  import Logger from "@nan0web/log";
package/types/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { str2argv } from "./utils/parse.js";
2
2
  export const renderers: Map<string, (data: any) => string>;
3
3
  export default CLIInputAdapter;
4
+ export type CommandHelpField = import("./CommandHelp.js").CommandHelpField;
4
5
  import CLI from "./CLI.js";
5
6
  import CLIInputAdapter from "./InputAdapter.js";
6
7
  import { CancelError } from "@nan0web/ui/core";