Skip to content

feature request: dynamic defaults #4

@teidesu

Description

@teidesu

in some cases a default value for an argument might be from an env. variable (or otherwise computed), in which case using a .default(process.env.FOO) would a) result in bad ux for the user in --help (they wouldn't know where the value comes from), and b) might be expensive

currently the workaround is to keep the argument as optional, put the default value description in the .desc() and use ?? in handler/transform function, which adds a bit of boilerplate

i propose an overload for the .default() method that would accept a function and optionally a textual description of what happens there:

const foo = command({
  name: 'foo',
  options: {
    foo: string().default(() => process.env.FOO, 'process.env.FOO'),
    bar: number().default(() => 2 ** 32),
  }
})

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions