You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added onMissing callback support to flags, options, and custom types
6
+
7
+
That allows providing dynamic fallback values when command-line arguments are not provided, This enables:
8
+
9
+
- Hiding default values from help output
10
+
- Interactive prompts: Ask users for input when flags/options are missing
11
+
- Environment-based defaults: Check environment variables or config files dynamically
12
+
- Auto-discovery: Automatically find files or resources when not specified
13
+
- Async support: Handle both synchronous and asynchronous fallback logic
14
+
15
+
The onMissing callback is used as a fallback when defaultValue is not provided, following the precedence order: environment variables β defaultValue β onMissing β type defaults.
-`description` to provide a default description for this type
66
66
-`displayName` is a short way to describe the type in the help
67
67
-`defaultValue(): B` to allow the type to be optional and have a default value
68
+
-`onMissing(): B | Promise<B>` to provide a dynamic fallback when the argument is not provided (used as fallback if `defaultValue` is not provided)
68
69
69
70
Using the type we've just created is no different that using `string`:
70
71
@@ -93,4 +94,40 @@ Now, we can add more features to our `ReadStream` type and stop touching our cod
93
94
- We can try to parse the string as a URI and check if the protocol is HTTP, if so - make an HTTP request and return the body stream
94
95
- We can see if the string is `-`, and when it happens, return `process.stdin` like many Unix applications
95
96
97
+
### Custom Types with `onMissing`
98
+
99
+
Custom types can also provide dynamic defaults using `onMissing`. This is useful when you want the type itself to determine what happens when no argument is provided:
100
+
101
+
```ts
102
+
const ConfigFile:Type<string, Config> = {
103
+
async from(str) {
104
+
if (!fs.existsSync(str)) {
105
+
thrownewError(`Config file not found: ${str}`);
106
+
}
107
+
returnJSON.parse(fs.readFileSync(str, 'utf8'));
108
+
},
109
+
110
+
displayName: 'config-file',
111
+
112
+
async onMissing() {
113
+
// Look for config in standard locations when not provided
And the best thing about it β everything is encapsulated to an easily tested type definition, which can be easily shared and reused. Take a look at [io-ts-types](https://github.com/gcanti/io-ts-types), for instance, which has types like DateFromISOString, NumberFromString and more, which is something we can totally do.
Copy file name to clipboardExpand all lines: docs/parsers/flags.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,46 @@ const cmd = command({
46
46
});
47
47
```
48
48
49
+
### Dynamic Defaults with `onMissing`
50
+
51
+
The `onMissing` callback provides a way to dynamically generate values when a flag is not provided. This is useful for environment-based defaults, configuration file lookups, or user prompts.
52
+
53
+
```ts
54
+
import { command, flag } from'cmd-ts';
55
+
56
+
const verboseFlag =flag({
57
+
long: 'verbose',
58
+
short: 'v',
59
+
description: 'Enable verbose output',
60
+
onMissing: () => {
61
+
// Check environment variable as fallback
62
+
returnprocess.env.NODE_ENV==='development';
63
+
},
64
+
});
65
+
66
+
const debugFlag =flag({
67
+
long: 'debug',
68
+
short: 'd',
69
+
description: 'Enable debug mode',
70
+
onMissing: async () => {
71
+
// Async example: check config file or make API call
-`type` (required): A type from `string[]` to any value
@@ -74,3 +148,4 @@ This parser will fail to parse if:
74
148
-`displayName`: A short description regarding the option
75
149
-`defaultValue`: A function that returns a default value for the option array in case no options were provided. If not provided, the default value will be an empty array.
76
150
-`defaultValueIsSerializable`: Whether to print the defaultValue as a string in the help docs.
151
+
-`onMissing`: A function (sync or async) that returns a value when the option is not provided. Used as fallback if `defaultValue` is not provided.
0 commit comments