@optique/run 0.1.0-dev.2 → 0.1.0-dev.27
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 +11 -70
- package/dist/index.cjs +4 -70
- package/dist/index.d.cts +3 -113
- package/dist/index.d.ts +3 -113
- package/dist/index.js +3 -69
- package/dist/run.cjs +71 -0
- package/dist/run.d.cts +113 -0
- package/dist/run.d.ts +113 -0
- package/dist/run.js +70 -0
- package/dist/valueparser.cjs +88 -0
- package/dist/valueparser.d.cts +74 -0
- package/dist/valueparser.d.ts +74 -0
- package/dist/valueparser.js +87 -0
- package/package.json +17 -1
package/README.md
CHANGED
|
@@ -2,30 +2,25 @@
|
|
|
2
2
|
============
|
|
3
3
|
|
|
4
4
|
> [!CAUTION]
|
|
5
|
-
> Optique is currently in early development
|
|
6
|
-
>
|
|
7
|
-
> and there may be bugs or missing features.
|
|
5
|
+
> Optique is currently in early development and may change significantly.
|
|
6
|
+
> Expect breaking changes as we refine the API and features.
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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.
|
|
8
|
+
Process-integrated CLI parser for Node.js, Bun, and Deno that provides a
|
|
9
|
+
batteries-included interface for *@optique/core* with automatic `process.argv`
|
|
10
|
+
handling, `process.exit()`, and terminal capabilities.
|
|
16
11
|
|
|
17
12
|
|
|
18
13
|
When to use @optique/run
|
|
19
14
|
------------------------
|
|
20
15
|
|
|
21
|
-
Use
|
|
16
|
+
Use *@optique/run* when:
|
|
22
17
|
|
|
23
18
|
- Building CLI applications for Node.js, Bun, or Deno
|
|
24
19
|
- You want automatic `process.argv` parsing and `process.exit()` handling
|
|
25
20
|
- You need automatic terminal capability detection (colors, width)
|
|
26
21
|
- You prefer a simple, batteries-included approach
|
|
27
22
|
|
|
28
|
-
Use
|
|
23
|
+
Use *@optique/core* instead when:
|
|
29
24
|
|
|
30
25
|
- Building web applications or libraries
|
|
31
26
|
- You need full control over argument sources and error handling
|
|
@@ -62,12 +57,8 @@ const parser = object({
|
|
|
62
57
|
const config = run(parser);
|
|
63
58
|
|
|
64
59
|
console.log(`Hello ${config.name}!`);
|
|
65
|
-
if (config.age) {
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
if (config.verbose) {
|
|
69
|
-
console.log("Verbose mode enabled.");
|
|
70
|
-
}
|
|
60
|
+
if (config.age) console.log(`You are ${config.age} years old.`);
|
|
61
|
+
if (config.verbose) console.log("Verbose mode enabled.");
|
|
71
62
|
~~~~
|
|
72
63
|
|
|
73
64
|
Run it:
|
|
@@ -79,56 +70,6 @@ You are 25 years old.
|
|
|
79
70
|
Verbose mode enabled.
|
|
80
71
|
~~~~
|
|
81
72
|
|
|
73
|
+
For more resources, see the [docs] and the [*examples/*](/examples/) directory.
|
|
82
74
|
|
|
83
|
-
|
|
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()`)
|
|
75
|
+
[docs]: https://optique.dev/
|
package/dist/index.cjs
CHANGED
|
@@ -1,71 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const node_path = require_rolldown_runtime.__toESM(require("node:path"));
|
|
4
|
-
const node_process = require_rolldown_runtime.__toESM(require("node:process"));
|
|
1
|
+
const require_run = require('./run.cjs');
|
|
2
|
+
const require_valueparser = require('./valueparser.cjs');
|
|
5
3
|
|
|
6
|
-
|
|
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;
|
|
4
|
+
exports.path = require_valueparser.path;
|
|
5
|
+
exports.run = require_run.run;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,113 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
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 };
|
|
1
|
+
import { RunOptions, run } from "./run.cjs";
|
|
2
|
+
import { PathOptions, path } from "./valueparser.cjs";
|
|
3
|
+
export { PathOptions, RunOptions, path, run };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,113 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
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 };
|
|
1
|
+
import { RunOptions, run } from "./run.js";
|
|
2
|
+
import { PathOptions, path } from "./valueparser.js";
|
|
3
|
+
export { PathOptions, RunOptions, path, run };
|
package/dist/index.js
CHANGED
|
@@ -1,70 +1,4 @@
|
|
|
1
|
-
import { run
|
|
2
|
-
import path from "
|
|
3
|
-
import process from "node:process";
|
|
1
|
+
import { run } from "./run.js";
|
|
2
|
+
import { path } from "./valueparser.js";
|
|
4
3
|
|
|
5
|
-
|
|
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 };
|
|
4
|
+
export { path, run };
|
package/dist/run.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/run.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;
|
package/dist/run.d.cts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { InferValue, Parser } from "@optique/core/parser";
|
|
2
|
+
|
|
3
|
+
//#region src/run.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/run.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { InferValue, Parser } from "@optique/core/parser";
|
|
2
|
+
|
|
3
|
+
//#region src/run.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/run.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/run.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 };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const node_path = require_rolldown_runtime.__toESM(require("node:path"));
|
|
3
|
+
const __optique_core_message = require_rolldown_runtime.__toESM(require("@optique/core/message"));
|
|
4
|
+
const node_fs = require_rolldown_runtime.__toESM(require("node:fs"));
|
|
5
|
+
|
|
6
|
+
//#region src/valueparser.ts
|
|
7
|
+
/**
|
|
8
|
+
* Creates a ValueParser for file system paths with validation options.
|
|
9
|
+
*
|
|
10
|
+
* This parser provides filesystem validation and type checking for command-line
|
|
11
|
+
* path arguments. It can validate existence, file vs directory types, parent
|
|
12
|
+
* directory existence for new files, and file extensions.
|
|
13
|
+
*
|
|
14
|
+
* @param options Configuration options for path validation.
|
|
15
|
+
* @returns A ValueParser that validates and returns string paths.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { path } from "@optique/run";
|
|
20
|
+
* import { argument, object } from "@optique/core/parser";
|
|
21
|
+
*
|
|
22
|
+
* // Basic path parser (any path, no validation)
|
|
23
|
+
* const configFile = argument(path());
|
|
24
|
+
*
|
|
25
|
+
* // File must exist
|
|
26
|
+
* const inputFile = argument(path({ mustExist: true }));
|
|
27
|
+
*
|
|
28
|
+
* // Directory must exist
|
|
29
|
+
* const outputDir = argument(path({ mustExist: true, type: "directory" }));
|
|
30
|
+
*
|
|
31
|
+
* // File can be created (parent directory must exist)
|
|
32
|
+
* const logFile = argument(path({ type: "file", allowCreate: true }));
|
|
33
|
+
*
|
|
34
|
+
* // Config files with specific extensions
|
|
35
|
+
* const config = argument(path({
|
|
36
|
+
* mustExist: true,
|
|
37
|
+
* type: "file",
|
|
38
|
+
* extensions: [".json", ".yaml", ".yml"]
|
|
39
|
+
* }));
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function path(options = {}) {
|
|
43
|
+
const { metavar = "PATH", mustExist = false, type = "either", allowCreate = false, extensions } = options;
|
|
44
|
+
return {
|
|
45
|
+
metavar,
|
|
46
|
+
parse(input) {
|
|
47
|
+
if (extensions && extensions.length > 0) {
|
|
48
|
+
const ext = (0, node_path.extname)(input);
|
|
49
|
+
if (!extensions.includes(ext)) return {
|
|
50
|
+
success: false,
|
|
51
|
+
error: __optique_core_message.message`Expected file with extension ${(0, __optique_core_message.text)(extensions.join(", "))}, got ${(0, __optique_core_message.text)(ext || "no extension")}.`
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (mustExist) {
|
|
55
|
+
if (!(0, node_fs.existsSync)(input)) return {
|
|
56
|
+
success: false,
|
|
57
|
+
error: __optique_core_message.message`Path ${(0, __optique_core_message.text)(input)} does not exist.`
|
|
58
|
+
};
|
|
59
|
+
const stats = (0, node_fs.statSync)(input);
|
|
60
|
+
if (type === "file" && !stats.isFile()) return {
|
|
61
|
+
success: false,
|
|
62
|
+
error: __optique_core_message.message`Expected a file, but ${(0, __optique_core_message.text)(input)} is not a file.`
|
|
63
|
+
};
|
|
64
|
+
if (type === "directory" && !stats.isDirectory()) return {
|
|
65
|
+
success: false,
|
|
66
|
+
error: __optique_core_message.message`Expected a directory, but ${(0, __optique_core_message.text)(input)} is not a directory.`
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (allowCreate && !mustExist) {
|
|
70
|
+
const parentDir = (0, node_path.dirname)(input);
|
|
71
|
+
if (!(0, node_fs.existsSync)(parentDir)) return {
|
|
72
|
+
success: false,
|
|
73
|
+
error: __optique_core_message.message`Parent directory ${(0, __optique_core_message.text)(parentDir)} does not exist.`
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
success: true,
|
|
78
|
+
value: input
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
format(value) {
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
exports.path = path;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ValueParser } from "@optique/core/valueparser";
|
|
2
|
+
|
|
3
|
+
//#region src/valueparser.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the {@link path} value parser.
|
|
7
|
+
*/
|
|
8
|
+
interface PathOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The metavariable name for this parser, e.g., `"FILE"`, `"DIR"`.
|
|
11
|
+
* @default "PATH"
|
|
12
|
+
*/
|
|
13
|
+
readonly metavar?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Whether the path must exist on the filesystem.
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
readonly mustExist?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Expected type of path (file, directory, or either).
|
|
21
|
+
* Only checked when {@link mustExist} is true.
|
|
22
|
+
* @default "either"
|
|
23
|
+
*/
|
|
24
|
+
readonly type?: "file" | "directory" | "either";
|
|
25
|
+
/**
|
|
26
|
+
* Whether to allow creating new files/directories.
|
|
27
|
+
* When true and mustExist is false, validates that parent directory exists.
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
readonly allowCreate?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* File extensions to accept (for files only). Each extension must
|
|
33
|
+
* start with a dot (e.g. `".json"`, `".yaml"`).
|
|
34
|
+
*/
|
|
35
|
+
readonly extensions?: readonly string[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a ValueParser for file system paths with validation options.
|
|
39
|
+
*
|
|
40
|
+
* This parser provides filesystem validation and type checking for command-line
|
|
41
|
+
* path arguments. It can validate existence, file vs directory types, parent
|
|
42
|
+
* directory existence for new files, and file extensions.
|
|
43
|
+
*
|
|
44
|
+
* @param options Configuration options for path validation.
|
|
45
|
+
* @returns A ValueParser that validates and returns string paths.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { path } from "@optique/run";
|
|
50
|
+
* import { argument, object } from "@optique/core/parser";
|
|
51
|
+
*
|
|
52
|
+
* // Basic path parser (any path, no validation)
|
|
53
|
+
* const configFile = argument(path());
|
|
54
|
+
*
|
|
55
|
+
* // File must exist
|
|
56
|
+
* const inputFile = argument(path({ mustExist: true }));
|
|
57
|
+
*
|
|
58
|
+
* // Directory must exist
|
|
59
|
+
* const outputDir = argument(path({ mustExist: true, type: "directory" }));
|
|
60
|
+
*
|
|
61
|
+
* // File can be created (parent directory must exist)
|
|
62
|
+
* const logFile = argument(path({ type: "file", allowCreate: true }));
|
|
63
|
+
*
|
|
64
|
+
* // Config files with specific extensions
|
|
65
|
+
* const config = argument(path({
|
|
66
|
+
* mustExist: true,
|
|
67
|
+
* type: "file",
|
|
68
|
+
* extensions: [".json", ".yaml", ".yml"]
|
|
69
|
+
* }));
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
declare function path(options?: PathOptions): ValueParser<string>;
|
|
73
|
+
//#endregion
|
|
74
|
+
export { PathOptions, path };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ValueParser } from "@optique/core/valueparser";
|
|
2
|
+
|
|
3
|
+
//#region src/valueparser.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the {@link path} value parser.
|
|
7
|
+
*/
|
|
8
|
+
interface PathOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The metavariable name for this parser, e.g., `"FILE"`, `"DIR"`.
|
|
11
|
+
* @default "PATH"
|
|
12
|
+
*/
|
|
13
|
+
readonly metavar?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Whether the path must exist on the filesystem.
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
readonly mustExist?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Expected type of path (file, directory, or either).
|
|
21
|
+
* Only checked when {@link mustExist} is true.
|
|
22
|
+
* @default "either"
|
|
23
|
+
*/
|
|
24
|
+
readonly type?: "file" | "directory" | "either";
|
|
25
|
+
/**
|
|
26
|
+
* Whether to allow creating new files/directories.
|
|
27
|
+
* When true and mustExist is false, validates that parent directory exists.
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
readonly allowCreate?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* File extensions to accept (for files only). Each extension must
|
|
33
|
+
* start with a dot (e.g. `".json"`, `".yaml"`).
|
|
34
|
+
*/
|
|
35
|
+
readonly extensions?: readonly string[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a ValueParser for file system paths with validation options.
|
|
39
|
+
*
|
|
40
|
+
* This parser provides filesystem validation and type checking for command-line
|
|
41
|
+
* path arguments. It can validate existence, file vs directory types, parent
|
|
42
|
+
* directory existence for new files, and file extensions.
|
|
43
|
+
*
|
|
44
|
+
* @param options Configuration options for path validation.
|
|
45
|
+
* @returns A ValueParser that validates and returns string paths.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { path } from "@optique/run";
|
|
50
|
+
* import { argument, object } from "@optique/core/parser";
|
|
51
|
+
*
|
|
52
|
+
* // Basic path parser (any path, no validation)
|
|
53
|
+
* const configFile = argument(path());
|
|
54
|
+
*
|
|
55
|
+
* // File must exist
|
|
56
|
+
* const inputFile = argument(path({ mustExist: true }));
|
|
57
|
+
*
|
|
58
|
+
* // Directory must exist
|
|
59
|
+
* const outputDir = argument(path({ mustExist: true, type: "directory" }));
|
|
60
|
+
*
|
|
61
|
+
* // File can be created (parent directory must exist)
|
|
62
|
+
* const logFile = argument(path({ type: "file", allowCreate: true }));
|
|
63
|
+
*
|
|
64
|
+
* // Config files with specific extensions
|
|
65
|
+
* const config = argument(path({
|
|
66
|
+
* mustExist: true,
|
|
67
|
+
* type: "file",
|
|
68
|
+
* extensions: [".json", ".yaml", ".yml"]
|
|
69
|
+
* }));
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
declare function path(options?: PathOptions): ValueParser<string>;
|
|
73
|
+
//#endregion
|
|
74
|
+
export { PathOptions, path };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { dirname, extname } from "node:path";
|
|
2
|
+
import { message, text } from "@optique/core/message";
|
|
3
|
+
import { existsSync, statSync } from "node:fs";
|
|
4
|
+
|
|
5
|
+
//#region src/valueparser.ts
|
|
6
|
+
/**
|
|
7
|
+
* Creates a ValueParser for file system paths with validation options.
|
|
8
|
+
*
|
|
9
|
+
* This parser provides filesystem validation and type checking for command-line
|
|
10
|
+
* path arguments. It can validate existence, file vs directory types, parent
|
|
11
|
+
* directory existence for new files, and file extensions.
|
|
12
|
+
*
|
|
13
|
+
* @param options Configuration options for path validation.
|
|
14
|
+
* @returns A ValueParser that validates and returns string paths.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { path } from "@optique/run";
|
|
19
|
+
* import { argument, object } from "@optique/core/parser";
|
|
20
|
+
*
|
|
21
|
+
* // Basic path parser (any path, no validation)
|
|
22
|
+
* const configFile = argument(path());
|
|
23
|
+
*
|
|
24
|
+
* // File must exist
|
|
25
|
+
* const inputFile = argument(path({ mustExist: true }));
|
|
26
|
+
*
|
|
27
|
+
* // Directory must exist
|
|
28
|
+
* const outputDir = argument(path({ mustExist: true, type: "directory" }));
|
|
29
|
+
*
|
|
30
|
+
* // File can be created (parent directory must exist)
|
|
31
|
+
* const logFile = argument(path({ type: "file", allowCreate: true }));
|
|
32
|
+
*
|
|
33
|
+
* // Config files with specific extensions
|
|
34
|
+
* const config = argument(path({
|
|
35
|
+
* mustExist: true,
|
|
36
|
+
* type: "file",
|
|
37
|
+
* extensions: [".json", ".yaml", ".yml"]
|
|
38
|
+
* }));
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function path(options = {}) {
|
|
42
|
+
const { metavar = "PATH", mustExist = false, type = "either", allowCreate = false, extensions } = options;
|
|
43
|
+
return {
|
|
44
|
+
metavar,
|
|
45
|
+
parse(input) {
|
|
46
|
+
if (extensions && extensions.length > 0) {
|
|
47
|
+
const ext = extname(input);
|
|
48
|
+
if (!extensions.includes(ext)) return {
|
|
49
|
+
success: false,
|
|
50
|
+
error: message`Expected file with extension ${text(extensions.join(", "))}, got ${text(ext || "no extension")}.`
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (mustExist) {
|
|
54
|
+
if (!existsSync(input)) return {
|
|
55
|
+
success: false,
|
|
56
|
+
error: message`Path ${text(input)} does not exist.`
|
|
57
|
+
};
|
|
58
|
+
const stats = statSync(input);
|
|
59
|
+
if (type === "file" && !stats.isFile()) return {
|
|
60
|
+
success: false,
|
|
61
|
+
error: message`Expected a file, but ${text(input)} is not a file.`
|
|
62
|
+
};
|
|
63
|
+
if (type === "directory" && !stats.isDirectory()) return {
|
|
64
|
+
success: false,
|
|
65
|
+
error: message`Expected a directory, but ${text(input)} is not a directory.`
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (allowCreate && !mustExist) {
|
|
69
|
+
const parentDir = dirname(input);
|
|
70
|
+
if (!existsSync(parentDir)) return {
|
|
71
|
+
success: false,
|
|
72
|
+
error: message`Parent directory ${text(parentDir)} does not exist.`
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
success: true,
|
|
77
|
+
value: input
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
format(value) {
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//#endregion
|
|
87
|
+
export { path };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@optique/run",
|
|
3
|
-
"version": "0.1.0-dev.
|
|
3
|
+
"version": "0.1.0-dev.27+836bb5a6",
|
|
4
4
|
"description": "Type-safe combinatorial command-line interface parser",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CLI",
|
|
@@ -50,6 +50,22 @@
|
|
|
50
50
|
},
|
|
51
51
|
"import": "./dist/index.js",
|
|
52
52
|
"require": "./dist/index.cjs"
|
|
53
|
+
},
|
|
54
|
+
"./run": {
|
|
55
|
+
"types": {
|
|
56
|
+
"import": "./dist/run.d.ts",
|
|
57
|
+
"require": "./dist/run.cts"
|
|
58
|
+
},
|
|
59
|
+
"import": "./dist/run.js",
|
|
60
|
+
"require": "./dist/run.cjs"
|
|
61
|
+
},
|
|
62
|
+
"./valueparser": {
|
|
63
|
+
"types": {
|
|
64
|
+
"import": "./dist/valueparser.d.ts",
|
|
65
|
+
"require": "./dist/valueparser.cts"
|
|
66
|
+
},
|
|
67
|
+
"import": "./dist/valueparser.js",
|
|
68
|
+
"require": "./dist/valueparser.cjs"
|
|
53
69
|
}
|
|
54
70
|
},
|
|
55
71
|
"sideEffects": false,
|