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
Copy file name to clipboardExpand all lines: docs/docs/features/argument-parsing/flags.mdx
+62
Original file line number
Diff line number
Diff line change
@@ -147,6 +147,68 @@ import DefaultFlagCode from "./examples/default-flag.txt";
147
147
{DefaultFlagCode}
148
148
</StricliPlayground>
149
149
150
+
#### Default from Environment Variables
151
+
152
+
The default value can be sourced from anywhere, as long as it is a valid string. This includes environment variables, which can be useful for providing default values that are specific to the user's environment.
153
+
154
+
```ts
155
+
// output-next-line
156
+
/// impl.ts
157
+
exportdefaultfunction(flags: { token:string }) {
158
+
console.log(flags.token);
159
+
}
160
+
161
+
// output-next-line
162
+
/// command.ts
163
+
buildCommand({
164
+
loader: async () =>import("./impl"),
165
+
parameters: {
166
+
flags: {
167
+
token: {
168
+
kind: "parsed",
169
+
parse: String,
170
+
brief: "Token used for authentication",
171
+
// highlight-next-line
172
+
default: process.env["SOME_TOKEN"],
173
+
},
174
+
},
175
+
...
176
+
},
177
+
...
178
+
});
179
+
```
180
+
181
+
However, Stricli has built-in support for loading default values from environment variables. Specifying the default this way enables additional formatting to indicate to the user where this value came from, and the option to "redact" the value from the output. This is done by passing an option with the `env` property in the flag configuration. The value of this property should be a string that represents the name of the environment variable to load. If the user does not pass in a value for the flag and the environment variable is not set, it will throw an `UnsatisfiedFlagError`.
182
+
183
+
The additional `redact` config is optional, and if enabled will replace the value with `***` in the **Stricli-generated output**. Note that the value is still just a string passed to the flag, so there is nothing preventing the command implementation from displaying the value. There is no way for the command implementation to know where the value was sourced from, so it is up to the developer to ensure that the value is not printed if contains sensitive information.
184
+
185
+
```ts
186
+
// output-next-line
187
+
/// impl.ts
188
+
exportdefaultfunction(flags: { token:string }) {
189
+
console.log(flags.token);
190
+
}
191
+
192
+
// output-next-line
193
+
/// command.ts
194
+
buildCommand({
195
+
loader: async () =>import("./impl"),
196
+
parameters: {
197
+
flags: {
198
+
token: {
199
+
kind: "parsed",
200
+
parse: String,
201
+
brief: "Token used for authentication",
202
+
// highlight-next-line
203
+
default: { env: "SOME_TOKEN", redact: true },
204
+
},
205
+
},
206
+
...
207
+
},
208
+
...
209
+
});
210
+
```
211
+
150
212
### Variadic
151
213
152
214
A flag can be variadic when the type it represents is an array of values. In this case, the flag can be specified multiple times and each value is then parsed individually and added to a single array. If the type of a flag is an array it must be set as variadic.
0 commit comments