urfave-cli/flag.go

148 lines
2.6 KiB
Go
Raw Normal View History

2013-07-15 02:16:30 +00:00
package cli
import "fmt"
2013-07-17 02:59:04 +00:00
import "flag"
import "strconv"
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-09-24 01:41:31 +00:00
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
}
2013-09-24 01:41:31 +00:00
type StringSlice []string
func (f *StringSlice) Set(value string) error {
*f = append(*f, value)
2013-09-24 01:41:31 +00:00
return nil
}
func (f *StringSlice) String() string {
return fmt.Sprintf("%s", *f)
2013-09-24 01:41:31 +00:00
}
func (f *StringSlice) Value() []string {
return *f
2013-09-24 01:41:31 +00:00
}
type StringSliceFlag struct {
Name string
Value *StringSlice
Usage string
}
func (f StringSliceFlag) String() string {
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, "-"+f.Name+" option -"+f.Name+" option", f.Usage)
}
func (f StringSliceFlag) Apply(set *flag.FlagSet) {
set.Var(f.Value, f.Name, f.Usage)
}
type IntSlice []int
func (f *IntSlice) Set(value string) error {
tmp, err := strconv.Atoi(value)
if err != nil {
return err
} else {
*f = append(*f, tmp)
}
return nil
}
func (f *IntSlice) String() string {
return fmt.Sprintf("%d", *f)
}
func (f *IntSlice) Value() []int {
return *f
}
type IntSliceFlag struct {
Name string
Value *IntSlice
Usage string
}
func (f IntSliceFlag) String() string {
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, "-"+f.Name+" option -"+f.Name+" option", f.Usage)
}
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
set.Var(f.Value, f.Name, f.Usage)
}
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
}