File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff 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.
311316func (in InputInfo ) Default (v string ) InputInfo {
312317 in .StrDefault = v
313318 in .HasStrDefault = true
Original file line number Diff line number Diff 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+
1528func ExampleInputInfo_Short () {
1629 in := cli .New ().
1730 Opt (cli .NewOpt ("flag" ).Short ('f' ))
You can’t perform that action at this time.
0 commit comments