Skip to content

Commit f25c17a

Browse files
committed
docs: add comment and example for InputInfo.Default
1 parent 6e0c7a3 commit f25c17a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ func (in InputInfo) WithValueName(name string) InputInfo {
308308
return in
309309
}
310310

311+
// Default sets v as the default string value for this InputInfo, which will be gathered and
312+
// parsed using this InputInfo's parser before any CLI arguments or environment variables.
313+
// This will always happen as the first step in parsing a command, so if a default value
314+
// is set here, then at least it will always be present meaning it's safe to use
315+
// [InputInfo.Get] to get its parsed value.
311316
func (in InputInfo) Default(v string) InputInfo {
312317
in.StrDefault = v
313318
in.HasStrDefault = true

examples_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ import (
1212
"github.com/steverusso/cli"
1313
)
1414

15+
func ExampleInputInfo_Default() {
16+
in := cli.New().Opt(cli.NewIntOpt("flag").Default("1234"))
17+
18+
c1 := in.ParseTheseOrExit()
19+
fmt.Println(cli.Get[int](c1, "flag")) // Default value present for 'flag' even though no CLI arg was provided.
20+
21+
c2 := in.ParseTheseOrExit("--flag", "5678")
22+
fmt.Println(cli.Get[int](c2, "flag"))
23+
// Output:
24+
// 1234
25+
// 5678
26+
}
27+
1528
func ExampleInputInfo_Short() {
1629
in := cli.New().
1730
Opt(cli.NewOpt("flag").Short('f'))

0 commit comments

Comments
 (0)