From e2733adc452e66dd7dfca4ad722ab17116009fa6 Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Tue, 16 Jul 2013 19:59:04 -0700 Subject: [PATCH] JMS #4: Kinda parsing flags now --- cli.go | 8 ++++++-- flag.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cli.go b/cli.go index 6c86876..b864865 100644 --- a/cli.go +++ b/cli.go @@ -1,7 +1,7 @@ package cli +import "fmt" import "os" -import "flag" // The name of the program. Defaults to os.Args[0] var Name = os.Args[0] @@ -21,6 +21,10 @@ var Flags []Flag var Action = ShowHelp func Run(args []string) { + + set := flagSet(Flags) + set.Parse(args[1:]) + context := Context{} if len(args) > 1 { name := args[1] @@ -42,7 +46,7 @@ type Command struct { Usage string Description string Action Handler - Flags flag.FlagSet + Flags []Flag } type Context struct { diff --git a/flag.go b/flag.go index 8003659..aabb86c 100644 --- a/flag.go +++ b/flag.go @@ -1,9 +1,11 @@ package cli import "fmt" +import "flag" type Flag interface { fmt.Stringer + Apply(*flag.FlagSet) } type BoolFlag struct { @@ -21,6 +23,22 @@ func (f StringFlag) String() string { return fmt.Sprintf("--%v 'string'\t%v", f.Name, f.Usage) } +func (f StringFlag) Apply(set *flag.FlagSet) { + set.String(f.Name, f.Value, f.Usage) +} + 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) +} + +func flagSet(flags []Flag) *flag.FlagSet { + set := flag.NewFlagSet("set", flag.PanicOnError) + for _, f := range flags { + f.Apply(set) + } + return set +}