@optique/env 1.1.0-dev.2006 → 1.1.0-dev.2054

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
@@ -36,6 +36,14 @@ function defaultEnvSource(key) {
36
36
  /**
37
37
  * Creates an environment context for use with Optique runners.
38
38
  *
39
+ * Pass the returned context to `run()`'s `contexts` option so that
40
+ * `bindEnv()` can read environment variables during parsing:
41
+ *
42
+ * ```typescript
43
+ * const envContext = createEnvContext({ prefix: "MYAPP_" });
44
+ * run(parser, { contexts: [envContext] });
45
+ * ```
46
+ *
39
47
  * When calling `context.getAnnotations()` manually, pass the returned
40
48
  * annotations to low-level APIs such as `parse()`, `parseAsync()`,
41
49
  * `parser.complete()`, `suggest()`, or `getDocPage()`. Since environment
@@ -81,6 +89,18 @@ function createEnvContext(options = {}) {
81
89
  * 3. Default value
82
90
  * 4. Error
83
91
  *
92
+ * > **Important:** `bindEnv()` only reads environment variables when its
93
+ * > `EnvContext` is registered with the runner via the `contexts` option:
94
+ * >
95
+ * > ```typescript
96
+ * > run(parser, { contexts: [envContext] });
97
+ * > ```
98
+ * >
99
+ * > Omitting `contexts` causes `bindEnv()` to skip the env lookup and fall
100
+ * > through to the default or an error. When other contexts are registered
101
+ * > but this env context is not, the error explicitly names the `contexts`
102
+ * > option to aid diagnosis.
103
+ *
84
104
  * @param parser Parser that reads CLI values.
85
105
  * @param options Environment binding options.
86
106
  * @returns A parser with environment fallback behavior.
@@ -294,10 +314,23 @@ function getEnvOrDefault(state, options, mode, innerParser, innerState, exec) {
294
314
  success: true,
295
315
  value: options.default
296
316
  }));
317
+ const envContextAbsent = annotations != null && !(options.context.id in annotations);
297
318
  if (innerParser != null) {
298
319
  const completeState = innerState ?? (annotations != null && innerParser.initialState == null && (0, __optique_core_extension.getTraits)(innerParser).inheritsAnnotations === true ? (0, __optique_core_extension.injectAnnotations)(innerParser.initialState, annotations) : innerParser.initialState);
299
- return (0, __optique_core_extension.wrapForMode)(mode, innerParser.complete(completeState, exec));
320
+ const innerResult = innerParser.complete(completeState, exec);
321
+ if (envContextAbsent) {
322
+ const unregisteredError = {
323
+ success: false,
324
+ error: __optique_core_message.message`Environment variable ${(0, __optique_core_message.envVar)(fullKey)} could not be read: the env context was not passed to run()'s contexts option.`
325
+ };
326
+ return (0, __optique_core_extension.mapModeValue)(mode, innerResult, (r) => r.success ? r : unregisteredError);
327
+ }
328
+ return (0, __optique_core_extension.wrapForMode)(mode, innerResult);
300
329
  }
330
+ if (envContextAbsent) return (0, __optique_core_extension.wrapForMode)(mode, {
331
+ success: false,
332
+ error: __optique_core_message.message`Environment variable ${(0, __optique_core_message.envVar)(fullKey)} could not be read: the env context was not passed to run()'s contexts option.`
333
+ });
301
334
  return (0, __optique_core_extension.wrapForMode)(mode, {
302
335
  success: false,
303
336
  error: __optique_core_message.message`Missing required environment variable: ${(0, __optique_core_message.envVar)(fullKey)}`
package/dist/index.d.cts CHANGED
@@ -48,6 +48,14 @@ interface EnvContextOptions {
48
48
  /**
49
49
  * Creates an environment context for use with Optique runners.
50
50
  *
51
+ * Pass the returned context to `run()`'s `contexts` option so that
52
+ * `bindEnv()` can read environment variables during parsing:
53
+ *
54
+ * ```typescript
55
+ * const envContext = createEnvContext({ prefix: "MYAPP_" });
56
+ * run(parser, { contexts: [envContext] });
57
+ * ```
58
+ *
51
59
  * When calling `context.getAnnotations()` manually, pass the returned
52
60
  * annotations to low-level APIs such as `parse()`, `parseAsync()`,
53
61
  * `parser.complete()`, `suggest()`, or `getDocPage()`. Since environment
@@ -99,6 +107,18 @@ interface BindEnvOptions<M extends Mode, TValue> {
99
107
  * 3. Default value
100
108
  * 4. Error
101
109
  *
110
+ * > **Important:** `bindEnv()` only reads environment variables when its
111
+ * > `EnvContext` is registered with the runner via the `contexts` option:
112
+ * >
113
+ * > ```typescript
114
+ * > run(parser, { contexts: [envContext] });
115
+ * > ```
116
+ * >
117
+ * > Omitting `contexts` causes `bindEnv()` to skip the env lookup and fall
118
+ * > through to the default or an error. When other contexts are registered
119
+ * > but this env context is not, the error explicitly names the `contexts`
120
+ * > option to aid diagnosis.
121
+ *
102
122
  * @param parser Parser that reads CLI values.
103
123
  * @param options Environment binding options.
104
124
  * @returns A parser with environment fallback behavior.
package/dist/index.d.ts CHANGED
@@ -48,6 +48,14 @@ interface EnvContextOptions {
48
48
  /**
49
49
  * Creates an environment context for use with Optique runners.
50
50
  *
51
+ * Pass the returned context to `run()`'s `contexts` option so that
52
+ * `bindEnv()` can read environment variables during parsing:
53
+ *
54
+ * ```typescript
55
+ * const envContext = createEnvContext({ prefix: "MYAPP_" });
56
+ * run(parser, { contexts: [envContext] });
57
+ * ```
58
+ *
51
59
  * When calling `context.getAnnotations()` manually, pass the returned
52
60
  * annotations to low-level APIs such as `parse()`, `parseAsync()`,
53
61
  * `parser.complete()`, `suggest()`, or `getDocPage()`. Since environment
@@ -99,6 +107,18 @@ interface BindEnvOptions<M extends Mode, TValue> {
99
107
  * 3. Default value
100
108
  * 4. Error
101
109
  *
110
+ * > **Important:** `bindEnv()` only reads environment variables when its
111
+ * > `EnvContext` is registered with the runner via the `contexts` option:
112
+ * >
113
+ * > ```typescript
114
+ * > run(parser, { contexts: [envContext] });
115
+ * > ```
116
+ * >
117
+ * > Omitting `contexts` causes `bindEnv()` to skip the env lookup and fall
118
+ * > through to the default or an error. When other contexts are registered
119
+ * > but this env context is not, the error explicitly names the `contexts`
120
+ * > option to aid diagnosis.
121
+ *
102
122
  * @param parser Parser that reads CLI values.
103
123
  * @param options Environment binding options.
104
124
  * @returns A parser with environment fallback behavior.
package/dist/index.js CHANGED
@@ -13,6 +13,14 @@ function defaultEnvSource(key) {
13
13
  /**
14
14
  * Creates an environment context for use with Optique runners.
15
15
  *
16
+ * Pass the returned context to `run()`'s `contexts` option so that
17
+ * `bindEnv()` can read environment variables during parsing:
18
+ *
19
+ * ```typescript
20
+ * const envContext = createEnvContext({ prefix: "MYAPP_" });
21
+ * run(parser, { contexts: [envContext] });
22
+ * ```
23
+ *
16
24
  * When calling `context.getAnnotations()` manually, pass the returned
17
25
  * annotations to low-level APIs such as `parse()`, `parseAsync()`,
18
26
  * `parser.complete()`, `suggest()`, or `getDocPage()`. Since environment
@@ -58,6 +66,18 @@ function createEnvContext(options = {}) {
58
66
  * 3. Default value
59
67
  * 4. Error
60
68
  *
69
+ * > **Important:** `bindEnv()` only reads environment variables when its
70
+ * > `EnvContext` is registered with the runner via the `contexts` option:
71
+ * >
72
+ * > ```typescript
73
+ * > run(parser, { contexts: [envContext] });
74
+ * > ```
75
+ * >
76
+ * > Omitting `contexts` causes `bindEnv()` to skip the env lookup and fall
77
+ * > through to the default or an error. When other contexts are registered
78
+ * > but this env context is not, the error explicitly names the `contexts`
79
+ * > option to aid diagnosis.
80
+ *
61
81
  * @param parser Parser that reads CLI values.
62
82
  * @param options Environment binding options.
63
83
  * @returns A parser with environment fallback behavior.
@@ -271,10 +291,23 @@ function getEnvOrDefault(state, options, mode, innerParser, innerState, exec) {
271
291
  success: true,
272
292
  value: options.default
273
293
  }));
294
+ const envContextAbsent = annotations != null && !(options.context.id in annotations);
274
295
  if (innerParser != null) {
275
296
  const completeState = innerState ?? (annotations != null && innerParser.initialState == null && getTraits(innerParser).inheritsAnnotations === true ? injectAnnotations(innerParser.initialState, annotations) : innerParser.initialState);
276
- return wrapForMode(mode, innerParser.complete(completeState, exec));
297
+ const innerResult = innerParser.complete(completeState, exec);
298
+ if (envContextAbsent) {
299
+ const unregisteredError = {
300
+ success: false,
301
+ error: message`Environment variable ${envVar(fullKey)} could not be read: the env context was not passed to run()'s contexts option.`
302
+ };
303
+ return mapModeValue(mode, innerResult, (r) => r.success ? r : unregisteredError);
304
+ }
305
+ return wrapForMode(mode, innerResult);
277
306
  }
307
+ if (envContextAbsent) return wrapForMode(mode, {
308
+ success: false,
309
+ error: message`Environment variable ${envVar(fullKey)} could not be read: the env context was not passed to run()'s contexts option.`
310
+ });
278
311
  return wrapForMode(mode, {
279
312
  success: false,
280
313
  error: message`Missing required environment variable: ${envVar(fullKey)}`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/env",
3
- "version": "1.1.0-dev.2006+b336c042",
3
+ "version": "1.1.0-dev.2054",
4
4
  "description": "Environment variable support for Optique",
5
5
  "keywords": [
6
6
  "CLI",
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "sideEffects": false,
56
56
  "dependencies": {
57
- "@optique/core": "1.1.0-dev.2006+b336c042"
57
+ "@optique/core": "1.1.0-dev.2054+d94be831"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@types/node": "^20.19.9",