@optique/run 0.3.0-dev.44 → 0.3.0-dev.47

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/dist/index.cjs CHANGED
@@ -1,5 +1,9 @@
1
1
  const require_run = require('./run.cjs');
2
2
  const require_valueparser = require('./valueparser.cjs');
3
+ const require_print = require('./print.cjs');
3
4
 
5
+ exports.createPrinter = require_print.createPrinter;
4
6
  exports.path = require_valueparser.path;
7
+ exports.print = require_print.print;
8
+ exports.printError = require_print.printError;
5
9
  exports.run = require_run.run;
package/dist/index.d.cts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { RunOptions, run } from "./run.cjs";
2
2
  import { PathOptions, path } from "./valueparser.cjs";
3
- export { PathOptions, RunOptions, path, run };
3
+ import { PrintErrorOptions, PrintOptions, Printer, PrinterOptions, createPrinter, print, printError } from "./print.cjs";
4
+ export { PathOptions, PrintErrorOptions, PrintOptions, Printer, PrinterOptions, RunOptions, createPrinter, path, print, printError, run };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { RunOptions, run } from "./run.js";
2
2
  import { PathOptions, path } from "./valueparser.js";
3
- export { PathOptions, RunOptions, path, run };
3
+ import { PrintErrorOptions, PrintOptions, Printer, PrinterOptions, createPrinter, print, printError } from "./print.js";
4
+ export { PathOptions, PrintErrorOptions, PrintOptions, Printer, PrinterOptions, RunOptions, createPrinter, path, print, printError, run };
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { run } from "./run.js";
2
2
  import { path } from "./valueparser.js";
3
+ import { createPrinter, print, printError } from "./print.js";
3
4
 
4
- export { path, run };
5
+ export { createPrinter, path, print, printError, run };
package/dist/print.cjs ADDED
@@ -0,0 +1,101 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const node_process = require_rolldown_runtime.__toESM(require("node:process"));
3
+ const __optique_core_message = require_rolldown_runtime.__toESM(require("@optique/core/message"));
4
+
5
+ //#region src/print.ts
6
+ /**
7
+ * Prints a formatted message to stdout with automatic terminal detection.
8
+ *
9
+ * This function automatically detects terminal capabilities (colors, width)
10
+ * and formats the message accordingly. It's ideal for general application
11
+ * output that should be visible to users.
12
+ *
13
+ * @param message The structured message to print.
14
+ * @param options Optional formatting options to override defaults.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { print } from "@optique/run";
19
+ * import { message, optionName } from "@optique/core/message";
20
+ *
21
+ * const configFile = "config.json";
22
+ * const port = 3000;
23
+ *
24
+ * print(message`Configuration loaded from ${configFile}`);
25
+ * print(message`Using ${optionName("--port")} ${port}`);
26
+ * ```
27
+ *
28
+ * @since 0.3.0
29
+ */
30
+ function print(message, options = {}) {
31
+ const printer = createPrinter({
32
+ stream: options.stream ?? "stdout",
33
+ colors: options.colors,
34
+ quotes: options.quotes,
35
+ maxWidth: options.maxWidth
36
+ });
37
+ printer(message);
38
+ }
39
+ function printError(message, options = {}) {
40
+ const stream = options.stream ?? "stderr";
41
+ const output = node_process.default[stream];
42
+ const quotes = options.quotes ?? !output.isTTY;
43
+ const printer = createPrinter({
44
+ stream,
45
+ colors: options.colors,
46
+ quotes,
47
+ maxWidth: options.maxWidth
48
+ });
49
+ const errorMessage = [{
50
+ type: "text",
51
+ text: "Error: "
52
+ }, ...message];
53
+ printer(errorMessage);
54
+ if (options.exitCode != null) node_process.default.exit(options.exitCode);
55
+ }
56
+ /**
57
+ * Creates a custom printer function with predefined formatting options.
58
+ *
59
+ * This is useful when you need consistent formatting across multiple print
60
+ * operations or when you want to override the automatic terminal detection.
61
+ *
62
+ * @param options Formatting options for the printer.
63
+ * @returns A printer function that can be called with messages.
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * import { createPrinter } from "@optique/run";
68
+ * import { message, metavar } from "@optique/core/message";
69
+ *
70
+ * // Create a printer with forced colors and no quotes
71
+ * const printer = createPrinter({
72
+ * colors: true,
73
+ * quotes: false,
74
+ * stream: "stdout",
75
+ * });
76
+ *
77
+ * printer(message`Starting server on ${metavar("PORT")}...`);
78
+ * printer(message`Ready to accept connections`);
79
+ * ```
80
+ *
81
+ * @since 0.3.0
82
+ */
83
+ function createPrinter(options = {}) {
84
+ const stream = options.stream ?? "stdout";
85
+ const output = node_process.default[stream];
86
+ const formatOptions = {
87
+ colors: options.colors ?? output.isTTY,
88
+ quotes: options.quotes,
89
+ maxWidth: options.maxWidth ?? output.columns
90
+ };
91
+ return (message) => {
92
+ const formatted = (0, __optique_core_message.formatMessage)(message, formatOptions);
93
+ if (stream === "stderr") console.error(formatted);
94
+ else console.log(formatted);
95
+ };
96
+ }
97
+
98
+ //#endregion
99
+ exports.createPrinter = createPrinter;
100
+ exports.print = print;
101
+ exports.printError = printError;
@@ -0,0 +1,133 @@
1
+ import { Message, MessageFormatOptions } from "@optique/core/message";
2
+
3
+ //#region src/print.d.ts
4
+
5
+ /**
6
+ * Options for the {@link print} function.
7
+ * @since 0.3.0
8
+ */
9
+ interface PrintOptions extends MessageFormatOptions {
10
+ /**
11
+ * The output stream to write to.
12
+ * @default `"stdout"`
13
+ */
14
+ readonly stream?: "stdout" | "stderr";
15
+ }
16
+ /**
17
+ * Options for the {@link printError} function.
18
+ * @since 0.3.0
19
+ */
20
+ interface PrintErrorOptions extends PrintOptions {
21
+ /**
22
+ * The output stream to write to.
23
+ * @default `"stderr"`
24
+ */
25
+ readonly stream?: "stdout" | "stderr";
26
+ /**
27
+ * Exit code to use when exiting the process.
28
+ * If specified, the process will exit with this code after printing the error.
29
+ */
30
+ readonly exitCode?: number;
31
+ }
32
+ /**
33
+ * Options for creating a custom printer.
34
+ * @since 0.3.0
35
+ */
36
+ interface PrinterOptions extends MessageFormatOptions {
37
+ /**
38
+ * The output stream to write to.
39
+ * @default `"stdout"`
40
+ */
41
+ readonly stream?: "stdout" | "stderr";
42
+ }
43
+ /**
44
+ * A printer function that outputs formatted messages.
45
+ * @param message The structured message to print.
46
+ * @since 0.3.0
47
+ */
48
+ type Printer = (message: Message) => void;
49
+ /**
50
+ * Prints a formatted message to stdout with automatic terminal detection.
51
+ *
52
+ * This function automatically detects terminal capabilities (colors, width)
53
+ * and formats the message accordingly. It's ideal for general application
54
+ * output that should be visible to users.
55
+ *
56
+ * @param message The structured message to print.
57
+ * @param options Optional formatting options to override defaults.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * import { print } from "@optique/run";
62
+ * import { message, optionName } from "@optique/core/message";
63
+ *
64
+ * const configFile = "config.json";
65
+ * const port = 3000;
66
+ *
67
+ * print(message`Configuration loaded from ${configFile}`);
68
+ * print(message`Using ${optionName("--port")} ${port}`);
69
+ * ```
70
+ *
71
+ * @since 0.3.0
72
+ */
73
+ declare function print(message: Message, options?: PrintOptions): void;
74
+ /**
75
+ * Prints a formatted error message to stderr with automatic terminal detection.
76
+ *
77
+ * This function automatically detects terminal capabilities and formats error
78
+ * messages with an "Error: " prefix. Optionally exits the process with a
79
+ * specified exit code.
80
+ *
81
+ * @param message The structured error message to print.
82
+ * @param options Optional formatting options and exit code.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * import { printError } from "@optique/run";
87
+ * import { message, optionName } from "@optique/core/message";
88
+ *
89
+ * const filename = "missing.txt";
90
+ *
91
+ * // Print error and continue
92
+ * printError(message`File ${filename} not found`);
93
+ *
94
+ * // Print error and exit with code 2
95
+ * printError(message`Invalid ${optionName("--config")} value`, { exitCode: 2 });
96
+ * ```
97
+ *
98
+ * @since 0.3.0
99
+ */
100
+ declare function printError(message: Message, options: PrintErrorOptions & {
101
+ exitCode: number;
102
+ }): never;
103
+ declare function printError(message: Message, options?: PrintErrorOptions): void;
104
+ /**
105
+ * Creates a custom printer function with predefined formatting options.
106
+ *
107
+ * This is useful when you need consistent formatting across multiple print
108
+ * operations or when you want to override the automatic terminal detection.
109
+ *
110
+ * @param options Formatting options for the printer.
111
+ * @returns A printer function that can be called with messages.
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * import { createPrinter } from "@optique/run";
116
+ * import { message, metavar } from "@optique/core/message";
117
+ *
118
+ * // Create a printer with forced colors and no quotes
119
+ * const printer = createPrinter({
120
+ * colors: true,
121
+ * quotes: false,
122
+ * stream: "stdout",
123
+ * });
124
+ *
125
+ * printer(message`Starting server on ${metavar("PORT")}...`);
126
+ * printer(message`Ready to accept connections`);
127
+ * ```
128
+ *
129
+ * @since 0.3.0
130
+ */
131
+ declare function createPrinter(options?: PrinterOptions): Printer;
132
+ //#endregion
133
+ export { PrintErrorOptions, PrintOptions, Printer, PrinterOptions, createPrinter, print, printError };
@@ -0,0 +1,133 @@
1
+ import { Message, MessageFormatOptions } from "@optique/core/message";
2
+
3
+ //#region src/print.d.ts
4
+
5
+ /**
6
+ * Options for the {@link print} function.
7
+ * @since 0.3.0
8
+ */
9
+ interface PrintOptions extends MessageFormatOptions {
10
+ /**
11
+ * The output stream to write to.
12
+ * @default `"stdout"`
13
+ */
14
+ readonly stream?: "stdout" | "stderr";
15
+ }
16
+ /**
17
+ * Options for the {@link printError} function.
18
+ * @since 0.3.0
19
+ */
20
+ interface PrintErrorOptions extends PrintOptions {
21
+ /**
22
+ * The output stream to write to.
23
+ * @default `"stderr"`
24
+ */
25
+ readonly stream?: "stdout" | "stderr";
26
+ /**
27
+ * Exit code to use when exiting the process.
28
+ * If specified, the process will exit with this code after printing the error.
29
+ */
30
+ readonly exitCode?: number;
31
+ }
32
+ /**
33
+ * Options for creating a custom printer.
34
+ * @since 0.3.0
35
+ */
36
+ interface PrinterOptions extends MessageFormatOptions {
37
+ /**
38
+ * The output stream to write to.
39
+ * @default `"stdout"`
40
+ */
41
+ readonly stream?: "stdout" | "stderr";
42
+ }
43
+ /**
44
+ * A printer function that outputs formatted messages.
45
+ * @param message The structured message to print.
46
+ * @since 0.3.0
47
+ */
48
+ type Printer = (message: Message) => void;
49
+ /**
50
+ * Prints a formatted message to stdout with automatic terminal detection.
51
+ *
52
+ * This function automatically detects terminal capabilities (colors, width)
53
+ * and formats the message accordingly. It's ideal for general application
54
+ * output that should be visible to users.
55
+ *
56
+ * @param message The structured message to print.
57
+ * @param options Optional formatting options to override defaults.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * import { print } from "@optique/run";
62
+ * import { message, optionName } from "@optique/core/message";
63
+ *
64
+ * const configFile = "config.json";
65
+ * const port = 3000;
66
+ *
67
+ * print(message`Configuration loaded from ${configFile}`);
68
+ * print(message`Using ${optionName("--port")} ${port}`);
69
+ * ```
70
+ *
71
+ * @since 0.3.0
72
+ */
73
+ declare function print(message: Message, options?: PrintOptions): void;
74
+ /**
75
+ * Prints a formatted error message to stderr with automatic terminal detection.
76
+ *
77
+ * This function automatically detects terminal capabilities and formats error
78
+ * messages with an "Error: " prefix. Optionally exits the process with a
79
+ * specified exit code.
80
+ *
81
+ * @param message The structured error message to print.
82
+ * @param options Optional formatting options and exit code.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * import { printError } from "@optique/run";
87
+ * import { message, optionName } from "@optique/core/message";
88
+ *
89
+ * const filename = "missing.txt";
90
+ *
91
+ * // Print error and continue
92
+ * printError(message`File ${filename} not found`);
93
+ *
94
+ * // Print error and exit with code 2
95
+ * printError(message`Invalid ${optionName("--config")} value`, { exitCode: 2 });
96
+ * ```
97
+ *
98
+ * @since 0.3.0
99
+ */
100
+ declare function printError(message: Message, options: PrintErrorOptions & {
101
+ exitCode: number;
102
+ }): never;
103
+ declare function printError(message: Message, options?: PrintErrorOptions): void;
104
+ /**
105
+ * Creates a custom printer function with predefined formatting options.
106
+ *
107
+ * This is useful when you need consistent formatting across multiple print
108
+ * operations or when you want to override the automatic terminal detection.
109
+ *
110
+ * @param options Formatting options for the printer.
111
+ * @returns A printer function that can be called with messages.
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * import { createPrinter } from "@optique/run";
116
+ * import { message, metavar } from "@optique/core/message";
117
+ *
118
+ * // Create a printer with forced colors and no quotes
119
+ * const printer = createPrinter({
120
+ * colors: true,
121
+ * quotes: false,
122
+ * stream: "stdout",
123
+ * });
124
+ *
125
+ * printer(message`Starting server on ${metavar("PORT")}...`);
126
+ * printer(message`Ready to accept connections`);
127
+ * ```
128
+ *
129
+ * @since 0.3.0
130
+ */
131
+ declare function createPrinter(options?: PrinterOptions): Printer;
132
+ //#endregion
133
+ export { PrintErrorOptions, PrintOptions, Printer, PrinterOptions, createPrinter, print, printError };
package/dist/print.js ADDED
@@ -0,0 +1,98 @@
1
+ import process from "node:process";
2
+ import { formatMessage } from "@optique/core/message";
3
+
4
+ //#region src/print.ts
5
+ /**
6
+ * Prints a formatted message to stdout with automatic terminal detection.
7
+ *
8
+ * This function automatically detects terminal capabilities (colors, width)
9
+ * and formats the message accordingly. It's ideal for general application
10
+ * output that should be visible to users.
11
+ *
12
+ * @param message The structured message to print.
13
+ * @param options Optional formatting options to override defaults.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { print } from "@optique/run";
18
+ * import { message, optionName } from "@optique/core/message";
19
+ *
20
+ * const configFile = "config.json";
21
+ * const port = 3000;
22
+ *
23
+ * print(message`Configuration loaded from ${configFile}`);
24
+ * print(message`Using ${optionName("--port")} ${port}`);
25
+ * ```
26
+ *
27
+ * @since 0.3.0
28
+ */
29
+ function print(message$1, options = {}) {
30
+ const printer = createPrinter({
31
+ stream: options.stream ?? "stdout",
32
+ colors: options.colors,
33
+ quotes: options.quotes,
34
+ maxWidth: options.maxWidth
35
+ });
36
+ printer(message$1);
37
+ }
38
+ function printError(message$1, options = {}) {
39
+ const stream = options.stream ?? "stderr";
40
+ const output = process[stream];
41
+ const quotes = options.quotes ?? !output.isTTY;
42
+ const printer = createPrinter({
43
+ stream,
44
+ colors: options.colors,
45
+ quotes,
46
+ maxWidth: options.maxWidth
47
+ });
48
+ const errorMessage = [{
49
+ type: "text",
50
+ text: "Error: "
51
+ }, ...message$1];
52
+ printer(errorMessage);
53
+ if (options.exitCode != null) process.exit(options.exitCode);
54
+ }
55
+ /**
56
+ * Creates a custom printer function with predefined formatting options.
57
+ *
58
+ * This is useful when you need consistent formatting across multiple print
59
+ * operations or when you want to override the automatic terminal detection.
60
+ *
61
+ * @param options Formatting options for the printer.
62
+ * @returns A printer function that can be called with messages.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * import { createPrinter } from "@optique/run";
67
+ * import { message, metavar } from "@optique/core/message";
68
+ *
69
+ * // Create a printer with forced colors and no quotes
70
+ * const printer = createPrinter({
71
+ * colors: true,
72
+ * quotes: false,
73
+ * stream: "stdout",
74
+ * });
75
+ *
76
+ * printer(message`Starting server on ${metavar("PORT")}...`);
77
+ * printer(message`Ready to accept connections`);
78
+ * ```
79
+ *
80
+ * @since 0.3.0
81
+ */
82
+ function createPrinter(options = {}) {
83
+ const stream = options.stream ?? "stdout";
84
+ const output = process[stream];
85
+ const formatOptions = {
86
+ colors: options.colors ?? output.isTTY,
87
+ quotes: options.quotes,
88
+ maxWidth: options.maxWidth ?? output.columns
89
+ };
90
+ return (message$1) => {
91
+ const formatted = formatMessage(message$1, formatOptions);
92
+ if (stream === "stderr") console.error(formatted);
93
+ else console.log(formatted);
94
+ };
95
+ }
96
+
97
+ //#endregion
98
+ export { createPrinter, print, printError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/run",
3
- "version": "0.3.0-dev.44+d098ce5a",
3
+ "version": "0.3.0-dev.47+8e847e26",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",