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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagSet(flags []Flag) *flag.FlagSet {
|
2013-07-19 15:34:01 +00:00
|
|
|
set := flag.NewFlagSet("cli", flag.ExitOnError)
|
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 {
|
|
|
|
return fmt.Sprintf("--%v\t%v", f.Name, f.Usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-19 00:39:42 +00:00
|
|
|
return fmt.Sprintf("--%v '%v'\t%v", 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 {
|
|
|
|
return fmt.Sprintf("--%v '%v'\t%v", 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
|
|
|
}
|