Skip to content

Commit 12dcc45

Browse files
committed
Add Duration
1 parent 6c29207 commit 12dcc45

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

cli/bigcli/bigcli.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Package bigcli offers an all-in-one solution for a highly configurable CLI
22
// application. Within Coder, we use it for our `server` subcommand, which
3-
// demands more than cobra/viper can offer.
3+
// demands more functionality than cobra/viper can offer.
44
//
55
// We may extend its usage to the rest of our application, completely replacing
6-
// cobra/viper, in the future. It's also a candidate to be broken out into its
7-
// own open-source library.
6+
// cobra/viper. It's also a candidate to be broken out into its own open-source
7+
// library, so we avoid deep coupling with Coder concepts.
88
package bigcli

cli/bigcli/option.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"golang.org/x/xerrors"
1010
)
1111

12+
// Disable is a sentinel value for Option.Flag and Option.Env to disable
13+
// features.
1214
const Disable = "-"
1315

1416
// Option is a configuration option for a CLI application.
@@ -30,12 +32,13 @@ type Option struct {
3032
Default string
3133
Value pflag.Value
3234

33-
// Annotations can be anything and everything you want. It's useful for
35+
// Annotations enable extensions to bigcli higher up in the stack. It's useful for
3436
// help formatting and documentation generation.
3537
Annotations map[string]string
3638
Hidden bool
3739
}
3840

41+
// FlagName returns the flag name for the option.
3942
func (o *Option) FlagName() (string, bool) {
4043
if o.Flag == Disable {
4144
return "", false
@@ -131,7 +134,7 @@ func (os *OptionSet) ParseEnv(globalPrefix string, environ []string) error {
131134
}
132135

133136
// SetDefaults sets the default values for each Option.
134-
// It should be called after all parsing (e.g. ParseFlags, ParseEnv, ParseConfig).
137+
// It should be called after all parsing (e.g. ParseFlags, ParseEnv).
135138
func (os *OptionSet) SetDefaults() error {
136139
var merr *multierror.Error
137140
for _, opt := range *os {

cli/bigcli/value.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package bigcli
22

3-
import "strconv"
3+
import (
4+
"strconv"
5+
"time"
6+
)
7+
8+
// values.go contains a standard set of value types that can be used as
9+
// Option Values.
410

511
type Int int
612

@@ -32,3 +38,19 @@ func (s String) String() string {
3238
func (String) Type() string {
3339
return "string"
3440
}
41+
42+
type Duration time.Duration
43+
44+
func (d *Duration) Set(v string) error {
45+
dd, err := time.ParseDuration(v)
46+
*d = Duration(dd)
47+
return err
48+
}
49+
50+
func (d *Duration) String() string {
51+
return time.Duration(*d).String()
52+
}
53+
54+
func (Duration) Type() string {
55+
return "duration"
56+
}

0 commit comments

Comments
 (0)