ffmd/examples/multiple/main.go

94 lines
2.2 KiB
Go

//go:build generate
// +build generate
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/peterbourgon/ff/v3/ffcli"
"go.jolheiser.com/ffmd"
)
//go:generate go run main.go
func main() {
fs := flagSet("myapp-")
cmd := &ffcli.Command{
Name: "myapp",
FlagSet: fs,
}
fs1 := flagSet("sub1-")
cmd1 := &ffcli.Command{
Name: "sub1",
ShortUsage: "root [FLAGS] sub1 [FLAGS] [ARGS...]",
FlagSet: fs1,
}
fs2 := flagSet("sub2-")
cmd2 := &ffcli.Command{
Name: "sub2",
ShortUsage: "root [FLAGS] sub2 [FLAGS] [ARGS...]",
ShortHelp: "Short help",
FlagSet: fs2,
}
fs3 := flagSet("sub3-")
cmd3 := &ffcli.Command{
Name: "sub3",
ShortUsage: "root [FLAGS] sub2 [FLAGS] sub3 [FLAGS] [ARGS...]",
ShortHelp: "Short help",
LongHelp: "Long help",
FlagSet: fs3,
}
fs4 := flagSet("sub4-")
cmd4 := &ffcli.Command{
Name: "sub4",
FlagSet: fs4,
}
fs5 := flagSet("sub5-")
cmd5 := &ffcli.Command{
Name: "sub5",
FlagSet: fs5,
}
fs6 := flagSet("sub6-")
cmd6 := &ffcli.Command{
Name: "sub6",
FlagSet: fs6,
}
cmd.Subcommands = []*ffcli.Command{cmd1, cmd2, cmd6}
cmd2.Subcommands = []*ffcli.Command{cmd3, cmd5}
cmd3.Subcommands = []*ffcli.Command{cmd4}
md, err := ffmd.Command(cmd)
if err != nil {
panic(err)
}
write("README.md", md)
}
func write(path, content string) {
fi, err := os.Create(path)
if err != nil {
panic(err)
}
defer fi.Close()
if _, err := fi.WriteString(content); err != nil {
panic(err)
}
}
func flagSet(name string) *flag.FlagSet {
fs := flag.NewFlagSet("myapp", flag.ExitOnError)
fs.String(fmt.Sprintf("%sstring-flag", name), "", "String flag with no default")
fs.String(fmt.Sprintf("%sstring-flag-default", name), "string default", "String flag with default")
fs.Int(fmt.Sprintf("%sint-flag", name), 0, "Int flag with no default")
fs.Int(fmt.Sprintf("%sint-flag-default", name), 100, "Int flag with default")
fs.Bool(fmt.Sprintf("%sbool-flag-f", name), false, "Bool flag false")
fs.Bool(fmt.Sprintf("%sbool-flag-t", name), true, "Bool flag true")
fs.Duration(fmt.Sprintf("%sduration-flag", name), 0, "Duration flag with no default")
fs.Duration(fmt.Sprintf("%sduration-flag-default", name), time.Minute*5, "Duration flag with default")
return fs
}