Skip to content

Instantly share code, notes, and snippets.

@technosophos
Created September 23, 2014 03:55
Show Gist options
  • Select an option

  • Save technosophos/32b1815a4ff26cf5bf48 to your computer and use it in GitHub Desktop.

Select an option

Save technosophos/32b1815a4ff26cf5bf48 to your computer and use it in GitHub Desktop.
Example of Cookoo subcommand.
package main
import (
"github.com/Masterminds/cookoo"
"github.com/Masterminds/cookoo/cli"
"github.com/Masterminds/cookoo/fmt"
"flag"
)
func main() {
reg, router, cxt := cookoo.Cookoo()
// global flags
flags := flag.NewFlagSet("global", flag.PanicOnError)
flags.Bool("h", false, "Print help")
// Create a new app
app := cli.New(reg, router, cxt).Help("Test", "test -h", flags)
// Create a new subcommand on that app
app.Subcommand("test", "A test route.", "example test", nil).
Does(fmt.Println, "out").Using("content").WithDefault("Hello World")
// Subcommand with flags.
helloFlags := flag.NewFlagSet("test", flag.ContinueOnError)
helloFlags.Bool("h", false, "Print help")
helloFlags.String("n", "World!", "A name to greet.")
app.Subcommand("hello", "Print hello.", "example hello -n Matt", helloFlags).
Does(fmt.Printf, "out").
Using("format").WithDefault("Hello %s\n").
Using("0").WithDefault("World").From("cxt:n")
// Run the app, and let it figure out which subcommand to run.
app.RunSubcommand()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment