Skip to content

Commit c332ed8

Browse files
feat: set CLI flag constant values automatically where x-stainless-const is set
1 parent 42310f5 commit c332ed8

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

internal/requestflag/requestflag.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ type Flag[
3737
BodyPath string // location in the request body to put this flag's value
3838
BodyRoot bool // if true, then use this value as the entire request body
3939

40+
// Const, when true, marks this flag as a constant. The flag's Default value is used as the fixed value
41+
// and always included in the request (IsSet returns true). The user can still see and override the flag,
42+
// but isn't required to provide it. This is used for single-value enums and `x-stainless-const`
43+
// parameters.
44+
Const bool
45+
4046
// unexported fields for internal use
4147
count int // number of times the flag has been set
4248
hasBeenSet bool // whether the flag has been set from env or file
@@ -229,7 +235,7 @@ func (f *Flag[T]) String() string {
229235
}
230236

231237
func (f *Flag[T]) IsSet() bool {
232-
return f.hasBeenSet
238+
return f.hasBeenSet || f.Const
233239
}
234240

235241
func (f *Flag[T]) Names() []string {
@@ -255,6 +261,10 @@ func (f *Flag[T]) SetCategory(c string) {
255261
var _ cli.RequiredFlag = (*Flag[any])(nil) // Type assertion to ensure interface compliance
256262

257263
func (f *Flag[T]) IsRequired() bool {
264+
// Const flags are always auto-set, so never required from the user.
265+
if f.Const {
266+
return false
267+
}
258268
// Intentionally don't use `f.Required`, because request flags may be passed
259269
// over stdin as well as by flag.
260270
if f.BodyPath != "" || f.BodyRoot {
@@ -268,6 +278,10 @@ type RequiredFlagOrStdin interface {
268278
}
269279

270280
func (f *Flag[T]) IsRequiredAsFlagOrStdin() bool {
281+
// Const flags are always auto-set, so never required from the user.
282+
if f.Const {
283+
return false
284+
}
271285
return f.Required
272286
}
273287

0 commit comments

Comments
 (0)