2013-07-15 02:16:30 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import "fmt"
|
2013-07-17 02:59:04 +00:00
|
|
|
import "flag"
|
2013-07-15 02:16:30 +00:00
|
|
|
|
|
|
|
type Flag interface {
|
2013-07-19 00:39:42 +00:00
|
|
|
fmt.Stringer
|
|
|
|
Apply(*flag.FlagSet)
|
|
|
|
}
|
|
|
|
|
2013-07-20 15:21:20 +00:00
|
|
|
func flagSet(name string, flags []Flag) *flag.FlagSet {
|
2013-07-24 14:35:45 +00:00
|
|
|
set := flag.NewFlagSet(name, flag.ContinueOnError)
|
2013-07-19 00:39:42 +00:00
|
|
|
for _, f := range flags {
|
|
|
|
f.Apply(set)
|
|
|
|
}
|
|
|
|
return set
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BoolFlag struct {
|
2013-07-19 00:39:42 +00:00
|
|
|
Name string
|
|
|
|
Usage string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f BoolFlag) String() string {
|
2013-09-09 18:51:46 +00:00
|
|
|
return fmt.Sprintf("%s%v\t%v", prefixFor(f.Name), f.Name, f.Usage)
|
2013-07-19 00:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f BoolFlag) Apply(set *flag.FlagSet) {
|
|
|
|
set.Bool(f.Name, false, f.Usage)
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type StringFlag struct {
|
2013-07-19 00:39:42 +00:00
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
Usage string
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f StringFlag) String() string {
|
2013-09-09 18:51:46 +00:00
|
|
|
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, f.Value, f.Usage)
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 02:59:04 +00:00
|
|
|
func (f StringFlag) Apply(set *flag.FlagSet) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set.String(f.Name, f.Value, f.Usage)
|
2013-07-17 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 00:39:42 +00:00
|
|
|
type IntFlag struct {
|
|
|
|
Name string
|
|
|
|
Value int
|
|
|
|
Usage string
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
2013-07-17 02:59:04 +00:00
|
|
|
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f IntFlag) String() string {
|
2013-09-09 18:51:46 +00:00
|
|
|
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, f.Value, f.Usage)
|
2013-07-17 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f IntFlag) Apply(set *flag.FlagSet) {
|
|
|
|
set.Int(f.Name, f.Value, f.Usage)
|
2013-07-17 02:59:04 +00:00
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
|
|
|
|
type helpFlag struct {
|
|
|
|
Usage string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f helpFlag) String() string {
|
|
|
|
return fmt.Sprintf("--help, -h\t%v", f.Usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f helpFlag) Apply(set *flag.FlagSet) {
|
|
|
|
set.Bool("h", false, f.Usage)
|
|
|
|
set.Bool("help", false, f.Usage)
|
|
|
|
}
|
2013-09-09 18:51:46 +00:00
|
|
|
|
|
|
|
func prefixFor(name string) (prefix string) {
|
|
|
|
if len(name) == 1 {
|
|
|
|
prefix = "-"
|
|
|
|
} else {
|
|
|
|
prefix = "--"
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|