Support for global flags in help text
This commit is contained in:
parent
16bf6d4f76
commit
921da63e2e
7
cli.go
7
cli.go
@ -1,6 +1,7 @@
|
||||
package cli
|
||||
|
||||
import "os"
|
||||
import "flag"
|
||||
|
||||
// The name of the program. Defaults to os.Args[0]
|
||||
var Name = os.Args[0]
|
||||
@ -12,8 +13,11 @@ var Usage = "<No Description>"
|
||||
var Version = "0.0.0"
|
||||
|
||||
// List of commands to execute
|
||||
var Commands []Command = nil
|
||||
var Commands []Command
|
||||
|
||||
var Flags []Flag
|
||||
|
||||
// The action to execute when no subcommands are specified
|
||||
var DefaultAction = ShowHelp
|
||||
|
||||
func Run(args []string) {
|
||||
@ -37,6 +41,7 @@ type Command struct {
|
||||
Usage string
|
||||
Description string
|
||||
Action Action
|
||||
Flags flag.FlagSet
|
||||
}
|
||||
|
||||
type Action func(name string)
|
||||
|
27
flag.go
Normal file
27
flag.go
Normal file
@ -0,0 +1,27 @@
|
||||
package cli
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Flag interface {
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type BoolFlag struct {
|
||||
Name string
|
||||
Value bool
|
||||
Usage string
|
||||
}
|
||||
|
||||
type StringFlag struct {
|
||||
Name string
|
||||
Value string
|
||||
Usage string
|
||||
}
|
||||
|
||||
func (f StringFlag) String() string {
|
||||
return fmt.Sprintf("--%v 'string'\t%v", f.Name, f.Usage)
|
||||
}
|
||||
|
||||
func (f BoolFlag) String() string {
|
||||
return fmt.Sprintf("--%v\t%v", f.Name, f.Usage)
|
||||
}
|
9
help.go
9
help.go
@ -7,8 +7,9 @@ import "text/template"
|
||||
type HelpData struct {
|
||||
Name string
|
||||
Usage string
|
||||
Commands []Command
|
||||
Version string
|
||||
Commands []Command
|
||||
Flags []Flag
|
||||
}
|
||||
|
||||
var HelpCommand = Command{
|
||||
@ -34,12 +35,16 @@ VERSION:
|
||||
COMMANDS:
|
||||
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
|
||||
{{end}}
|
||||
GLOBAL OPTIONS
|
||||
{{range .Flags}}{{.}}
|
||||
{{end}}
|
||||
`
|
||||
data := HelpData{
|
||||
Name,
|
||||
Usage,
|
||||
append(Commands, HelpCommand),
|
||||
Version,
|
||||
append(Commands, HelpCommand),
|
||||
Flags,
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
|
||||
|
Loading…
Reference in New Issue
Block a user