/
options.go
53 lines (46 loc) · 1019 Bytes
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package guinea
import (
"fmt"
)
type ValType int
const (
String ValType = iota
Bool
Int
)
// Option represents an optional flag.
type Option struct {
Name string
Type ValType
Default interface{}
Description string
}
// String prepends the option name with one or two leading dashes and returns
// it. It is used to generate help texts.
func (opt Option) String() string {
prefix := "-"
if len(opt.Name) > 1 {
prefix = "--"
}
return prefix + opt.Name
}
// Argument represents a required argument.
type Argument struct {
Name string
Multiple bool
Optional bool
Description string
}
// String places the argument name in angle brackets and appends three dots to
// it in order to indicate multiple arguments. It is used to generate help
// texts.
func (arg Argument) String() string {
format := "<%s>"
if arg.Multiple {
format = fmt.Sprintf("%s...", format)
}
if arg.Optional {
format = fmt.Sprintf("[%s]", format)
}
return fmt.Sprintf(format, arg.Name)
}