urfave-cli/flag.go
2013-11-18 16:35:23 -07:00

193 lines
3.5 KiB
Go

package cli
import "fmt"
import "flag"
import "strconv"
import "strings"
// Flag is a common interface related to parsing flags in cli.
// For more advanced flag parsing techniques, it is recomended that
// this interface be implemented.
type Flag interface {
fmt.Stringer
// Apply Flag settings to the given flag set
Apply(*flag.FlagSet)
GetName() string
}
func flagSet(name string, flags []Flag) *flag.FlagSet {
set := flag.NewFlagSet(name, flag.ContinueOnError)
for _, f := range flags {
f.Apply(set)
}
return set
}
type StringSlice []string
func (f *StringSlice) Set(value string) error {
*f = append(*f, value)
return nil
}
func (f *StringSlice) String() string {
return fmt.Sprintf("%s", *f)
}
func (f *StringSlice) Value() []string {
return *f
}
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) {
parts := strings.Split(f.Name, ", ")
for _, name := range parts {
set.Var(f.Value, name, f.Usage)
}
}
func (f StringSliceFlag) GetName() string {
return f.Name
}
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) {
parts := strings.Split(f.Name, ", ")
for _, name := range parts {
set.Var(f.Value, name, f.Usage)
}
}
func (f IntSliceFlag) GetName() string {
return f.Name
}
type BoolFlag struct {
Name string
Usage string
}
func (f BoolFlag) String() string {
return fmt.Sprintf("%s%v\t%v", prefixFor(f.Name), f.Name, f.Usage)
}
func (f BoolFlag) Apply(set *flag.FlagSet) {
parts := strings.Split(f.Name, ", ")
for _, name := range parts {
set.Bool(name, false, f.Usage)
}
}
func (f BoolFlag) GetName() string {
return f.Name
}
type StringFlag struct {
Name string
Value string
Usage string
}
func (f StringFlag) String() string {
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, f.Value, f.Usage)
}
func (f StringFlag) Apply(set *flag.FlagSet) {
parts := strings.Split(f.Name, ", ")
for _, name := range parts {
set.String(name, f.Value, f.Usage)
}
}
func (f StringFlag) GetName() string {
return f.Name
}
type IntFlag struct {
Name string
Value int
Usage string
}
func (f IntFlag) String() string {
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, f.Value, f.Usage)
}
func (f IntFlag) Apply(set *flag.FlagSet) {
parts := strings.Split(f.Name, ", ")
for _, name := range parts {
set.Int(name, f.Value, f.Usage)
}
}
func (f IntFlag) GetName() string {
return f.Name
}
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)
}
func (f helpFlag) GetName() string {
return "help"
}
func prefixFor(name string) (prefix string) {
if len(name) == 1 {
prefix = "-"
} else {
prefix = "--"
}
return
}