@optique/run 0.1.0-dev.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/README.md ADDED
@@ -0,0 +1,134 @@
1
+ @optique/run
2
+ ============
3
+
4
+ > [!CAUTION]
5
+ > Optique is currently in early development for proof of concept purposes,
6
+ > and is not yet ready for production use. The API is subject to change,
7
+ > and there may be bugs or missing features.
8
+
9
+ *Process-integrated CLI parser for Node.js, Bun, and Deno.*
10
+
11
+ *@optique/run* provides a convenient high-level interface for *@optique/core*
12
+ that automatically handles `process.argv`, `process.exit()`, and terminal
13
+ capabilities. It eliminates the manual setup required by the core library,
14
+ making it perfect for building CLI applications in server-side JavaScript
15
+ runtimes.
16
+
17
+
18
+ When to use @optique/run
19
+ ------------------------
20
+
21
+ Use `@optique/run` when:
22
+
23
+ - Building CLI applications for Node.js, Bun, or Deno
24
+ - You want automatic `process.argv` parsing and `process.exit()` handling
25
+ - You need automatic terminal capability detection (colors, width)
26
+ - You prefer a simple, batteries-included approach
27
+
28
+ Use `@optique/core` instead when:
29
+
30
+ - Building web applications or libraries
31
+ - You need full control over argument sources and error handling
32
+ - Working in environments without `process` (browsers, web workers)
33
+ - Building reusable parser components
34
+
35
+
36
+ Installation
37
+ ------------
38
+
39
+ ~~~~ bash
40
+ deno add jsr:@optique/run jsr:@optique/core
41
+ npm add @optique/run @optique/core
42
+ pnpm add @optique/run @optique/core
43
+ yarn add @optique/run @optique/core
44
+ ~~~~
45
+
46
+
47
+ Quick example
48
+ -------------
49
+
50
+ ~~~~ typescript
51
+ import { run } from "@optique/run";
52
+ import { object, option, argument } from "@optique/core/parser";
53
+ import { string, integer } from "@optique/core/valueparser";
54
+
55
+ const parser = object({
56
+ name: argument(string()),
57
+ age: option("-a", "--age", integer()),
58
+ verbose: option("-v", "--verbose"),
59
+ });
60
+
61
+ // Automatically uses process.argv, handles errors and help, exits on completion
62
+ const config = run(parser);
63
+
64
+ console.log(`Hello ${config.name}!`);
65
+ if (config.age) {
66
+ console.log(`You are ${config.age} years old.`);
67
+ }
68
+ if (config.verbose) {
69
+ console.log("Verbose mode enabled.");
70
+ }
71
+ ~~~~
72
+
73
+ Run it:
74
+
75
+ ~~~~ bash
76
+ $ node cli.js Alice --age 25 --verbose
77
+ Hello Alice!
78
+ You are 25 years old.
79
+ Verbose mode enabled.
80
+ ~~~~
81
+
82
+
83
+ API differences from @optique/core
84
+ ----------------------------------
85
+
86
+ | Feature | @optique/core | @optique/run |
87
+ |--------------------|-----------------------------------------|----------------------------------|
88
+ | Argument source | Manual (`run(parser, "program", args)`) | Automatic (`process.argv`) |
89
+ | Error handling | Return value or callback | Automatic `process.exit()` |
90
+ | Help display | Manual implementation | Automatic help option/command |
91
+ | Terminal detection | Manual specification | Automatic TTY/width detection |
92
+ | Program name | Manual parameter | Automatic from `process.argv[1]` |
93
+
94
+
95
+ Configuration options
96
+ ---------------------
97
+
98
+ ~~~~ typescript
99
+ const result = run(parser, {
100
+ programName: "my-cli", // Override detected program name
101
+ args: ["custom", "args"], // Override process.argv
102
+ colors: true, // Force colored output
103
+ maxWidth: 100, // Set output width
104
+ help: "both", // Enable --help and help command
105
+ aboveError: "usage", // Show usage on errors
106
+ errorExitCode: 1, // Exit code for errors
107
+ });
108
+ ~~~~
109
+
110
+
111
+ Help system
112
+ -----------
113
+
114
+ Enable built-in help functionality:
115
+
116
+ ~~~~ typescript
117
+ const result = run(parser, {
118
+ help: "option", // Adds --help option
119
+ help: "command", // Adds help subcommand
120
+ help: "both", // Adds both --help and help command
121
+ help: "none", // No help (default)
122
+ });
123
+ ~~~~
124
+
125
+
126
+ Error handling
127
+ --------------
128
+
129
+ The `run()` function automatically:
130
+
131
+ - Prints usage information and error messages to stderr
132
+ - Exits with code `0` for help requests
133
+ - Exits with code `1` (or custom) for parse errors
134
+ - Never returns on errors (always calls `process.exit()`)
@@ -0,0 +1,25 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+
25
+ exports.__toESM = __toESM;
package/dist/index.cjs ADDED
@@ -0,0 +1,71 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const __optique_core_facade = require_rolldown_runtime.__toESM(require("@optique/core/facade"));
3
+ const node_path = require_rolldown_runtime.__toESM(require("node:path"));
4
+ const node_process = require_rolldown_runtime.__toESM(require("node:process"));
5
+
6
+ //#region src/index.ts
7
+ /**
8
+ * Runs a command-line parser with automatic process integration.
9
+ *
10
+ * This function provides a convenient high-level interface for parsing
11
+ * command-line arguments in Node.js, Bun, and Deno environments. It
12
+ * automatically handles `process.argv`, `process.exit()`, and terminal
13
+ * output formatting, eliminating the need to manually pass these values
14
+ * as the lower-level `run()` function (from `@optique/core/facade`) requires.
15
+ *
16
+ * The function will automatically:
17
+ *
18
+ * - Extract arguments from `process.argv`
19
+ * - Detect terminal capabilities for colors and width
20
+ * - Exit the process with appropriate codes on help or error
21
+ * - Format output according to terminal capabilities
22
+ *
23
+ * @template T The parser type being executed.
24
+ * @param parser The command-line parser to execute.
25
+ * @param options Configuration options for customizing behavior.
26
+ * See {@link RunOptions} for available settings.
27
+ * @returns The parsed result if successful. On help display or parse errors,
28
+ * the function will call `process.exit()` and not return.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * import { object, option, run } from "@optique/run";
33
+ * import { string, integer } from "@optique/core/valueparser";
34
+ *
35
+ * const parser = object({
36
+ * name: option("-n", "--name", string()),
37
+ * port: option("-p", "--port", integer()),
38
+ * });
39
+ *
40
+ * // Automatically uses process.argv, handles errors/help, exits on completion
41
+ * const config = run(parser);
42
+ * console.log(`Starting server ${config.name} on port ${config.port}`);
43
+ * ```
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // With custom options
48
+ * const result = run(parser, {
49
+ * programName: "myapp",
50
+ * help: "both", // Enable both --help option and help command
51
+ * colors: true, // Force colored output
52
+ * errorExitCode: 2, // Exit with code 2 on errors
53
+ * });
54
+ * ```
55
+ */
56
+ function run(parser, options = {}) {
57
+ const { programName = node_path.default.basename(node_process.default.argv[1] || "cli"), args = node_process.default.argv.slice(2), colors = node_process.default.stdout.isTTY, maxWidth = node_process.default.stdout.columns, help = "none", aboveError = "usage", errorExitCode = 1 } = options;
58
+ return (0, __optique_core_facade.run)(parser, programName, args, {
59
+ colors,
60
+ maxWidth,
61
+ help,
62
+ aboveError,
63
+ onHelp: node_process.default.exit,
64
+ onError() {
65
+ return node_process.default.exit(errorExitCode);
66
+ }
67
+ });
68
+ }
69
+
70
+ //#endregion
71
+ exports.run = run;
@@ -0,0 +1,113 @@
1
+ import { InferValue, Parser } from "@optique/core/parser";
2
+
3
+ //#region src/index.d.ts
4
+
5
+ /**
6
+ * Configuration options for the {@link run} function.
7
+ */
8
+ interface RunOptions {
9
+ /**
10
+ * The name of the program to display in usage and help messages.
11
+ *
12
+ * @default The basename of `process.argv[1]` (the script filename)
13
+ */
14
+ readonly programName?: string;
15
+ /**
16
+ * The command-line arguments to parse.
17
+ *
18
+ * @default `process.argv.slice(2)` (arguments after the script name)
19
+ */
20
+ readonly args?: readonly string[];
21
+ /**
22
+ * Whether to enable colored output in help and error messages.
23
+ *
24
+ * @default `process.stdout.isTTY` (auto-detect based on terminal)
25
+ */
26
+ readonly colors?: boolean;
27
+ /**
28
+ * Maximum width for output formatting. Text will be wrapped to fit within
29
+ * this width. If not specified, uses the terminal width.
30
+ *
31
+ * @default `process.stdout.columns` (auto-detect terminal width)
32
+ */
33
+ readonly maxWidth?: number;
34
+ /**
35
+ * Determines how help functionality is made available:
36
+ *
37
+ * - `"command"`: Only the `help` subcommand is available
38
+ * - `"option"`: Only the `--help` option is available
39
+ * - `"both"`: Both `help` subcommand and `--help` option are available
40
+ * - `"none"`: No help functionality is provided
41
+ *
42
+ * @default `"none"`
43
+ */
44
+ readonly help?: "command" | "option" | "both" | "none";
45
+ /**
46
+ * What to display above error messages:
47
+ *
48
+ * - `"usage"`: Show usage line before error message
49
+ * - `"help"`: Show full help page before error message
50
+ * - `"none"`: Show only the error message
51
+ *
52
+ * @default `"usage"`
53
+ */
54
+ readonly aboveError?: "usage" | "help" | "none";
55
+ /**
56
+ * The exit code to use when the parser encounters an error.
57
+ *
58
+ * @default `1`
59
+ */
60
+ readonly errorExitCode?: number;
61
+ }
62
+ /**
63
+ * Runs a command-line parser with automatic process integration.
64
+ *
65
+ * This function provides a convenient high-level interface for parsing
66
+ * command-line arguments in Node.js, Bun, and Deno environments. It
67
+ * automatically handles `process.argv`, `process.exit()`, and terminal
68
+ * output formatting, eliminating the need to manually pass these values
69
+ * as the lower-level `run()` function (from `@optique/core/facade`) requires.
70
+ *
71
+ * The function will automatically:
72
+ *
73
+ * - Extract arguments from `process.argv`
74
+ * - Detect terminal capabilities for colors and width
75
+ * - Exit the process with appropriate codes on help or error
76
+ * - Format output according to terminal capabilities
77
+ *
78
+ * @template T The parser type being executed.
79
+ * @param parser The command-line parser to execute.
80
+ * @param options Configuration options for customizing behavior.
81
+ * See {@link RunOptions} for available settings.
82
+ * @returns The parsed result if successful. On help display or parse errors,
83
+ * the function will call `process.exit()` and not return.
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * import { object, option, run } from "@optique/run";
88
+ * import { string, integer } from "@optique/core/valueparser";
89
+ *
90
+ * const parser = object({
91
+ * name: option("-n", "--name", string()),
92
+ * port: option("-p", "--port", integer()),
93
+ * });
94
+ *
95
+ * // Automatically uses process.argv, handles errors/help, exits on completion
96
+ * const config = run(parser);
97
+ * console.log(`Starting server ${config.name} on port ${config.port}`);
98
+ * ```
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * // With custom options
103
+ * const result = run(parser, {
104
+ * programName: "myapp",
105
+ * help: "both", // Enable both --help option and help command
106
+ * colors: true, // Force colored output
107
+ * errorExitCode: 2, // Exit with code 2 on errors
108
+ * });
109
+ * ```
110
+ */
111
+ declare function run<T extends Parser<unknown, unknown>>(parser: T, options?: RunOptions): InferValue<T>;
112
+ //#endregion
113
+ export { RunOptions, run };
@@ -0,0 +1,113 @@
1
+ import { InferValue, Parser } from "@optique/core/parser";
2
+
3
+ //#region src/index.d.ts
4
+
5
+ /**
6
+ * Configuration options for the {@link run} function.
7
+ */
8
+ interface RunOptions {
9
+ /**
10
+ * The name of the program to display in usage and help messages.
11
+ *
12
+ * @default The basename of `process.argv[1]` (the script filename)
13
+ */
14
+ readonly programName?: string;
15
+ /**
16
+ * The command-line arguments to parse.
17
+ *
18
+ * @default `process.argv.slice(2)` (arguments after the script name)
19
+ */
20
+ readonly args?: readonly string[];
21
+ /**
22
+ * Whether to enable colored output in help and error messages.
23
+ *
24
+ * @default `process.stdout.isTTY` (auto-detect based on terminal)
25
+ */
26
+ readonly colors?: boolean;
27
+ /**
28
+ * Maximum width for output formatting. Text will be wrapped to fit within
29
+ * this width. If not specified, uses the terminal width.
30
+ *
31
+ * @default `process.stdout.columns` (auto-detect terminal width)
32
+ */
33
+ readonly maxWidth?: number;
34
+ /**
35
+ * Determines how help functionality is made available:
36
+ *
37
+ * - `"command"`: Only the `help` subcommand is available
38
+ * - `"option"`: Only the `--help` option is available
39
+ * - `"both"`: Both `help` subcommand and `--help` option are available
40
+ * - `"none"`: No help functionality is provided
41
+ *
42
+ * @default `"none"`
43
+ */
44
+ readonly help?: "command" | "option" | "both" | "none";
45
+ /**
46
+ * What to display above error messages:
47
+ *
48
+ * - `"usage"`: Show usage line before error message
49
+ * - `"help"`: Show full help page before error message
50
+ * - `"none"`: Show only the error message
51
+ *
52
+ * @default `"usage"`
53
+ */
54
+ readonly aboveError?: "usage" | "help" | "none";
55
+ /**
56
+ * The exit code to use when the parser encounters an error.
57
+ *
58
+ * @default `1`
59
+ */
60
+ readonly errorExitCode?: number;
61
+ }
62
+ /**
63
+ * Runs a command-line parser with automatic process integration.
64
+ *
65
+ * This function provides a convenient high-level interface for parsing
66
+ * command-line arguments in Node.js, Bun, and Deno environments. It
67
+ * automatically handles `process.argv`, `process.exit()`, and terminal
68
+ * output formatting, eliminating the need to manually pass these values
69
+ * as the lower-level `run()` function (from `@optique/core/facade`) requires.
70
+ *
71
+ * The function will automatically:
72
+ *
73
+ * - Extract arguments from `process.argv`
74
+ * - Detect terminal capabilities for colors and width
75
+ * - Exit the process with appropriate codes on help or error
76
+ * - Format output according to terminal capabilities
77
+ *
78
+ * @template T The parser type being executed.
79
+ * @param parser The command-line parser to execute.
80
+ * @param options Configuration options for customizing behavior.
81
+ * See {@link RunOptions} for available settings.
82
+ * @returns The parsed result if successful. On help display or parse errors,
83
+ * the function will call `process.exit()` and not return.
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * import { object, option, run } from "@optique/run";
88
+ * import { string, integer } from "@optique/core/valueparser";
89
+ *
90
+ * const parser = object({
91
+ * name: option("-n", "--name", string()),
92
+ * port: option("-p", "--port", integer()),
93
+ * });
94
+ *
95
+ * // Automatically uses process.argv, handles errors/help, exits on completion
96
+ * const config = run(parser);
97
+ * console.log(`Starting server ${config.name} on port ${config.port}`);
98
+ * ```
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * // With custom options
103
+ * const result = run(parser, {
104
+ * programName: "myapp",
105
+ * help: "both", // Enable both --help option and help command
106
+ * colors: true, // Force colored output
107
+ * errorExitCode: 2, // Exit with code 2 on errors
108
+ * });
109
+ * ```
110
+ */
111
+ declare function run<T extends Parser<unknown, unknown>>(parser: T, options?: RunOptions): InferValue<T>;
112
+ //#endregion
113
+ export { RunOptions, run };
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ import { run as run$1 } from "@optique/core/facade";
2
+ import path from "node:path";
3
+ import process from "node:process";
4
+
5
+ //#region src/index.ts
6
+ /**
7
+ * Runs a command-line parser with automatic process integration.
8
+ *
9
+ * This function provides a convenient high-level interface for parsing
10
+ * command-line arguments in Node.js, Bun, and Deno environments. It
11
+ * automatically handles `process.argv`, `process.exit()`, and terminal
12
+ * output formatting, eliminating the need to manually pass these values
13
+ * as the lower-level `run()` function (from `@optique/core/facade`) requires.
14
+ *
15
+ * The function will automatically:
16
+ *
17
+ * - Extract arguments from `process.argv`
18
+ * - Detect terminal capabilities for colors and width
19
+ * - Exit the process with appropriate codes on help or error
20
+ * - Format output according to terminal capabilities
21
+ *
22
+ * @template T The parser type being executed.
23
+ * @param parser The command-line parser to execute.
24
+ * @param options Configuration options for customizing behavior.
25
+ * See {@link RunOptions} for available settings.
26
+ * @returns The parsed result if successful. On help display or parse errors,
27
+ * the function will call `process.exit()` and not return.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * import { object, option, run } from "@optique/run";
32
+ * import { string, integer } from "@optique/core/valueparser";
33
+ *
34
+ * const parser = object({
35
+ * name: option("-n", "--name", string()),
36
+ * port: option("-p", "--port", integer()),
37
+ * });
38
+ *
39
+ * // Automatically uses process.argv, handles errors/help, exits on completion
40
+ * const config = run(parser);
41
+ * console.log(`Starting server ${config.name} on port ${config.port}`);
42
+ * ```
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * // With custom options
47
+ * const result = run(parser, {
48
+ * programName: "myapp",
49
+ * help: "both", // Enable both --help option and help command
50
+ * colors: true, // Force colored output
51
+ * errorExitCode: 2, // Exit with code 2 on errors
52
+ * });
53
+ * ```
54
+ */
55
+ function run(parser, options = {}) {
56
+ const { programName = path.basename(process.argv[1] || "cli"), args = process.argv.slice(2), colors = process.stdout.isTTY, maxWidth = process.stdout.columns, help = "none", aboveError = "usage", errorExitCode = 1 } = options;
57
+ return run$1(parser, programName, args, {
58
+ colors,
59
+ maxWidth,
60
+ help,
61
+ aboveError,
62
+ onHelp: process.exit,
63
+ onError() {
64
+ return process.exit(errorExitCode);
65
+ }
66
+ });
67
+ }
68
+
69
+ //#endregion
70
+ export { run };
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@optique/run",
3
+ "version": "0.1.0-dev.1+4d43eddd",
4
+ "description": "Type-safe combinatorial command-line interface parser",
5
+ "keywords": [
6
+ "CLI",
7
+ "command-line",
8
+ "commandline",
9
+ "parser",
10
+ "getopt",
11
+ "optparse"
12
+ ],
13
+ "license": "MIT",
14
+ "author": {
15
+ "name": "Hong Minhee",
16
+ "email": "hong@minhee.org",
17
+ "url": "https://hongminhee.org/"
18
+ },
19
+ "homepage": "https://optique.dev/",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/dahlia/optique.git",
23
+ "directory": "packages/run/"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/dahlia/optique/issues"
27
+ },
28
+ "funding": [
29
+ "https://github.com/sponsors/dahlia"
30
+ ],
31
+ "engines": {
32
+ "node": ">=20.0.0",
33
+ "bun": ">=1.2.0",
34
+ "deno": ">=2.3.0"
35
+ },
36
+ "files": [
37
+ "dist/",
38
+ "package.json",
39
+ "README.md"
40
+ ],
41
+ "type": "module",
42
+ "module": "./dist/index.js",
43
+ "main": "./dist/index.cjs",
44
+ "types": "./dist/index.d.ts",
45
+ "exports": {
46
+ ".": {
47
+ "types": {
48
+ "import": "./dist/index.d.ts",
49
+ "require": "./dist/index.d.cts"
50
+ },
51
+ "import": "./dist/index.js",
52
+ "require": "./dist/index.cjs"
53
+ }
54
+ },
55
+ "sideEffects": false,
56
+ "dependencies": {
57
+ "@optique/core": ""
58
+ },
59
+ "devDependencies": {
60
+ "@types/node": "^20.19.9",
61
+ "tsdown": "^0.13.0",
62
+ "typescript": "^5.8.3"
63
+ },
64
+ "scripts": {
65
+ "build": "tsdown",
66
+ "prepublish": "tsdown",
67
+ "test": "tsdown && node --experimental-transform-types --test",
68
+ "test:bun": "tsdown && bun test",
69
+ "test:deno": "deno test",
70
+ "test-all": "tsdown && node --experimental-transform-types --test && bun test && deno test"
71
+ }
72
+ }