JMS #4: Kinda parsing flags now
This commit is contained in:
parent
4b4324bb47
commit
e2733adc45
8
cli.go
8
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 {
|
||||
|
18
flag.go
18
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user