コード例 #1
0
ファイル: late-inject.go プロジェクト: TMaYaD/clif
func main() {

	// init cli app
	cli := clif.New("my-app", "Demo for using config", "1.2.3").SetDescription(description)

	// add a default option, which registers a new object in the injection container
	cli.AddDefaultOptions(
		clif.NewOption("config", "c", "Path to config", "fixtures/config.json", true, false).SetSetup(func(name, value string) (string, error) {
			conf := &MyConfig{make(map[string]string)}
			if raw, err := ioutil.ReadFile(value); err != nil {
				return "", fmt.Errorf("Could not read config file %s: %s", value, err)
			} else if err = json.Unmarshal(raw, &conf.Data); err != nil {
				return "", fmt.Errorf("Could not unmarshal config file %s: %s", value, err)
			} else if _, ok := conf.Data["name"]; !ok {
				return "", fmt.Errorf("Config %s is missing \"name\"", value)
			} else {
				cli.Register(conf)
				return value, nil
			}
		}),
	)

	// add command which uses the late injected configf
	cli.Add(clif.NewCommand("xxx", "Call xxx", func(c *clif.Command, foo *MyConfig, out clif.Output) {
		out.Printf("Hello there: <success>%s<reset>\n", foo.Data["name"])
	}))

	// add another ocmmand, using the config as well
	cli.Add(clif.NewCommand("yyy", "Call yyy", func(c *clif.Command, foo *MyConfig, out clif.Output) {
		out.Printf("Hello there: <success>%s<reset>\n", foo.Data["name"])
	}))

	cli.Run()
}
コード例 #2
0
ファイル: injection.go プロジェクト: TMaYaD/clif
func main() {
	cli := clif.New("my-app", "My kewl App", "0.8.5")
	cmd := clif.NewCommand("call", "Call me", callMe)
	cli.Add(cmd)
	cli.Register(new(MyBaz)).
		RegisterAs(reflect.TypeOf((*MyFoo)(nil)).Elem().String(), new(MyBar))
	cli.Run()
}
コード例 #3
0
ファイル: extended.go プロジェクト: TMaYaD/clif
func main() {
	setStyle(os.Getenv("CLI_STYLE"), nil)

	// extend output styles
	clif.DefaultStyles["mine"] = "\033[32;1m"

	// initialize the app with custom registered objects in the injection container
	c := clif.New("My App", "1.0.0", "An example application").
		Register(&exampleStruct{"bar1"}).
		RegisterAs(reflect.TypeOf((*exampleInterface)(nil)).Elem().String(), &exampleStruct{"bar2"}).
		New("hello", "The obligatory hello world", callHello)

	styleArg := clif.NewArgument("style", "Name of a style. Available: default, sunburn, winter", "default", true, false).
		SetParse(func(name, value string) (string, error) { setStyle(value, c); return value, nil })
	c.Add(clif.NewCommand("styles", "Print all color style tokens", callStyles).AddArgument(styleArg))

	// customize error handler
	clif.Die = func(msg string, args ...interface{}) {
		c.Output().Printf("<error>Everyting went wrong: %s<reset>\n\n", fmt.Sprintf(msg, args...))
		clif.Exit(1)
	}

	// build & add a complex command
	cmd := clif.NewCommand("foo", "It does foo", callFoo).
		NewArgument("name", "Name for greeting", "", true, false).
		NewArgument("more-names", "And more names for greeting", "", false, true).
		NewOption("whatever", "w", "Some required option", "", true, false)
	cnt := clif.NewOption("counter", "c", "Show how high you can count", "", false, false)
	cnt.SetParse(clif.IsInt)
	cmd.AddOption(cnt)
	c.Add(cmd)

	cb := func(c *clif.Command, out clif.Output) {
		out.Printf("Called %s\n", c.Name)
	}
	c.New("bar:baz", "A grouped command", cb).
		New("bar:zoing", "Another grouped command", cb).
		New("hmm:huh", "Yet another grouped command", cb).
		New("hmm:uhm", "And yet another grouped command", cb)

	// execute the main loop
	c.Run()
}